This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,k;
struct node{
int val;
int siz;
}fa[maxn];
int find(int x){
return x==fa[x].val?x:find(fa[x].val);
}
void merge(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy)return ;
int sizx=fa[fx].siz,sizy=fa[fy].siz;
if(fa[fx].siz<fa[fy].siz)swap(fx,fy);
fa[fy].val=fx;
fa[fx].siz=sizx+sizy;
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
fa[i].val=i;
fa[i].siz=1;
}
for(int i=1;i<=n+k-1;i++){
char op;
cin>>op;
if(op=='S'){
int a,b;cin>>a>>b;
merge(a,b);
}
else if(op=='Q'){
int a,b;cin>>a>>b;
int fx=find(a),fy=find(b);
if(fx==fy)cout<<"yes\n";
else cout<<"no\n";
}
else {
int a;
cin>>a;
int f=find(a);
cout<<fa[f].siz<<'\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |