#include "supertrees.h"
#include <algorithm>
#include <vector>
struct DSU
{
public:
std::vector<int> p, r;
DSU(int N): p(N, -1), r(N, 0) {}
int find(int n) {return p[n] == -1 ? n : p[n] = find(p[n]);}
bool merge(int u, int v)
{
u=find(u), v=find(v);
if(u==v) return 0;
if(r[u] < r[v]) std::swap(u, v);
p[v]=u, r[u]+=r[u]==r[v], r[v]=-1;
return 1;
}
};
int construct(std::vector<std::vector<int>> p)
{
int N = p.size();
std::vector<std::vector<int>> answer(N, std::vector<int>(N, 0));
DSU dsu1(N);
for (int i = 0; i < N; i++)
for(int j=i+1;j<N;++j)
if(p[i][j]==1)
{
if(dsu1.merge(i, j))
{
if(p[i] != p[j]) // must have identical adj lists
return 0;
answer[i][j]=1; // this makes a tree among all 1-con components
answer[j][i]=1;
}
}
std::vector<int> head;
for(int i=0;i<N;++i) if(dsu1.p[i]==-1) head.push_back(i);
for(int sz: {2,3}) // handle 2/3 case
{
std::vector<std::vector<int>> group(N, std::vector<int>());
DSU dsu2(N);
for(int i=0;i<(int)head.size();++i)
for(int j=i+1;j<(int)head.size();++j)
if(p[head[i]][head[j]]==sz)
dsu2.merge(head[i], head[j]);
for(int i=0;i<N;++i)
group[dsu2.find(i)].push_back(i);
for(int i=0;i<N;++i)
if(group[i].size() >= 2)
{
if(group[i].size() <= 2) return 0;
if(sz == 3 && group[i].size() <= 4) return 0;
for(int j=0;j<(int)group[i].size();++j) // create a simple cycle
{
int u=group[i][j], v=group[i][(j+1)%group[i].size()];
answer[u][v]=answer[v][u]=1;
}
if(sz == 3)
answer[group[i][0]][group[i][2]]=answer[group[i][2]][group[i][0]]=1;
//identicality test
for(int j=0;j<(int)group[i].size();++j)
for(int k=j+1;k<(int)group[i].size();++k)
if(p[group[i][j]][group[i][k]] != sz)
return 0;
}
}
build(answer);
return 1;
}
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
9 ms |
1100 KB |
Output is correct |
7 |
Correct |
243 ms |
21996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
9 ms |
1100 KB |
Output is correct |
7 |
Correct |
243 ms |
21996 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 |
1168 KB |
Output is correct |
13 |
Correct |
237 ms |
21932 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
98 ms |
12100 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
64 ms |
5744 KB |
Output is correct |
21 |
Correct |
240 ms |
23912 KB |
Output is correct |
22 |
Correct |
227 ms |
23920 KB |
Output is correct |
23 |
Correct |
232 ms |
23920 KB |
Output is correct |
24 |
Correct |
284 ms |
23900 KB |
Output is correct |
25 |
Correct |
96 ms |
14048 KB |
Output is correct |
26 |
Correct |
91 ms |
14056 KB |
Output is correct |
27 |
Correct |
236 ms |
23996 KB |
Output is correct |
28 |
Correct |
236 ms |
24000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
9 ms |
1100 KB |
Output is correct |
9 |
Correct |
221 ms |
21956 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1144 KB |
Output is correct |
13 |
Correct |
226 ms |
22016 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
716 KB |
Output is correct |
17 |
Correct |
106 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 |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
55 ms |
5724 KB |
Output is correct |
22 |
Correct |
240 ms |
21956 KB |
Output is correct |
23 |
Correct |
219 ms |
22008 KB |
Output is correct |
24 |
Correct |
237 ms |
22152 KB |
Output is correct |
25 |
Correct |
95 ms |
12140 KB |
Output is correct |
26 |
Correct |
94 ms |
12144 KB |
Output is correct |
27 |
Correct |
231 ms |
22016 KB |
Output is correct |
28 |
Correct |
231 ms |
21956 KB |
Output is correct |
29 |
Correct |
92 ms |
12100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
56 ms |
6212 KB |
Output is correct |
5 |
Correct |
222 ms |
23912 KB |
Output is correct |
6 |
Correct |
221 ms |
23876 KB |
Output is correct |
7 |
Correct |
236 ms |
23928 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
61 ms |
6160 KB |
Output is correct |
10 |
Correct |
225 ms |
23920 KB |
Output is correct |
11 |
Correct |
223 ms |
23916 KB |
Output is correct |
12 |
Correct |
243 ms |
23880 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
276 KB |
Output is correct |
16 |
Correct |
59 ms |
6236 KB |
Output is correct |
17 |
Correct |
241 ms |
23972 KB |
Output is correct |
18 |
Correct |
229 ms |
23904 KB |
Output is correct |
19 |
Correct |
224 ms |
23988 KB |
Output is correct |
20 |
Correct |
221 ms |
24100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
9 ms |
1100 KB |
Output is correct |
7 |
Correct |
243 ms |
21996 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 |
1168 KB |
Output is correct |
13 |
Correct |
237 ms |
21932 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
98 ms |
12100 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
64 ms |
5744 KB |
Output is correct |
21 |
Correct |
240 ms |
23912 KB |
Output is correct |
22 |
Correct |
227 ms |
23920 KB |
Output is correct |
23 |
Correct |
232 ms |
23920 KB |
Output is correct |
24 |
Correct |
284 ms |
23900 KB |
Output is correct |
25 |
Correct |
96 ms |
14048 KB |
Output is correct |
26 |
Correct |
91 ms |
14056 KB |
Output is correct |
27 |
Correct |
236 ms |
23996 KB |
Output is correct |
28 |
Correct |
236 ms |
24000 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1100 KB |
Output is correct |
37 |
Correct |
221 ms |
21956 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1144 KB |
Output is correct |
41 |
Correct |
226 ms |
22016 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
716 KB |
Output is correct |
45 |
Correct |
106 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 |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
55 ms |
5724 KB |
Output is correct |
50 |
Correct |
240 ms |
21956 KB |
Output is correct |
51 |
Correct |
219 ms |
22008 KB |
Output is correct |
52 |
Correct |
237 ms |
22152 KB |
Output is correct |
53 |
Correct |
95 ms |
12140 KB |
Output is correct |
54 |
Correct |
94 ms |
12144 KB |
Output is correct |
55 |
Correct |
231 ms |
22016 KB |
Output is correct |
56 |
Correct |
231 ms |
21956 KB |
Output is correct |
57 |
Correct |
92 ms |
12100 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
4 ms |
800 KB |
Output is correct |
61 |
Correct |
96 ms |
14064 KB |
Output is correct |
62 |
Correct |
1 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 |
56 ms |
6232 KB |
Output is correct |
66 |
Correct |
97 ms |
14044 KB |
Output is correct |
67 |
Correct |
92 ms |
14060 KB |
Output is correct |
68 |
Correct |
96 ms |
14056 KB |
Output is correct |
69 |
Correct |
94 ms |
14152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
9 ms |
1100 KB |
Output is correct |
7 |
Correct |
243 ms |
21996 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 |
1168 KB |
Output is correct |
13 |
Correct |
237 ms |
21932 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
98 ms |
12100 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
64 ms |
5744 KB |
Output is correct |
21 |
Correct |
240 ms |
23912 KB |
Output is correct |
22 |
Correct |
227 ms |
23920 KB |
Output is correct |
23 |
Correct |
232 ms |
23920 KB |
Output is correct |
24 |
Correct |
284 ms |
23900 KB |
Output is correct |
25 |
Correct |
96 ms |
14048 KB |
Output is correct |
26 |
Correct |
91 ms |
14056 KB |
Output is correct |
27 |
Correct |
236 ms |
23996 KB |
Output is correct |
28 |
Correct |
236 ms |
24000 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1100 KB |
Output is correct |
37 |
Correct |
221 ms |
21956 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1144 KB |
Output is correct |
41 |
Correct |
226 ms |
22016 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
716 KB |
Output is correct |
45 |
Correct |
106 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 |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
55 ms |
5724 KB |
Output is correct |
50 |
Correct |
240 ms |
21956 KB |
Output is correct |
51 |
Correct |
219 ms |
22008 KB |
Output is correct |
52 |
Correct |
237 ms |
22152 KB |
Output is correct |
53 |
Correct |
95 ms |
12140 KB |
Output is correct |
54 |
Correct |
94 ms |
12144 KB |
Output is correct |
55 |
Correct |
231 ms |
22016 KB |
Output is correct |
56 |
Correct |
231 ms |
21956 KB |
Output is correct |
57 |
Correct |
92 ms |
12100 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
56 ms |
6212 KB |
Output is correct |
62 |
Correct |
222 ms |
23912 KB |
Output is correct |
63 |
Correct |
221 ms |
23876 KB |
Output is correct |
64 |
Correct |
236 ms |
23928 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
61 ms |
6160 KB |
Output is correct |
67 |
Correct |
225 ms |
23920 KB |
Output is correct |
68 |
Correct |
223 ms |
23916 KB |
Output is correct |
69 |
Correct |
243 ms |
23880 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
276 KB |
Output is correct |
73 |
Correct |
59 ms |
6236 KB |
Output is correct |
74 |
Correct |
241 ms |
23972 KB |
Output is correct |
75 |
Correct |
229 ms |
23904 KB |
Output is correct |
76 |
Correct |
224 ms |
23988 KB |
Output is correct |
77 |
Correct |
221 ms |
24100 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
4 ms |
800 KB |
Output is correct |
81 |
Correct |
96 ms |
14064 KB |
Output is correct |
82 |
Correct |
1 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 |
56 ms |
6232 KB |
Output is correct |
86 |
Correct |
97 ms |
14044 KB |
Output is correct |
87 |
Correct |
92 ms |
14060 KB |
Output is correct |
88 |
Correct |
96 ms |
14056 KB |
Output is correct |
89 |
Correct |
94 ms |
14152 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
4 ms |
800 KB |
Output is correct |
93 |
Correct |
96 ms |
14044 KB |
Output is correct |
94 |
Correct |
1 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 |
24 ms |
3676 KB |
Output is correct |
98 |
Correct |
98 ms |
14060 KB |
Output is correct |
99 |
Correct |
93 ms |
14056 KB |
Output is correct |
100 |
Correct |
91 ms |
14044 KB |
Output is correct |
101 |
Correct |
101 ms |
14200 KB |
Output is correct |
102 |
Incorrect |
245 ms |
23916 KB |
Answer gives possible 1 while actual possible 0 |
103 |
Halted |
0 ms |
0 KB |
- |