#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
//void build(vector<vector<int> >);
struct UnionFind {
vector<int> root;
UnionFind(int N) {
root.resize(N);
fill(root.begin(),root.end(),-1);
}
int Find(int a) {
if(root[a] < 0) return a;
int r = Find(root[a]);
root[a] = r;
return r;
}
void Merge(int a, int b) {
a = Find(a);
b = Find(b);
if(a == b) return;
if(root[a] > root[b]) swap(a, b);
root[a] += root[b];
root[b] = a;
}
};
vector<vector<int> > adj;
vector<vector<int> > adj2;
int construct(vector<vector<int> > p) {
vector<vector<int> > ans;
int N = p.size();
ans.resize(N);
int i;
for(i=0;i<N;i++) ans[i].resize(N);
int j;
UnionFind UF1(N);
UnionFind UF2(N);
adj.resize(N);
adj2.resize(N);
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
if(p[i][j]==1) {
UF2.Merge(i, j);
}
if(p[i][j]) {
UF1.Merge(i, j);
}
if(p[i][j]==3) return 0;
}
}
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
if(!p[i][j]) {
if(UF1.Find(i)==UF1.Find(j)) return 0;
if(UF2.Find(i)==UF2.Find(j)) return 0;
}
if(p[i][j] == 2) {
if(UF2.Find(i)==UF2.Find(j)) return 0;
}
}
}
for(i=0;i<N;i++) {
adj2[UF2.Find(i)].push_back(i);
}
for(i=0;i<N;i++) {
if(!adj2[i].empty()) {
//cout << adj2[i][0] << ' ';
adj[UF1.Find(adj2[i][0])].push_back(adj2[i][0]);
}
}
for(i=0;i<N;i++) {
int sz = adj[i].size();
//if(sz == 1) return 0;
if(sz == 2) return 0;
if(sz >= 3) {
for(j=0;j<sz-1;j++) {
ans[adj[i][j]][adj[i][j+1]] = 1;
ans[adj[i][j+1]][adj[i][j]] = 1;
}
ans[adj[i][0]][adj[i][sz-1]] = 1;
ans[adj[i][sz-1]][adj[i][0]] = 1;
}
int sz2 = adj2[i].size();
for(j=0;j<sz2-1;j++) {
ans[adj2[i][j]][adj2[i][j+1]] = 1;
ans[adj2[i][j+1]][adj2[i][j]] = 1;
}
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1092 KB |
Output is correct |
7 |
Correct |
240 ms |
22148 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1092 KB |
Output is correct |
7 |
Correct |
240 ms |
22148 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
227 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
111 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5756 KB |
Output is correct |
21 |
Correct |
238 ms |
22012 KB |
Output is correct |
22 |
Correct |
236 ms |
22084 KB |
Output is correct |
23 |
Correct |
258 ms |
22016 KB |
Output is correct |
24 |
Correct |
245 ms |
22208 KB |
Output is correct |
25 |
Correct |
97 ms |
12168 KB |
Output is correct |
26 |
Correct |
93 ms |
12156 KB |
Output is correct |
27 |
Correct |
249 ms |
22144 KB |
Output is correct |
28 |
Correct |
228 ms |
22084 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
10 ms |
1100 KB |
Output is correct |
9 |
Correct |
254 ms |
22152 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 |
1100 KB |
Output is correct |
13 |
Correct |
233 ms |
22032 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
111 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
59 ms |
5756 KB |
Output is correct |
22 |
Correct |
251 ms |
22140 KB |
Output is correct |
23 |
Correct |
230 ms |
22048 KB |
Output is correct |
24 |
Correct |
247 ms |
22016 KB |
Output is correct |
25 |
Correct |
105 ms |
12156 KB |
Output is correct |
26 |
Correct |
96 ms |
12152 KB |
Output is correct |
27 |
Correct |
232 ms |
22156 KB |
Output is correct |
28 |
Correct |
260 ms |
22084 KB |
Output is correct |
29 |
Correct |
110 ms |
12232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
68 ms |
5748 KB |
Output is correct |
5 |
Correct |
236 ms |
22084 KB |
Output is correct |
6 |
Correct |
237 ms |
22084 KB |
Output is correct |
7 |
Correct |
269 ms |
22148 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
59 ms |
5720 KB |
Output is correct |
10 |
Correct |
233 ms |
22100 KB |
Output is correct |
11 |
Correct |
229 ms |
22084 KB |
Output is correct |
12 |
Correct |
258 ms |
22184 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
59 ms |
5756 KB |
Output is correct |
17 |
Correct |
238 ms |
22152 KB |
Output is correct |
18 |
Correct |
236 ms |
22036 KB |
Output is correct |
19 |
Correct |
238 ms |
22032 KB |
Output is correct |
20 |
Correct |
227 ms |
22152 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1092 KB |
Output is correct |
7 |
Correct |
240 ms |
22148 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
227 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
111 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5756 KB |
Output is correct |
21 |
Correct |
238 ms |
22012 KB |
Output is correct |
22 |
Correct |
236 ms |
22084 KB |
Output is correct |
23 |
Correct |
258 ms |
22016 KB |
Output is correct |
24 |
Correct |
245 ms |
22208 KB |
Output is correct |
25 |
Correct |
97 ms |
12168 KB |
Output is correct |
26 |
Correct |
93 ms |
12156 KB |
Output is correct |
27 |
Correct |
249 ms |
22144 KB |
Output is correct |
28 |
Correct |
228 ms |
22084 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1100 KB |
Output is correct |
37 |
Correct |
254 ms |
22152 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 |
1100 KB |
Output is correct |
41 |
Correct |
233 ms |
22032 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
111 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
5756 KB |
Output is correct |
50 |
Correct |
251 ms |
22140 KB |
Output is correct |
51 |
Correct |
230 ms |
22048 KB |
Output is correct |
52 |
Correct |
247 ms |
22016 KB |
Output is correct |
53 |
Correct |
105 ms |
12156 KB |
Output is correct |
54 |
Correct |
96 ms |
12152 KB |
Output is correct |
55 |
Correct |
232 ms |
22156 KB |
Output is correct |
56 |
Correct |
260 ms |
22084 KB |
Output is correct |
57 |
Correct |
110 ms |
12232 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
6 ms |
716 KB |
Output is correct |
61 |
Correct |
116 ms |
12160 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
61 ms |
5756 KB |
Output is correct |
66 |
Correct |
97 ms |
12156 KB |
Output is correct |
67 |
Correct |
96 ms |
12152 KB |
Output is correct |
68 |
Correct |
99 ms |
12164 KB |
Output is correct |
69 |
Correct |
106 ms |
12152 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 |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1092 KB |
Output is correct |
7 |
Correct |
240 ms |
22148 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1172 KB |
Output is correct |
13 |
Correct |
227 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
111 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
60 ms |
5756 KB |
Output is correct |
21 |
Correct |
238 ms |
22012 KB |
Output is correct |
22 |
Correct |
236 ms |
22084 KB |
Output is correct |
23 |
Correct |
258 ms |
22016 KB |
Output is correct |
24 |
Correct |
245 ms |
22208 KB |
Output is correct |
25 |
Correct |
97 ms |
12168 KB |
Output is correct |
26 |
Correct |
93 ms |
12156 KB |
Output is correct |
27 |
Correct |
249 ms |
22144 KB |
Output is correct |
28 |
Correct |
228 ms |
22084 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1100 KB |
Output is correct |
37 |
Correct |
254 ms |
22152 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 |
1100 KB |
Output is correct |
41 |
Correct |
233 ms |
22032 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
111 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
5756 KB |
Output is correct |
50 |
Correct |
251 ms |
22140 KB |
Output is correct |
51 |
Correct |
230 ms |
22048 KB |
Output is correct |
52 |
Correct |
247 ms |
22016 KB |
Output is correct |
53 |
Correct |
105 ms |
12156 KB |
Output is correct |
54 |
Correct |
96 ms |
12152 KB |
Output is correct |
55 |
Correct |
232 ms |
22156 KB |
Output is correct |
56 |
Correct |
260 ms |
22084 KB |
Output is correct |
57 |
Correct |
110 ms |
12232 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
68 ms |
5748 KB |
Output is correct |
62 |
Correct |
236 ms |
22084 KB |
Output is correct |
63 |
Correct |
237 ms |
22084 KB |
Output is correct |
64 |
Correct |
269 ms |
22148 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
59 ms |
5720 KB |
Output is correct |
67 |
Correct |
233 ms |
22100 KB |
Output is correct |
68 |
Correct |
229 ms |
22084 KB |
Output is correct |
69 |
Correct |
258 ms |
22184 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
59 ms |
5756 KB |
Output is correct |
74 |
Correct |
238 ms |
22152 KB |
Output is correct |
75 |
Correct |
236 ms |
22036 KB |
Output is correct |
76 |
Correct |
238 ms |
22032 KB |
Output is correct |
77 |
Correct |
227 ms |
22152 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
6 ms |
716 KB |
Output is correct |
81 |
Correct |
116 ms |
12160 KB |
Output is correct |
82 |
Correct |
0 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
61 ms |
5756 KB |
Output is correct |
86 |
Correct |
97 ms |
12156 KB |
Output is correct |
87 |
Correct |
96 ms |
12152 KB |
Output is correct |
88 |
Correct |
99 ms |
12164 KB |
Output is correct |
89 |
Correct |
106 ms |
12152 KB |
Output is correct |
90 |
Correct |
0 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
4 ms |
716 KB |
Output is correct |
93 |
Correct |
101 ms |
12164 KB |
Output is correct |
94 |
Correct |
0 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
24 ms |
3652 KB |
Output is correct |
98 |
Correct |
120 ms |
14068 KB |
Output is correct |
99 |
Correct |
97 ms |
14068 KB |
Output is correct |
100 |
Correct |
94 ms |
14020 KB |
Output is correct |
101 |
Correct |
95 ms |
14116 KB |
Output is correct |
102 |
Correct |
94 ms |
14068 KB |
Output is correct |
103 |
Correct |
93 ms |
14020 KB |
Output is correct |
104 |
Correct |
95 ms |
14200 KB |
Output is correct |
105 |
Correct |
104 ms |
14076 KB |
Output is correct |