#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define FOR(i, x, y) for (int i = x; i < y; i++)
int construct(vector<vector<int>> p){
int n = p.size();
FOR(i, 0, n) FOR(j, 0, n) if (p[i][j] == 3) return 0;
vector<vector<int>> ans(n, vector<int>(n, 0));
bool vis[n] = {};
int par[n]; iota(par, par + n, 0);
function<int(int)> get = [&](int i){
return i == par[i] ? i : par[i] = get(par[i]);
};
auto merge = [&](int a, int b){
par[get(a)] = get(b);
};
FOR(src, 0, n) if (!vis[src]){
bool incmp[n] = {}; vector<int> cmp;
FOR(i, 0, n) if (p[src][i])
cmp.push_back(i), incmp[i] = 1;
for (int i : cmp) for (int j : cmp)
if (p[i][j] == 1) merge(i, j);
for (int i : cmp){ //check
if (vis[i]) return 0;
FOR(j, 0, n)
if (p[i][j] and !incmp[j]) return 0;
for (int j : cmp)
if (!((get(i) == get(j) and p[i][j] == 1) or (get(i) != get(j) and p[i][j] == 2)))
return 0;
}
for (int i : cmp) vis[i] = 1;
bool use[n] = {}; vector<int> head;
for (int i : cmp){
int c = get(i);
if (!use[c]) head.push_back(c), use[c] = 1;
if (i != c) ans[i][c] = ans[c][i] = 1;
}
FOR(i, 0, head.size()){
int a = head[i];
int b = head[(i + 1) % head.size()];
if (a != b) ans[a][b] = ans[b][a] = 1;
}
if (head.size() == 2) return 0; //cannot have cycle of length 2
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define FOR(i, x, y) for (int i = x; i < y; i++)
......
45 | FOR(i, 0, head.size()){
| ~~~~~~~~~~~~~~~~~
supertrees.cpp:45:9: note: in expansion of macro 'FOR'
45 | FOR(i, 0, head.size()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
11 ms |
1108 KB |
Output is correct |
7 |
Correct |
230 ms |
22072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
11 ms |
1108 KB |
Output is correct |
7 |
Correct |
230 ms |
22072 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
191 ms |
21968 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
94 ms |
12168 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
47 ms |
5772 KB |
Output is correct |
21 |
Correct |
180 ms |
22032 KB |
Output is correct |
22 |
Correct |
160 ms |
22020 KB |
Output is correct |
23 |
Correct |
199 ms |
22076 KB |
Output is correct |
24 |
Correct |
170 ms |
22020 KB |
Output is correct |
25 |
Correct |
70 ms |
12048 KB |
Output is correct |
26 |
Correct |
66 ms |
12108 KB |
Output is correct |
27 |
Correct |
214 ms |
22032 KB |
Output is correct |
28 |
Correct |
172 ms |
22052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
8 ms |
1212 KB |
Output is correct |
9 |
Correct |
170 ms |
24104 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
8 ms |
1204 KB |
Output is correct |
13 |
Correct |
180 ms |
24012 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
79 ms |
14064 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
0 ms |
300 KB |
Output is correct |
21 |
Correct |
42 ms |
6136 KB |
Output is correct |
22 |
Correct |
171 ms |
23904 KB |
Output is correct |
23 |
Correct |
163 ms |
24012 KB |
Output is correct |
24 |
Correct |
185 ms |
23920 KB |
Output is correct |
25 |
Correct |
67 ms |
14072 KB |
Output is correct |
26 |
Correct |
71 ms |
14080 KB |
Output is correct |
27 |
Correct |
160 ms |
23932 KB |
Output is correct |
28 |
Correct |
194 ms |
23924 KB |
Output is correct |
29 |
Correct |
67 ms |
14072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
42 ms |
5764 KB |
Output is correct |
5 |
Correct |
180 ms |
22024 KB |
Output is correct |
6 |
Correct |
169 ms |
22024 KB |
Output is correct |
7 |
Correct |
210 ms |
22092 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
42 ms |
5648 KB |
Output is correct |
10 |
Correct |
163 ms |
22036 KB |
Output is correct |
11 |
Correct |
166 ms |
22020 KB |
Output is correct |
12 |
Correct |
178 ms |
22024 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
41 ms |
5708 KB |
Output is correct |
17 |
Correct |
165 ms |
21992 KB |
Output is correct |
18 |
Correct |
166 ms |
22056 KB |
Output is correct |
19 |
Correct |
161 ms |
21968 KB |
Output is correct |
20 |
Correct |
162 ms |
22092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
11 ms |
1108 KB |
Output is correct |
7 |
Correct |
230 ms |
22072 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
191 ms |
21968 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
94 ms |
12168 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
47 ms |
5772 KB |
Output is correct |
21 |
Correct |
180 ms |
22032 KB |
Output is correct |
22 |
Correct |
160 ms |
22020 KB |
Output is correct |
23 |
Correct |
199 ms |
22076 KB |
Output is correct |
24 |
Correct |
170 ms |
22020 KB |
Output is correct |
25 |
Correct |
70 ms |
12048 KB |
Output is correct |
26 |
Correct |
66 ms |
12108 KB |
Output is correct |
27 |
Correct |
214 ms |
22032 KB |
Output is correct |
28 |
Correct |
172 ms |
22052 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
8 ms |
1212 KB |
Output is correct |
37 |
Correct |
170 ms |
24104 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
296 KB |
Output is correct |
40 |
Correct |
8 ms |
1204 KB |
Output is correct |
41 |
Correct |
180 ms |
24012 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
79 ms |
14064 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
296 KB |
Output is correct |
48 |
Correct |
0 ms |
300 KB |
Output is correct |
49 |
Correct |
42 ms |
6136 KB |
Output is correct |
50 |
Correct |
171 ms |
23904 KB |
Output is correct |
51 |
Correct |
163 ms |
24012 KB |
Output is correct |
52 |
Correct |
185 ms |
23920 KB |
Output is correct |
53 |
Correct |
67 ms |
14072 KB |
Output is correct |
54 |
Correct |
71 ms |
14080 KB |
Output is correct |
55 |
Correct |
160 ms |
23932 KB |
Output is correct |
56 |
Correct |
194 ms |
23924 KB |
Output is correct |
57 |
Correct |
67 ms |
14072 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
824 KB |
Output is correct |
61 |
Correct |
81 ms |
14072 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
43 ms |
6264 KB |
Output is correct |
66 |
Correct |
70 ms |
14028 KB |
Output is correct |
67 |
Correct |
79 ms |
14068 KB |
Output is correct |
68 |
Correct |
66 ms |
14072 KB |
Output is correct |
69 |
Correct |
66 ms |
14084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
11 ms |
1108 KB |
Output is correct |
7 |
Correct |
230 ms |
22072 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
191 ms |
21968 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
94 ms |
12168 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
47 ms |
5772 KB |
Output is correct |
21 |
Correct |
180 ms |
22032 KB |
Output is correct |
22 |
Correct |
160 ms |
22020 KB |
Output is correct |
23 |
Correct |
199 ms |
22076 KB |
Output is correct |
24 |
Correct |
170 ms |
22020 KB |
Output is correct |
25 |
Correct |
70 ms |
12048 KB |
Output is correct |
26 |
Correct |
66 ms |
12108 KB |
Output is correct |
27 |
Correct |
214 ms |
22032 KB |
Output is correct |
28 |
Correct |
172 ms |
22052 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
8 ms |
1212 KB |
Output is correct |
37 |
Correct |
170 ms |
24104 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
296 KB |
Output is correct |
40 |
Correct |
8 ms |
1204 KB |
Output is correct |
41 |
Correct |
180 ms |
24012 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
79 ms |
14064 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
296 KB |
Output is correct |
48 |
Correct |
0 ms |
300 KB |
Output is correct |
49 |
Correct |
42 ms |
6136 KB |
Output is correct |
50 |
Correct |
171 ms |
23904 KB |
Output is correct |
51 |
Correct |
163 ms |
24012 KB |
Output is correct |
52 |
Correct |
185 ms |
23920 KB |
Output is correct |
53 |
Correct |
67 ms |
14072 KB |
Output is correct |
54 |
Correct |
71 ms |
14080 KB |
Output is correct |
55 |
Correct |
160 ms |
23932 KB |
Output is correct |
56 |
Correct |
194 ms |
23924 KB |
Output is correct |
57 |
Correct |
67 ms |
14072 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
42 ms |
5764 KB |
Output is correct |
62 |
Correct |
180 ms |
22024 KB |
Output is correct |
63 |
Correct |
169 ms |
22024 KB |
Output is correct |
64 |
Correct |
210 ms |
22092 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
42 ms |
5648 KB |
Output is correct |
67 |
Correct |
163 ms |
22036 KB |
Output is correct |
68 |
Correct |
166 ms |
22020 KB |
Output is correct |
69 |
Correct |
178 ms |
22024 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
41 ms |
5708 KB |
Output is correct |
74 |
Correct |
165 ms |
21992 KB |
Output is correct |
75 |
Correct |
166 ms |
22056 KB |
Output is correct |
76 |
Correct |
161 ms |
21968 KB |
Output is correct |
77 |
Correct |
162 ms |
22092 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
824 KB |
Output is correct |
81 |
Correct |
81 ms |
14072 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
43 ms |
6264 KB |
Output is correct |
86 |
Correct |
70 ms |
14028 KB |
Output is correct |
87 |
Correct |
79 ms |
14068 KB |
Output is correct |
88 |
Correct |
66 ms |
14072 KB |
Output is correct |
89 |
Correct |
66 ms |
14084 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
4 ms |
692 KB |
Output is correct |
93 |
Correct |
69 ms |
10100 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
19 ms |
2772 KB |
Output is correct |
98 |
Correct |
67 ms |
10060 KB |
Output is correct |
99 |
Correct |
66 ms |
10104 KB |
Output is correct |
100 |
Correct |
65 ms |
10060 KB |
Output is correct |
101 |
Correct |
63 ms |
10032 KB |
Output is correct |
102 |
Correct |
65 ms |
10116 KB |
Output is correct |
103 |
Correct |
66 ms |
10108 KB |
Output is correct |
104 |
Correct |
65 ms |
10100 KB |
Output is correct |
105 |
Correct |
71 ms |
10044 KB |
Output is correct |