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