明明记得之前应该有并查集的板子在博客里?怎么找不到了??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; int father[10005]; int _find(int num) { if (father[num] == -1)return num; else return father[num] = _find(father[num]); } void _union(int v1, int v2) { if (_find(v1) == _find(v2))return; father[_find(v2)] = v1; } int n, m; int main(){ memset(father, -1, sizeof(father)); cin >> n >> m; while (m--) { int z, x, y; cin >> z >> x >> y; if (z == 1)_union(x, y); else cout << (_find(x) == _find(y) ? 'Y' : 'N') << endl; } } |