//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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
771 ms |
5576 KB |
Output is correct |
2 |
Correct |
669 ms |
4852 KB |
Output is correct |
3 |
Correct |
640 ms |
4556 KB |
Output is correct |
4 |
Correct |
755 ms |
5572 KB |
Output is correct |
5 |
Correct |
780 ms |
5596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
324 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
316 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
324 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
316 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
312 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
316 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
771 ms |
5576 KB |
Output is correct |
2 |
Correct |
669 ms |
4852 KB |
Output is correct |
3 |
Correct |
640 ms |
4556 KB |
Output is correct |
4 |
Correct |
755 ms |
5572 KB |
Output is correct |
5 |
Correct |
780 ms |
5596 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
324 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
336 KB |
Output is correct |
14 |
Correct |
2 ms |
316 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
320 KB |
Output is correct |
19 |
Correct |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
316 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
951 ms |
9368 KB |
Output is correct |
27 |
Correct |
945 ms |
9432 KB |
Output is correct |
28 |
Correct |
786 ms |
7796 KB |
Output is correct |
29 |
Correct |
920 ms |
9080 KB |
Output is correct |
30 |
Correct |
809 ms |
8144 KB |
Output is correct |
31 |
Correct |
806 ms |
8084 KB |
Output is correct |
32 |
Correct |
967 ms |
9548 KB |
Output is correct |
33 |
Correct |
954 ms |
9340 KB |
Output is correct |
34 |
Correct |
947 ms |
9608 KB |
Output is correct |
35 |
Correct |
950 ms |
9480 KB |
Output is correct |