#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU
{
int n = -37;
vector<int> par;
DSU(int sz) : n(sz) {
par.assign(n, -1);
}
int get(int x) {
return (par[x] < 0 ? x : par[x] = get(par[x]));
}
bool merge(int x, int y) {
x = get(x);
y = get(y);
if(x == y) {
return false;
}
assert(par[x] < 0 and par[y] < 0);
if(par[x] > par[y]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
DSU single(n);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(p[i][j] == 1) {
single.merge(i, j);
}
}
}
// cout << "Single\n---------\n";
// for(int i = 0; i < n; ++i) {
// cout << i << " : " << single.get(i) << endl;
// }
DSU cycles = single;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(p[i][j] == 2) {
if(single.get(i) == single.get(j)) {
return 0;
}
cycles.merge(i, j);
}
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(p[i][j] == 0) {
if(single.get(i) == single.get(j) or cycles.get(i) == cycles.get(j)) {
return 0;
}
}
}
}
vector<vector<int> > lines(n), answer(n, vector<int>(n)), circles(n);
for(int i = 0; i < n; ++i) {
lines[single.get(i)].push_back(i);
}
for(int i = 0; i < n; ++i) {
const int sz = lines[i].size();
// cout << "Line : ";
// for(int x : lines[i]) {
// cout << x << ' ';
// }cout << endl;
for(int j = 1; j < sz; ++j) {
int prev = lines[i][j - 1], cur = lines[i][j];
answer[prev][cur] = answer[cur][prev] = 1;
}
}
for(int i = 0; i < n; ++i) {
if(single.get(i) != i) {
continue;
}
circles[cycles.get(i)].push_back(i);
}
// cout << "Cycle\n------------\n";
// for(int i = 0; i < n; ++i) {
// cout << i << " : " << cycles.get(i) << endl;
// }
for(int i = 0; i < n; ++i) {
if(circles[i].size() < 2u) {
continue;
}
if(circles[i].size() == 2u) {
return 0;
}
const int sz = circles[i].size();
for(int j = 0; j < sz; ++j) {
int cur = circles[i][j], to = circles[i][(j + 1) % sz];
assert(answer[cur][to] == answer[to][cur] and answer[cur][to] == 0);
answer[cur][to] = answer[to][cur] = 1;
}
}
build(answer);
return 1;
}
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
2
1 0
0 1
2
1 2
2 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
98 ms |
24100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
98 ms |
24100 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
95 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
47 ms |
10244 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6264 KB |
Output is correct |
21 |
Correct |
97 ms |
24144 KB |
Output is correct |
22 |
Correct |
99 ms |
24144 KB |
Output is correct |
23 |
Correct |
102 ms |
24144 KB |
Output is correct |
24 |
Correct |
99 ms |
24144 KB |
Output is correct |
25 |
Correct |
46 ms |
10228 KB |
Output is correct |
26 |
Correct |
41 ms |
10068 KB |
Output is correct |
27 |
Correct |
99 ms |
24148 KB |
Output is correct |
28 |
Correct |
93 ms |
24152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
104 ms |
23860 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
436 KB |
Output is correct |
12 |
Correct |
4 ms |
1368 KB |
Output is correct |
13 |
Correct |
96 ms |
24068 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
48 ms |
10232 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
25 ms |
6224 KB |
Output is correct |
22 |
Correct |
97 ms |
24068 KB |
Output is correct |
23 |
Correct |
96 ms |
24032 KB |
Output is correct |
24 |
Correct |
102 ms |
24180 KB |
Output is correct |
25 |
Correct |
47 ms |
14196 KB |
Output is correct |
26 |
Correct |
41 ms |
10064 KB |
Output is correct |
27 |
Correct |
94 ms |
24144 KB |
Output is correct |
28 |
Correct |
107 ms |
24184 KB |
Output is correct |
29 |
Correct |
42 ms |
14160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
428 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
25 ms |
6236 KB |
Output is correct |
5 |
Correct |
99 ms |
24076 KB |
Output is correct |
6 |
Correct |
101 ms |
23980 KB |
Output is correct |
7 |
Correct |
104 ms |
24188 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
24 ms |
6232 KB |
Output is correct |
10 |
Correct |
95 ms |
24072 KB |
Output is correct |
11 |
Correct |
95 ms |
23888 KB |
Output is correct |
12 |
Correct |
105 ms |
24144 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
24 ms |
6340 KB |
Output is correct |
17 |
Correct |
95 ms |
24020 KB |
Output is correct |
18 |
Correct |
100 ms |
23980 KB |
Output is correct |
19 |
Correct |
99 ms |
24184 KB |
Output is correct |
20 |
Correct |
98 ms |
24144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
98 ms |
24100 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
95 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
47 ms |
10244 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6264 KB |
Output is correct |
21 |
Correct |
97 ms |
24144 KB |
Output is correct |
22 |
Correct |
99 ms |
24144 KB |
Output is correct |
23 |
Correct |
102 ms |
24144 KB |
Output is correct |
24 |
Correct |
99 ms |
24144 KB |
Output is correct |
25 |
Correct |
46 ms |
10228 KB |
Output is correct |
26 |
Correct |
41 ms |
10068 KB |
Output is correct |
27 |
Correct |
99 ms |
24148 KB |
Output is correct |
28 |
Correct |
93 ms |
24152 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
104 ms |
23860 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
436 KB |
Output is correct |
40 |
Correct |
4 ms |
1368 KB |
Output is correct |
41 |
Correct |
96 ms |
24068 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
48 ms |
10232 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
25 ms |
6224 KB |
Output is correct |
50 |
Correct |
97 ms |
24068 KB |
Output is correct |
51 |
Correct |
96 ms |
24032 KB |
Output is correct |
52 |
Correct |
102 ms |
24180 KB |
Output is correct |
53 |
Correct |
47 ms |
14196 KB |
Output is correct |
54 |
Correct |
41 ms |
10064 KB |
Output is correct |
55 |
Correct |
94 ms |
24144 KB |
Output is correct |
56 |
Correct |
107 ms |
24184 KB |
Output is correct |
57 |
Correct |
42 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
676 KB |
Output is correct |
61 |
Correct |
46 ms |
10244 KB |
Output is correct |
62 |
Correct |
0 ms |
600 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
24 ms |
6236 KB |
Output is correct |
66 |
Correct |
41 ms |
10244 KB |
Output is correct |
67 |
Correct |
42 ms |
10064 KB |
Output is correct |
68 |
Correct |
40 ms |
10064 KB |
Output is correct |
69 |
Correct |
42 ms |
14084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
98 ms |
24100 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
95 ms |
24148 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
47 ms |
10244 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6264 KB |
Output is correct |
21 |
Correct |
97 ms |
24144 KB |
Output is correct |
22 |
Correct |
99 ms |
24144 KB |
Output is correct |
23 |
Correct |
102 ms |
24144 KB |
Output is correct |
24 |
Correct |
99 ms |
24144 KB |
Output is correct |
25 |
Correct |
46 ms |
10228 KB |
Output is correct |
26 |
Correct |
41 ms |
10068 KB |
Output is correct |
27 |
Correct |
99 ms |
24148 KB |
Output is correct |
28 |
Correct |
93 ms |
24152 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
104 ms |
23860 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
436 KB |
Output is correct |
40 |
Correct |
4 ms |
1368 KB |
Output is correct |
41 |
Correct |
96 ms |
24068 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
48 ms |
10232 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
25 ms |
6224 KB |
Output is correct |
50 |
Correct |
97 ms |
24068 KB |
Output is correct |
51 |
Correct |
96 ms |
24032 KB |
Output is correct |
52 |
Correct |
102 ms |
24180 KB |
Output is correct |
53 |
Correct |
47 ms |
14196 KB |
Output is correct |
54 |
Correct |
41 ms |
10064 KB |
Output is correct |
55 |
Correct |
94 ms |
24144 KB |
Output is correct |
56 |
Correct |
107 ms |
24184 KB |
Output is correct |
57 |
Correct |
42 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
428 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
25 ms |
6236 KB |
Output is correct |
62 |
Correct |
99 ms |
24076 KB |
Output is correct |
63 |
Correct |
101 ms |
23980 KB |
Output is correct |
64 |
Correct |
104 ms |
24188 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
24 ms |
6232 KB |
Output is correct |
67 |
Correct |
95 ms |
24072 KB |
Output is correct |
68 |
Correct |
95 ms |
23888 KB |
Output is correct |
69 |
Correct |
105 ms |
24144 KB |
Output is correct |
70 |
Correct |
1 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
24 ms |
6340 KB |
Output is correct |
74 |
Correct |
95 ms |
24020 KB |
Output is correct |
75 |
Correct |
100 ms |
23980 KB |
Output is correct |
76 |
Correct |
99 ms |
24184 KB |
Output is correct |
77 |
Correct |
98 ms |
24144 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
2 ms |
676 KB |
Output is correct |
81 |
Correct |
46 ms |
10244 KB |
Output is correct |
82 |
Correct |
0 ms |
600 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
24 ms |
6236 KB |
Output is correct |
86 |
Correct |
41 ms |
10244 KB |
Output is correct |
87 |
Correct |
42 ms |
10064 KB |
Output is correct |
88 |
Correct |
40 ms |
10064 KB |
Output is correct |
89 |
Correct |
42 ms |
14084 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
604 KB |
Output is correct |
93 |
Correct |
49 ms |
10248 KB |
Output is correct |
94 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
95 |
Halted |
0 ms |
0 KB |
- |