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