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 = 1000 + 5;
int n, m, q, a, b, arr[N];
struct DSU{
int par[N], stsize[N];
DSU(){
for(int i = 1; i < N; i++) par[i] = i, stsize[i] = 1;
}
int setfind(int a){
if(par[a] == a) return a;
else return par[a] = setfind(par[a]);
}
void setunion(int a, int b){
a = setfind(a), b = setfind(b);
if(a != b){
if(stsize[b] > stsize[a]) swap(a, b);
par[b] = par[a];
stsize[a] += stsize[b];
}
}
};
DSU dsu;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m >> q;
for(int i = 1; i <= m; i++){
for(int i = 1; i <= n; i++) cin >> arr[i];
for(int i = 1; i <= n; i++) dsu.setunion(i, arr[i]);
}
while(q--){
cin >> a >> b;
if(dsu.setfind(a) == dsu.setfind(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... |