Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 1005;
- int idx[maxn];
- void init() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- }
- }
- int find_root(int x) {
- while(x != idx[x]) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- void union_set(int A, int B) {
- int rootA = find_root(A);
- int rootB = find_root(B);
- idx[rootA] = idx[rootB];
- }
- bool check_set(int A, int B) {
- return (find_root(A) == find_root(B));
- }
- int main() {
- init();
- while(true) {
- string s;
- cin >> s;
- int A, B;
- cin >> A >> B;
- if(s == "union") {
- union_set(A, B);
- }
- else if(s == "check") {
- if(check_set(A, B)) {
- cout << "YES" << endl;
- }
- else {
- cout << "NO" << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment