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;
#define ll long long
#define pb push_back
const int mxN = 1e3 + 5;
ll n,p[mxN],m,q;
bool vis[mxN];
struct ds{
vector<ll> par,rank;
void init(){
par.resize(mxN);
rank.assign(mxN, 0LL);
for(int i = 0; i <= n; i++) par[i] = i;
}
ll find(ll a){
if(par[a] == a) return a;
par[a] = find(par[a]);
return par[a];
}
void merge(ll a, ll b){
a = find(a);
b = find(b);
if(a == b) return;
par[b] = a;
rank[a] += (rank[a] == rank[b]);
}
};
ds s;
void dfs(ll at){
if(vis[at]) return;
vis[at] = true;
s.merge(at, p[at]);
dfs(p[at]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
s.init();
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
cin >> p[j];
vis[j] = false;
}
for(int j = 1; j <= n; j++){
dfs(j);
}
}
while(q--){
ll a, b;
cin >> a >> b;
if(s.find(a) == s.find(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... |