#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct ufds{
int par[1005],rnk[1005];
void init(int n){
for(int i=0;i<n;++i)par[i]=i,rnk[i]=0;
}
int fp(int i){return (par[i]==i)?i:par[i]=fp(par[i]);}
void join(int x,int y){
x=fp(x),y=fp(y);
if(x==y)return;
if(rnk[x]<rnk[y])par[x]=y;
else par[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
}
}u;
int construct(vector<vector<int>> p){
int n=p.size();
u.init(n);
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(p[i][j]==3)return 0;
if(p[i][j]!=0)u.join(i,j);
}
}
vector<vector<int>> c1;
c1.resize(n);
for(int i=0;i<n;++i){
c1[u.fp(i)].push_back(i);
for(int j=0;j<n;++j){
if(u.fp(i)==u.fp(j)&&p[i][j]==0){
return 0;
}
}
}
u.init(n);
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(p[i][j]==1)u.join(i,j);
}
}
vector<vector<int>> c2,c3;
c2.resize(n);
c3.resize(n);
for(int i=0;i<n;++i){
c2[u.fp(i)].push_back(i);
for(int j:c1[i]){//single cycle
if(u.fp(j)==j)c3[i].push_back(j);
}
}
for(int i=0;i<n;++i){
for(int a:c1[i]){
for(int b:c1[i]){
if(u.fp(a)!=u.fp(b)&&p[a][b]!=2){
return 0;
}
if(u.fp(a)==u.fp(b)&&p[a][b]!=1){
return 0;
}
}
}
}
vector<vector<int>> answer;
answer.resize(n);
for(int i=0;i<n;++i)answer[i].resize(n);
for(int i=0;i<n;++i){
for(int j:c2[i]){
if(i!=j)answer[i][j]=answer[j][i]=1;
}
}
for(int i=0;i<n;++i){
if(c3[i].size()==2)return 0;
if(c3[i].size()>=3){
int m=c3[i].size();
for(int j=0;j<m;++j){
int a=c3[i][j],b=c3[i][(j+1)%m];
if(a!=b)answer[a][b]=answer[b][a]=1;
}
}
}
build(answer);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1200 KB |
Output is correct |
7 |
Correct |
184 ms |
24076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1200 KB |
Output is correct |
7 |
Correct |
184 ms |
24076 KB |
Output is correct |
8 |
Correct |
0 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1208 KB |
Output is correct |
13 |
Correct |
190 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
280 KB |
Output is correct |
16 |
Correct |
4 ms |
656 KB |
Output is correct |
17 |
Correct |
78 ms |
10096 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
54 ms |
6244 KB |
Output is correct |
21 |
Correct |
185 ms |
24052 KB |
Output is correct |
22 |
Correct |
172 ms |
24056 KB |
Output is correct |
23 |
Correct |
200 ms |
24016 KB |
Output is correct |
24 |
Correct |
198 ms |
24132 KB |
Output is correct |
25 |
Correct |
67 ms |
10100 KB |
Output is correct |
26 |
Correct |
65 ms |
10092 KB |
Output is correct |
27 |
Correct |
200 ms |
23992 KB |
Output is correct |
28 |
Correct |
180 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
9 ms |
1284 KB |
Output is correct |
9 |
Correct |
188 ms |
24180 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1228 KB |
Output is correct |
13 |
Correct |
210 ms |
24076 KB |
Output is correct |
14 |
Correct |
1 ms |
284 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
7 ms |
588 KB |
Output is correct |
17 |
Correct |
104 ms |
10156 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
52 ms |
6208 KB |
Output is correct |
22 |
Correct |
261 ms |
24036 KB |
Output is correct |
23 |
Correct |
192 ms |
24060 KB |
Output is correct |
24 |
Correct |
234 ms |
24088 KB |
Output is correct |
25 |
Correct |
80 ms |
14188 KB |
Output is correct |
26 |
Correct |
74 ms |
10108 KB |
Output is correct |
27 |
Correct |
233 ms |
24048 KB |
Output is correct |
28 |
Correct |
222 ms |
24056 KB |
Output is correct |
29 |
Correct |
85 ms |
14192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
51 ms |
6260 KB |
Output is correct |
5 |
Correct |
179 ms |
24088 KB |
Output is correct |
6 |
Correct |
195 ms |
24132 KB |
Output is correct |
7 |
Correct |
206 ms |
24096 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
49 ms |
6252 KB |
Output is correct |
10 |
Correct |
173 ms |
24172 KB |
Output is correct |
11 |
Correct |
216 ms |
24044 KB |
Output is correct |
12 |
Correct |
186 ms |
24088 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
43 ms |
6240 KB |
Output is correct |
17 |
Correct |
197 ms |
24040 KB |
Output is correct |
18 |
Correct |
187 ms |
24080 KB |
Output is correct |
19 |
Correct |
184 ms |
24044 KB |
Output is correct |
20 |
Correct |
194 ms |
24048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1200 KB |
Output is correct |
7 |
Correct |
184 ms |
24076 KB |
Output is correct |
8 |
Correct |
0 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1208 KB |
Output is correct |
13 |
Correct |
190 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
280 KB |
Output is correct |
16 |
Correct |
4 ms |
656 KB |
Output is correct |
17 |
Correct |
78 ms |
10096 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
54 ms |
6244 KB |
Output is correct |
21 |
Correct |
185 ms |
24052 KB |
Output is correct |
22 |
Correct |
172 ms |
24056 KB |
Output is correct |
23 |
Correct |
200 ms |
24016 KB |
Output is correct |
24 |
Correct |
198 ms |
24132 KB |
Output is correct |
25 |
Correct |
67 ms |
10100 KB |
Output is correct |
26 |
Correct |
65 ms |
10092 KB |
Output is correct |
27 |
Correct |
200 ms |
23992 KB |
Output is correct |
28 |
Correct |
180 ms |
24172 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
296 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1284 KB |
Output is correct |
37 |
Correct |
188 ms |
24180 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
210 ms |
24076 KB |
Output is correct |
42 |
Correct |
1 ms |
284 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
7 ms |
588 KB |
Output is correct |
45 |
Correct |
104 ms |
10156 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
52 ms |
6208 KB |
Output is correct |
50 |
Correct |
261 ms |
24036 KB |
Output is correct |
51 |
Correct |
192 ms |
24060 KB |
Output is correct |
52 |
Correct |
234 ms |
24088 KB |
Output is correct |
53 |
Correct |
80 ms |
14188 KB |
Output is correct |
54 |
Correct |
74 ms |
10108 KB |
Output is correct |
55 |
Correct |
233 ms |
24048 KB |
Output is correct |
56 |
Correct |
222 ms |
24056 KB |
Output is correct |
57 |
Correct |
85 ms |
14192 KB |
Output is correct |
58 |
Correct |
0 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
4 ms |
688 KB |
Output is correct |
61 |
Correct |
94 ms |
10092 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
47 ms |
6244 KB |
Output is correct |
66 |
Correct |
66 ms |
10092 KB |
Output is correct |
67 |
Correct |
72 ms |
10052 KB |
Output is correct |
68 |
Correct |
70 ms |
10092 KB |
Output is correct |
69 |
Correct |
93 ms |
14192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1200 KB |
Output is correct |
7 |
Correct |
184 ms |
24076 KB |
Output is correct |
8 |
Correct |
0 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1208 KB |
Output is correct |
13 |
Correct |
190 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
280 KB |
Output is correct |
16 |
Correct |
4 ms |
656 KB |
Output is correct |
17 |
Correct |
78 ms |
10096 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
54 ms |
6244 KB |
Output is correct |
21 |
Correct |
185 ms |
24052 KB |
Output is correct |
22 |
Correct |
172 ms |
24056 KB |
Output is correct |
23 |
Correct |
200 ms |
24016 KB |
Output is correct |
24 |
Correct |
198 ms |
24132 KB |
Output is correct |
25 |
Correct |
67 ms |
10100 KB |
Output is correct |
26 |
Correct |
65 ms |
10092 KB |
Output is correct |
27 |
Correct |
200 ms |
23992 KB |
Output is correct |
28 |
Correct |
180 ms |
24172 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
296 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
296 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1284 KB |
Output is correct |
37 |
Correct |
188 ms |
24180 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1228 KB |
Output is correct |
41 |
Correct |
210 ms |
24076 KB |
Output is correct |
42 |
Correct |
1 ms |
284 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
7 ms |
588 KB |
Output is correct |
45 |
Correct |
104 ms |
10156 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
52 ms |
6208 KB |
Output is correct |
50 |
Correct |
261 ms |
24036 KB |
Output is correct |
51 |
Correct |
192 ms |
24060 KB |
Output is correct |
52 |
Correct |
234 ms |
24088 KB |
Output is correct |
53 |
Correct |
80 ms |
14188 KB |
Output is correct |
54 |
Correct |
74 ms |
10108 KB |
Output is correct |
55 |
Correct |
233 ms |
24048 KB |
Output is correct |
56 |
Correct |
222 ms |
24056 KB |
Output is correct |
57 |
Correct |
85 ms |
14192 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
296 KB |
Output is correct |
61 |
Correct |
51 ms |
6260 KB |
Output is correct |
62 |
Correct |
179 ms |
24088 KB |
Output is correct |
63 |
Correct |
195 ms |
24132 KB |
Output is correct |
64 |
Correct |
206 ms |
24096 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
49 ms |
6252 KB |
Output is correct |
67 |
Correct |
173 ms |
24172 KB |
Output is correct |
68 |
Correct |
216 ms |
24044 KB |
Output is correct |
69 |
Correct |
186 ms |
24088 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
43 ms |
6240 KB |
Output is correct |
74 |
Correct |
197 ms |
24040 KB |
Output is correct |
75 |
Correct |
187 ms |
24080 KB |
Output is correct |
76 |
Correct |
184 ms |
24044 KB |
Output is correct |
77 |
Correct |
194 ms |
24048 KB |
Output is correct |
78 |
Correct |
0 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
4 ms |
688 KB |
Output is correct |
81 |
Correct |
94 ms |
10092 KB |
Output is correct |
82 |
Correct |
0 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
0 ms |
204 KB |
Output is correct |
85 |
Correct |
47 ms |
6244 KB |
Output is correct |
86 |
Correct |
66 ms |
10092 KB |
Output is correct |
87 |
Correct |
72 ms |
10052 KB |
Output is correct |
88 |
Correct |
70 ms |
10092 KB |
Output is correct |
89 |
Correct |
93 ms |
14192 KB |
Output is correct |
90 |
Correct |
0 ms |
204 KB |
Output is correct |
91 |
Correct |
0 ms |
300 KB |
Output is correct |
92 |
Correct |
5 ms |
588 KB |
Output is correct |
93 |
Correct |
69 ms |
10020 KB |
Output is correct |
94 |
Correct |
0 ms |
204 KB |
Output is correct |
95 |
Correct |
0 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
21 ms |
2672 KB |
Output is correct |
98 |
Correct |
74 ms |
10096 KB |
Output is correct |
99 |
Correct |
64 ms |
10020 KB |
Output is correct |
100 |
Correct |
67 ms |
10096 KB |
Output is correct |
101 |
Correct |
77 ms |
10092 KB |
Output is correct |
102 |
Correct |
67 ms |
10096 KB |
Output is correct |
103 |
Correct |
69 ms |
10096 KB |
Output is correct |
104 |
Correct |
67 ms |
10076 KB |
Output is correct |
105 |
Correct |
68 ms |
10052 KB |
Output is correct |