Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, k, s[100001], p[100001], s1[100001], p1[100001], a, b;
- char h;
- int f(int x)
- {
- int g = x;
- while (p[g] != g)
- g = p[g];
- return g;
- }
- int f1(int x)
- {
- int g = x;
- while (p1[g] != g)
- g = p1[g];
- return g;
- }
- int main()
- {
- iostream::sync_with_stdio(0); cin.tie(0);
- cin >> n >> k;
- for (int i = 1 ; i <= n ; i++)
- {
- s[i] = 1;
- p[i] = i;
- s1[i] = 1;
- p1[i] = i;
- }
- for (int xx = 0 ; xx < k ; xx++)
- {
- cin >> h >> a >> b;
- int a1 = a, b1 = b;
- if (h == '+')
- {
- a = f(a);
- b = f(b);
- if (a != b)
- {
- if (s[a] < s[b])
- swap(a, b);
- s[a] += s[b];
- p[b] = a;
- }
- a1 = f1(a1);
- b1 = f1(b1);
- if (a1 == b1)
- continue;
- if (s1[a1] < s1[b1])
- swap(a1, b1);
- s1[a1] += s1[b1];
- p1[b1] = a1;
- }
- else if (h == '-')
- {
- a1 = f1(a1);
- b1 = f1(b1);
- if (a1 == b1)
- continue;
- if (s1[a1] < s1[b1])
- swap(a1, b1);
- s1[a1] += s1[b1];
- p1[b1] = a1;
- }
- else
- {
- a = f(a);
- b = f(b);
- if (a == b)
- cout << "+\n";
- else
- {
- a1 = f1(a1);
- b1 = f1(b1);
- if (a1 == b1)
- cout << "-\n";
- else
- cout << "?\n";
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement