Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int maxn=1e6+10;
- int idx[maxn];
- int s[maxn];
- int find_root( int x)
- {
- int t = x;
- while(idx[t]!=t)
- {
- idx[t]=idx[idx[t]];
- t=idx[t];
- }
- return t;
- }
- void u2e( int i, int j)
- {
- int idx_i=find_root(i);
- int idx_j=find_root(j);
- if(idx_i!=idx_j)
- {
- if(s[idx_i] > s[idx_j])
- {
- s[idx_i]+=s[idx_j];
- idx[idx_j]=idx[idx_i];
- }
- else
- {
- s[idx_j]+=s[idx_i];
- idx[idx_i]=idx[idx_j];
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- int n;
- int q;
- cin>>n>>q;
- for(int i=0;i<n;i++)
- {
- idx[i]=i;
- s[i]=1;
- }
- while(q--)
- {
- char c;
- int a,b;
- cin>>c>>a>>b;
- if(c=='=')
- {
- u2e(a,b);
- }
- else
- {
- if(find_root(a) == find_root(b))
- {
- printf("yes\n");
- }
- else
- {
- printf("no\n");
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement