Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int parent[100001],size[100001],d[100001], a[100001];
- string k;
- void make_set (int v) {
- parent[v] = v;
- size[v] = 1;
- }
- int find_set (int v) {
- if (v == parent[v])
- return v;
- return parent[v] = find_set (parent[v]);
- }
- void union_sets (int a, int b) {
- a = find_set (a);
- b = find_set (b);
- if (a != b) {
- if (size[a] < size[b])
- swap (a, b);
- parent[b] = a;
- size[a] += size[b];
- }
- }
- int main (){
- int n , m;
- cin >> n >> m;
- int b , c;
- for (int i = 1; i <= n; i++)
- make_set(i);
- for (int i = 1; i <= m; i++) {
- cin >> k;
- if (k == "Q") {
- int d1;
- cin >> d1;
- int ch = find_set(d1);
- cout << size[ch] << endl;
- }
- else {
- cin >> c >> b;
- union_sets(c,b);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement