#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
5652 KB |
Output is correct |
2 |
Correct |
50 ms |
4912 KB |
Output is correct |
3 |
Correct |
47 ms |
4608 KB |
Output is correct |
4 |
Correct |
61 ms |
5716 KB |
Output is correct |
5 |
Correct |
62 ms |
5592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
5652 KB |
Output is correct |
2 |
Correct |
50 ms |
4912 KB |
Output is correct |
3 |
Correct |
47 ms |
4608 KB |
Output is correct |
4 |
Correct |
61 ms |
5716 KB |
Output is correct |
5 |
Correct |
62 ms |
5592 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
101 ms |
9556 KB |
Output is correct |
27 |
Correct |
105 ms |
9540 KB |
Output is correct |
28 |
Correct |
86 ms |
7780 KB |
Output is correct |
29 |
Correct |
107 ms |
9044 KB |
Output is correct |
30 |
Correct |
87 ms |
8004 KB |
Output is correct |
31 |
Correct |
98 ms |
8128 KB |
Output is correct |
32 |
Correct |
102 ms |
9564 KB |
Output is correct |
33 |
Correct |
107 ms |
9540 KB |
Output is correct |
34 |
Correct |
99 ms |
9420 KB |
Output is correct |
35 |
Correct |
106 ms |
9556 KB |
Output is correct |