1416 两点 并查集
2017-02-09 14:45
190 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1416
对于每一个坐标(x, y)
要使得它变成一个整数,一个方法就是x * max(n, m) + y
这样就不会相同了。可以幻想一下吧。
这样坐标的大小只会去到n * m + y这个级别,不会很大。
然后并查集那里,每一次都走下面和右边的点,看看能否合并,能的话就合并。然后
合并到相同的爸爸,就是有了。
AA
AA
其中(4, 4)这个点被(1, 2)合并了一次,爸爸是(1, 1)
然后又被(2, 1)合并一次,爸爸是相同了。
View Code
对于每一个坐标(x, y)
要使得它变成一个整数,一个方法就是x * max(n, m) + y
这样就不会相同了。可以幻想一下吧。
这样坐标的大小只会去到n * m + y这个级别,不会很大。
然后并查集那里,每一次都走下面和右边的点,看看能否合并,能的话就合并。然后
合并到相同的爸爸,就是有了。
AA
AA
其中(4, 4)这个点被(1, 2)合并了一次,爸爸是(1, 1)
然后又被(2, 1)合并一次,爸爸是相同了。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <bitset> const int maxn = 50 + 20; int fa[maxn * maxn * maxn]; char str[maxn][maxn]; int tofind(int u) { if (fa[u] == u) return fa[u]; else return fa[u] = tofind(fa[u]); } bool tomerge(int x, int y) { x = tofind(x); y = tofind(y); if (x == y) return false; fa[y] = x; return true; } int n, m; int calc(int x, int y) { return max(n, m) * x + y; } void work() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> str[i] + 1; for (int i = 1; i <= maxn * maxn * maxn - 1; ++i) fa[i] = i; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (j + 1 <= m && str[i][j] == str[i][j + 1]) { if (!tomerge(calc(i, j), calc(i, j + 1))) { // cout << i << endl; // cout << j << endl; cout << "YES" << endl; return; } } if (i + 1 <= n && str[i][j] == str[i + 1][j]) { if (!tomerge(calc(i, j), calc(i + 1, j))) { cout << "YES" << endl; return; } } } } cout << "NO" << endl; } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif work(); return 0; }
View Code
相关文章推荐
- 51nod 1416 两点【并查集】
- 【Codeforces Round 329 (Div 2) D】【LCA+并查集路径压缩】Happy Tree Party 除上两点间路径全部权值
- 51Nod - 1416 两点
- HDU 5723 Abandoned country (并查集 + DFS+求解树上任意两点间的距离的平均值)
- 两点(51nod 1416)
- 51nod 1416 两点 【dfs+特殊判断】
- 1416 两点 51NOD
- 51nod 1416两点DFS
- 51NOD 1416 两点
- 51Nod -1416 两点(dfs)
- 51nod1416-搜索&剪枝|并查集-两点
- 51Nod - 1416 两点(搜索)
- 【原创】【算法】【ygylca】狭义带权并查集求lca两点之间路径最大路问题。
- 51Nod 1416 两点
- 1416 两点(dfs)
- 51NOD 1416 两点 【水】DFS
- hdu 3938 Portal 计算两点路上最大距离中的最小值<=L的路径个数 离线并查集
- nod-1416-两点
- 51nod 1416:两点 深搜
- 51nod 1416 两点 (dfs)