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 N=1005;
int x,k,q,p[N],s[N],a[N];
int get(int a){
return (a==p[a]?a:p[a]=get(p[a]));
}
void merge(int l,int r){
l=get(l),r=get(r);
if(l==r)return;
if(s[l]>s[r])swap(l,r);
s[r]+=s[l];
p[l]=r;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>x>>k>>q;
for(int i=1;i<=x;i++)p[i]=i,s[i]=1;
for(int j=1;j<=k;j++){
for(int i=1;i<=x;i++)cin>>a[i];
for(int i=1;i<=x;i++)merge(i,a[i]);
}
while(q--){
int l,r;cin>>l>>r;
cout<<(get(l)==get(r)?"DA":"NE")<<"\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... |