This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//LCA
#include <bits/stdc++.h>
using namespace std;
//DSU
int pr[1001];
int gs[1001];
int findleader(int x){
if(pr[x]==x){
return x;
}
return pr[x] = findleader(pr[x]);
}
bool samegroup(int x,int y){
int led1 = findleader(x);
int led2 = findleader(y);
return led1==led2;
}
void mergegroup(int x,int y){
int led1 = findleader(x);
int led2 = findleader(y);
if(led1==led2)return;
if(gs[led1]>gs[led2]){
pr[led2]=led1;
gs[led1]+=gs[led2];
}else{
pr[led1]=led2;
gs[led2]+=gs[led1];
}
}
int get(int x){
int led = findleader(x);
return gs[led];
}
int main(){
int n,m,q;
cin>>n>>m>>q;
for(int i = 0;i<=n;i++)pr[i] = i , gs[i] = 1;
for(int i = 0;i<m;i++){
for(int j = 1;j<=n;j++){
int x;cin>>x;
mergegroup(x,j);
}
}
while(q--){
int a,b;cin>>a>>b;
if(samegroup(a,b))cout<<"DA\n";
else cout<<"NE\n";
}
}
# | 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... |