#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1010;
struct DSU{
int dsu[mxn],sz[mxn];
DSU(){
iota(dsu,dsu+mxn,0);
fill(sz,sz+mxn,1);
}
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void onion(int a,int b){
a = root(a),b = root(b);
if(a == b)return;
if(sz[a]<sz[b])swap(a,b);
dsu[b] = a;
sz[a] += sz[b];
}
};
/*
std::vector<std::vector<int>> answer(n,vector<int>(n,0));
build(answer);
return 1;
*/
DSU chain,cyc;
int N;
vector<vector<int>> ans;
vector<vector<int>> tar;
vector<int> v[mxn];
int head[mxn];
void add_edge(int a,int b){
assert(a != b);
ans[a][b] = ans[b][a] = 1;
}
bool check(){
cerr<<"CHECK!"<<endl;
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
if(i == j)continue;
if(tar[i][j] == 0){
if(chain.root(i) == chain.root(j))return false;
int a = head[chain.root(i)],b = head[chain.root(j)];
if(cyc.root(a) == cyc.root(b))return false;
}
else if(tar[i][j] == 1){
if(chain.root(i) != chain.root(j))return false;
}
else{
if(chain.root(i) == chain.root(j))return false;
int a = head[chain.root(i)],b = head[chain.root(j)];
if(cyc.root(a) != cyc.root(b))return false;
}
}
}
return true;
}
bitset<mxn> done;
bool CYCLE(){
for(int i = 0;i<N;i++){
if(done[i]||head[i] == -1)continue;
cerr<<"CYC: "<<i<<endl;
int now = head[i];
vector<int> v;
v.push_back(now);
bool has = false;
for(int j = 0;j<N;j++){
if(tar[now][j] == 2){
if(done[j])return false;
has = true;
v.push_back(head[chain.root(j)]);
}
}
if(!has)continue;
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
if(v.size()<=2)return false;
cerr<<now<<"::";for(auto &j:v)cerr<<j<<' ';cerr<<endl;
for(int i = 1;i<v.size();i++){
add_edge(v[i-1],v[i]);
cyc.onion(v[i-1],v[i]);
}
add_edge(v[0],v.back());
for(auto &i:v)done[chain.root(i)] = true;
}
return true;
}
int construct(std::vector<std::vector<int>> vvv) {
memset(head,-1,sizeof(head));
tar = vvv;
N = tar.size();
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
if(tar[i][j] == 1)chain.onion(i,j);
}
}
ans = vector<vector<int>>(N,vector<int>(N,0));
for(int i = 0;i<N;i++){
v[chain.root(i)].push_back(i);
}
for(int i = 0;i<N;i++){
for(int j = 1;j<v[i].size();j++){
add_edge(v[i][j-1],v[i][j]);
}
if(!v[i].empty())head[i] = v[i][0];
}
if(!CYCLE())return 0;
if(!check())return 0;
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'bool CYCLE()':
supertrees.cpp:90:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:114:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int j = 1;j<v[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
103 ms |
25968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
103 ms |
25968 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
104 ms |
26180 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
53 ms |
16184 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
25 ms |
6852 KB |
Output is correct |
21 |
Correct |
100 ms |
26196 KB |
Output is correct |
22 |
Correct |
107 ms |
26172 KB |
Output is correct |
23 |
Correct |
110 ms |
26164 KB |
Output is correct |
24 |
Correct |
100 ms |
26192 KB |
Output is correct |
25 |
Correct |
43 ms |
16212 KB |
Output is correct |
26 |
Correct |
44 ms |
16208 KB |
Output is correct |
27 |
Correct |
110 ms |
26192 KB |
Output is correct |
28 |
Correct |
100 ms |
26192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1372 KB |
Output is correct |
9 |
Correct |
101 ms |
26196 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
114 ms |
26180 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 |
1116 KB |
Output is correct |
17 |
Correct |
48 ms |
16196 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
26 ms |
6784 KB |
Output is correct |
22 |
Correct |
100 ms |
26200 KB |
Output is correct |
23 |
Correct |
101 ms |
26196 KB |
Output is correct |
24 |
Correct |
108 ms |
26192 KB |
Output is correct |
25 |
Correct |
44 ms |
16192 KB |
Output is correct |
26 |
Correct |
45 ms |
16212 KB |
Output is correct |
27 |
Correct |
101 ms |
26112 KB |
Output is correct |
28 |
Correct |
107 ms |
25984 KB |
Output is correct |
29 |
Correct |
44 ms |
16192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
6748 KB |
Output is correct |
5 |
Correct |
104 ms |
26148 KB |
Output is correct |
6 |
Correct |
106 ms |
26180 KB |
Output is correct |
7 |
Correct |
106 ms |
26172 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
27 ms |
6836 KB |
Output is correct |
10 |
Correct |
104 ms |
26016 KB |
Output is correct |
11 |
Correct |
101 ms |
26176 KB |
Output is correct |
12 |
Correct |
114 ms |
26080 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
26 ms |
6748 KB |
Output is correct |
17 |
Correct |
101 ms |
26176 KB |
Output is correct |
18 |
Correct |
103 ms |
26176 KB |
Output is correct |
19 |
Correct |
104 ms |
26196 KB |
Output is correct |
20 |
Correct |
101 ms |
26144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
103 ms |
25968 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
104 ms |
26180 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
53 ms |
16184 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
25 ms |
6852 KB |
Output is correct |
21 |
Correct |
100 ms |
26196 KB |
Output is correct |
22 |
Correct |
107 ms |
26172 KB |
Output is correct |
23 |
Correct |
110 ms |
26164 KB |
Output is correct |
24 |
Correct |
100 ms |
26192 KB |
Output is correct |
25 |
Correct |
43 ms |
16212 KB |
Output is correct |
26 |
Correct |
44 ms |
16208 KB |
Output is correct |
27 |
Correct |
110 ms |
26192 KB |
Output is correct |
28 |
Correct |
100 ms |
26192 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
101 ms |
26196 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
114 ms |
26180 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 |
1116 KB |
Output is correct |
45 |
Correct |
48 ms |
16196 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
26 ms |
6784 KB |
Output is correct |
50 |
Correct |
100 ms |
26200 KB |
Output is correct |
51 |
Correct |
101 ms |
26196 KB |
Output is correct |
52 |
Correct |
108 ms |
26192 KB |
Output is correct |
53 |
Correct |
44 ms |
16192 KB |
Output is correct |
54 |
Correct |
45 ms |
16212 KB |
Output is correct |
55 |
Correct |
101 ms |
26112 KB |
Output is correct |
56 |
Correct |
107 ms |
25984 KB |
Output is correct |
57 |
Correct |
44 ms |
16192 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
4 ms |
1116 KB |
Output is correct |
61 |
Correct |
57 ms |
16144 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
26 ms |
6768 KB |
Output is correct |
66 |
Correct |
45 ms |
16216 KB |
Output is correct |
67 |
Correct |
45 ms |
16208 KB |
Output is correct |
68 |
Correct |
43 ms |
16212 KB |
Output is correct |
69 |
Correct |
45 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
103 ms |
25968 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
104 ms |
26180 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
53 ms |
16184 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
25 ms |
6852 KB |
Output is correct |
21 |
Correct |
100 ms |
26196 KB |
Output is correct |
22 |
Correct |
107 ms |
26172 KB |
Output is correct |
23 |
Correct |
110 ms |
26164 KB |
Output is correct |
24 |
Correct |
100 ms |
26192 KB |
Output is correct |
25 |
Correct |
43 ms |
16212 KB |
Output is correct |
26 |
Correct |
44 ms |
16208 KB |
Output is correct |
27 |
Correct |
110 ms |
26192 KB |
Output is correct |
28 |
Correct |
100 ms |
26192 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
101 ms |
26196 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
114 ms |
26180 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 |
1116 KB |
Output is correct |
45 |
Correct |
48 ms |
16196 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
26 ms |
6784 KB |
Output is correct |
50 |
Correct |
100 ms |
26200 KB |
Output is correct |
51 |
Correct |
101 ms |
26196 KB |
Output is correct |
52 |
Correct |
108 ms |
26192 KB |
Output is correct |
53 |
Correct |
44 ms |
16192 KB |
Output is correct |
54 |
Correct |
45 ms |
16212 KB |
Output is correct |
55 |
Correct |
101 ms |
26112 KB |
Output is correct |
56 |
Correct |
107 ms |
25984 KB |
Output is correct |
57 |
Correct |
44 ms |
16192 KB |
Output is correct |
58 |
Correct |
0 ms |
348 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 |
6748 KB |
Output is correct |
62 |
Correct |
104 ms |
26148 KB |
Output is correct |
63 |
Correct |
106 ms |
26180 KB |
Output is correct |
64 |
Correct |
106 ms |
26172 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
27 ms |
6836 KB |
Output is correct |
67 |
Correct |
104 ms |
26016 KB |
Output is correct |
68 |
Correct |
101 ms |
26176 KB |
Output is correct |
69 |
Correct |
114 ms |
26080 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
26 ms |
6748 KB |
Output is correct |
74 |
Correct |
101 ms |
26176 KB |
Output is correct |
75 |
Correct |
103 ms |
26176 KB |
Output is correct |
76 |
Correct |
104 ms |
26196 KB |
Output is correct |
77 |
Correct |
101 ms |
26144 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
4 ms |
1116 KB |
Output is correct |
81 |
Correct |
57 ms |
16144 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
26 ms |
6768 KB |
Output is correct |
86 |
Correct |
45 ms |
16216 KB |
Output is correct |
87 |
Correct |
45 ms |
16208 KB |
Output is correct |
88 |
Correct |
43 ms |
16212 KB |
Output is correct |
89 |
Correct |
45 ms |
16212 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
1116 KB |
Output is correct |
93 |
Correct |
47 ms |
16192 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
12 ms |
4444 KB |
Output is correct |
98 |
Correct |
46 ms |
16200 KB |
Output is correct |
99 |
Correct |
45 ms |
16188 KB |
Output is correct |
100 |
Correct |
44 ms |
16212 KB |
Output is correct |
101 |
Correct |
47 ms |
16208 KB |
Output is correct |
102 |
Correct |
48 ms |
16212 KB |
Output is correct |
103 |
Correct |
44 ms |
16208 KB |
Output is correct |
104 |
Correct |
60 ms |
16180 KB |
Output is correct |
105 |
Correct |
57 ms |
16200 KB |
Output is correct |