#include "supertrees.h"
#include<bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix= vector<vector<T>>;
struct UnionFind{
vector<int> pai;
matrix<int> group;
int n;
UnionFind(int sz = 0){
n = sz;
pai = vector<int>(n);
iota(all(pai),0);
group = matrix<int>(n);
for(int i = 0; i < n; i++)
group[i].push_back(i);
}
int find(int id){
if(pai[id] == id)
return id;
return pai[id] = find(pai[id]);
}
void onion(int a, int b){
a = find(a);
b = find(b);
if(a == b)
return;
if(group[a].size() > group[b].size())
swap(a,b);
for(int i : group[a])
group[b].push_back(i);
group[a].clear();
pai[a] = b;
}
};
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
matrix<int> resp(n,vector<int>(n));
UnionFind connected(n);
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
if(p[i][j])
connected.onion(i,j);
if(p[i][j] == 3)
return 0;
}
}
auto solveforgroup =[&](vector<int>& g){
vector<int> incall(n);
for(int i : g)
incall[i] = 1;
int line = 1;
for(int i : g){
for(int j : g){
if(!p[i][j])
return 0;
line&=p[i][j];
}
}
if(line){
for(int i = 1; i < g.size(); i++){
resp[g[i-1]][g[i]] = 1;
resp[g[i]][g[i-1]] = 1;
}
return 1;
}
if(g.size() <= 2){
return 0;
}
vector<int> count1(n);
for(int i : g){
for(int j : g){
count1[i]+=p[i][j] == 1;
}
}
vector<int> cycle, incycle(n);
for(int i : g){
if(!incycle[i] && count1[i] == 1)
cycle.push_back(i), incycle[i] = 1;
for(int j : g){
if(p[i][j] == 1 && !incycle[j] && count1[i] < count1[j]){
cycle.push_back(j);
incycle[j] = 1;
break;
}
}
}
UnionFind lines(n);
for(int i : g){
if(incycle[i])
continue;
for(int j : g){
if(p[i][j] == 1 && !incycle[j]){
lines.onion(i,j);
}
}
}
for(vector<int> & line : lines.group){
if(line.empty() || !incall[line[0]] || incycle[line[0]]){
continue;
}
for(int i : line){
for(int j : line){
if(p[i][j]!=1)
return 0;
}
}
for(int j = 1; j < line.size(); j++){
resp[line[j-1]][line[j]] = 1;
resp[line[j]][line[j-1]] = 1;
}
int root= -1, rootct =0;
for(int k : cycle){
int allright = 1, someright= 0;
for(int i : line){
allright&=p[k][i];
someright|=p[k][i]==1;
}
if(allright!=someright)
return 0;
if(allright){
root = k;
rootct++;
}
}
if(rootct > 1)
return 0;
if(rootct == 1){
resp[line[0]][root] = 1;
resp[root][line[0]] = 1;
}
if(rootct == 0){
cycle.push_back(line[0]);
incycle[line[0]] = 1;
}
}
if(cycle.size() <= 1)
return 0;
for(int i : cycle){
for(int j : cycle){
if(i!=j && p[i][j]!=2)
return 0;
}
}
for(int i = 1; i < cycle.size(); i++){
resp[cycle[i]][cycle[i-1]] = 1;
resp[cycle[i-1]][cycle[i]] = 1;
}
resp[cycle[0]][cycle.back()] = 1;
resp[cycle.back()][cycle[0]] = 1;
return 1;
};
bool ok = 1;
for(vector<int>& i : connected.group){
if(i.size())
ok&=solveforgroup(i);
}
if(!ok)
return 0;
build(resp);
return 1;
}
Compilation message
supertrees.cpp: In lambda function:
supertrees.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 1; i < g.size(); i++){
| ~~^~~~~~~~~~
supertrees.cpp:124:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int j = 1; j < line.size(); j++){
| ~~^~~~~~~~~~~~~
supertrees.cpp:163:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | for(int i = 1; i < cycle.size(); i++){
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
9 ms |
1208 KB |
Output is correct |
7 |
Correct |
162 ms |
24064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
9 ms |
1208 KB |
Output is correct |
7 |
Correct |
162 ms |
24064 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
11 ms |
1208 KB |
Output is correct |
13 |
Correct |
240 ms |
24012 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
93 ms |
14068 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
53 ms |
6212 KB |
Output is correct |
21 |
Correct |
172 ms |
24056 KB |
Output is correct |
22 |
Correct |
178 ms |
24052 KB |
Output is correct |
23 |
Correct |
188 ms |
23996 KB |
Output is correct |
24 |
Correct |
171 ms |
24064 KB |
Output is correct |
25 |
Correct |
71 ms |
14068 KB |
Output is correct |
26 |
Correct |
87 ms |
14080 KB |
Output is correct |
27 |
Correct |
178 ms |
24056 KB |
Output is correct |
28 |
Correct |
169 ms |
24052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
167 ms |
23972 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
12 ms |
1236 KB |
Output is correct |
13 |
Correct |
217 ms |
24048 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
84 ms |
14068 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
47 ms |
6168 KB |
Output is correct |
22 |
Correct |
170 ms |
24052 KB |
Output is correct |
23 |
Correct |
181 ms |
24048 KB |
Output is correct |
24 |
Correct |
194 ms |
23948 KB |
Output is correct |
25 |
Correct |
69 ms |
14156 KB |
Output is correct |
26 |
Correct |
76 ms |
14192 KB |
Output is correct |
27 |
Correct |
172 ms |
24040 KB |
Output is correct |
28 |
Correct |
198 ms |
23976 KB |
Output is correct |
29 |
Correct |
74 ms |
14284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
58 ms |
6236 KB |
Output is correct |
5 |
Correct |
171 ms |
24028 KB |
Output is correct |
6 |
Correct |
164 ms |
23948 KB |
Output is correct |
7 |
Correct |
179 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
42 ms |
6180 KB |
Output is correct |
10 |
Correct |
170 ms |
24148 KB |
Output is correct |
11 |
Correct |
183 ms |
24028 KB |
Output is correct |
12 |
Correct |
176 ms |
24048 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
48 ms |
6256 KB |
Output is correct |
17 |
Correct |
167 ms |
24048 KB |
Output is correct |
18 |
Correct |
162 ms |
24052 KB |
Output is correct |
19 |
Correct |
215 ms |
24012 KB |
Output is correct |
20 |
Correct |
163 ms |
24032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
9 ms |
1208 KB |
Output is correct |
7 |
Correct |
162 ms |
24064 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
11 ms |
1208 KB |
Output is correct |
13 |
Correct |
240 ms |
24012 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
93 ms |
14068 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
53 ms |
6212 KB |
Output is correct |
21 |
Correct |
172 ms |
24056 KB |
Output is correct |
22 |
Correct |
178 ms |
24052 KB |
Output is correct |
23 |
Correct |
188 ms |
23996 KB |
Output is correct |
24 |
Correct |
171 ms |
24064 KB |
Output is correct |
25 |
Correct |
71 ms |
14068 KB |
Output is correct |
26 |
Correct |
87 ms |
14080 KB |
Output is correct |
27 |
Correct |
178 ms |
24056 KB |
Output is correct |
28 |
Correct |
169 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
167 ms |
23972 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
12 ms |
1236 KB |
Output is correct |
41 |
Correct |
217 ms |
24048 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
84 ms |
14068 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
296 KB |
Output is correct |
49 |
Correct |
47 ms |
6168 KB |
Output is correct |
50 |
Correct |
170 ms |
24052 KB |
Output is correct |
51 |
Correct |
181 ms |
24048 KB |
Output is correct |
52 |
Correct |
194 ms |
23948 KB |
Output is correct |
53 |
Correct |
69 ms |
14156 KB |
Output is correct |
54 |
Correct |
76 ms |
14192 KB |
Output is correct |
55 |
Correct |
172 ms |
24040 KB |
Output is correct |
56 |
Correct |
198 ms |
23976 KB |
Output is correct |
57 |
Correct |
74 ms |
14284 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
111 ms |
14076 KB |
Output is correct |
62 |
Correct |
1 ms |
300 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
42 ms |
6220 KB |
Output is correct |
66 |
Correct |
72 ms |
14148 KB |
Output is correct |
67 |
Correct |
75 ms |
14208 KB |
Output is correct |
68 |
Correct |
93 ms |
14288 KB |
Output is correct |
69 |
Correct |
75 ms |
14156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
9 ms |
1208 KB |
Output is correct |
7 |
Correct |
162 ms |
24064 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
11 ms |
1208 KB |
Output is correct |
13 |
Correct |
240 ms |
24012 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
93 ms |
14068 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
53 ms |
6212 KB |
Output is correct |
21 |
Correct |
172 ms |
24056 KB |
Output is correct |
22 |
Correct |
178 ms |
24052 KB |
Output is correct |
23 |
Correct |
188 ms |
23996 KB |
Output is correct |
24 |
Correct |
171 ms |
24064 KB |
Output is correct |
25 |
Correct |
71 ms |
14068 KB |
Output is correct |
26 |
Correct |
87 ms |
14080 KB |
Output is correct |
27 |
Correct |
178 ms |
24056 KB |
Output is correct |
28 |
Correct |
169 ms |
24052 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
167 ms |
23972 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
12 ms |
1236 KB |
Output is correct |
41 |
Correct |
217 ms |
24048 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
84 ms |
14068 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
296 KB |
Output is correct |
49 |
Correct |
47 ms |
6168 KB |
Output is correct |
50 |
Correct |
170 ms |
24052 KB |
Output is correct |
51 |
Correct |
181 ms |
24048 KB |
Output is correct |
52 |
Correct |
194 ms |
23948 KB |
Output is correct |
53 |
Correct |
69 ms |
14156 KB |
Output is correct |
54 |
Correct |
76 ms |
14192 KB |
Output is correct |
55 |
Correct |
172 ms |
24040 KB |
Output is correct |
56 |
Correct |
198 ms |
23976 KB |
Output is correct |
57 |
Correct |
74 ms |
14284 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
58 ms |
6236 KB |
Output is correct |
62 |
Correct |
171 ms |
24028 KB |
Output is correct |
63 |
Correct |
164 ms |
23948 KB |
Output is correct |
64 |
Correct |
179 ms |
24012 KB |
Output is correct |
65 |
Correct |
1 ms |
296 KB |
Output is correct |
66 |
Correct |
42 ms |
6180 KB |
Output is correct |
67 |
Correct |
170 ms |
24148 KB |
Output is correct |
68 |
Correct |
183 ms |
24028 KB |
Output is correct |
69 |
Correct |
176 ms |
24048 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
48 ms |
6256 KB |
Output is correct |
74 |
Correct |
167 ms |
24048 KB |
Output is correct |
75 |
Correct |
162 ms |
24052 KB |
Output is correct |
76 |
Correct |
215 ms |
24012 KB |
Output is correct |
77 |
Correct |
163 ms |
24032 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
111 ms |
14076 KB |
Output is correct |
82 |
Correct |
1 ms |
300 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
42 ms |
6220 KB |
Output is correct |
86 |
Correct |
72 ms |
14148 KB |
Output is correct |
87 |
Correct |
75 ms |
14208 KB |
Output is correct |
88 |
Correct |
93 ms |
14288 KB |
Output is correct |
89 |
Correct |
75 ms |
14156 KB |
Output is correct |
90 |
Correct |
1 ms |
304 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
852 KB |
Output is correct |
93 |
Correct |
70 ms |
14072 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
26 ms |
3684 KB |
Output is correct |
98 |
Correct |
69 ms |
14064 KB |
Output is correct |
99 |
Correct |
74 ms |
14084 KB |
Output is correct |
100 |
Correct |
68 ms |
14064 KB |
Output is correct |
101 |
Correct |
70 ms |
14068 KB |
Output is correct |
102 |
Correct |
64 ms |
14068 KB |
Output is correct |
103 |
Correct |
63 ms |
14028 KB |
Output is correct |
104 |
Correct |
64 ms |
14028 KB |
Output is correct |
105 |
Correct |
79 ms |
14084 KB |
Output is correct |