#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(){
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
if(tar[i][j] == 0){
if(chain.root(i) == chain.root(j))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;
int now = head[i];
vector<int> v;
v.push_back(now);
bool has = false;
for(int j = 0;j<N;j++){
if(done[j])return false;
if(tar[now][j] == 2){
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;
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:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:108:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int j = 1;j<v[i].size();j++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
96 ms |
26152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
96 ms |
26152 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
96 ms |
26160 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 |
59 ms |
16176 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6796 KB |
Output is correct |
21 |
Correct |
100 ms |
26180 KB |
Output is correct |
22 |
Correct |
99 ms |
25940 KB |
Output is correct |
23 |
Correct |
102 ms |
26176 KB |
Output is correct |
24 |
Correct |
96 ms |
26192 KB |
Output is correct |
25 |
Correct |
49 ms |
16208 KB |
Output is correct |
26 |
Correct |
43 ms |
16212 KB |
Output is correct |
27 |
Correct |
103 ms |
26196 KB |
Output is correct |
28 |
Correct |
102 ms |
26000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
408 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 |
600 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
96 ms |
26196 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
97 ms |
26192 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 |
16212 KB |
Output is correct |
18 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
24 ms |
6912 KB |
Output is correct |
5 |
Correct |
97 ms |
25964 KB |
Output is correct |
6 |
Correct |
96 ms |
26196 KB |
Output is correct |
7 |
Correct |
102 ms |
26160 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
96 ms |
26152 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
96 ms |
26160 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 |
59 ms |
16176 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6796 KB |
Output is correct |
21 |
Correct |
100 ms |
26180 KB |
Output is correct |
22 |
Correct |
99 ms |
25940 KB |
Output is correct |
23 |
Correct |
102 ms |
26176 KB |
Output is correct |
24 |
Correct |
96 ms |
26192 KB |
Output is correct |
25 |
Correct |
49 ms |
16208 KB |
Output is correct |
26 |
Correct |
43 ms |
16212 KB |
Output is correct |
27 |
Correct |
103 ms |
26196 KB |
Output is correct |
28 |
Correct |
102 ms |
26000 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
408 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 |
600 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
96 ms |
26196 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
97 ms |
26192 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 |
16212 KB |
Output is correct |
46 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
96 ms |
26152 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
4 ms |
1372 KB |
Output is correct |
13 |
Correct |
96 ms |
26160 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 |
59 ms |
16176 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
6796 KB |
Output is correct |
21 |
Correct |
100 ms |
26180 KB |
Output is correct |
22 |
Correct |
99 ms |
25940 KB |
Output is correct |
23 |
Correct |
102 ms |
26176 KB |
Output is correct |
24 |
Correct |
96 ms |
26192 KB |
Output is correct |
25 |
Correct |
49 ms |
16208 KB |
Output is correct |
26 |
Correct |
43 ms |
16212 KB |
Output is correct |
27 |
Correct |
103 ms |
26196 KB |
Output is correct |
28 |
Correct |
102 ms |
26000 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
408 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 |
600 KB |
Output is correct |
36 |
Correct |
4 ms |
1372 KB |
Output is correct |
37 |
Correct |
96 ms |
26196 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
4 ms |
1372 KB |
Output is correct |
41 |
Correct |
97 ms |
26192 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 |
16212 KB |
Output is correct |
46 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
47 |
Halted |
0 ms |
0 KB |
- |