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