#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU{
int n;
vector<int> parent,sz;
DSU(int _n){
n = _n;
for(int i=0;i<=n;i++){
parent.push_back(i);
sz.push_back(1);
}
}
int find(int x){
return parent[x] = (parent[x] == x ? x : find(parent[x]));
}
void unite(int a,int b){
a = find(a);
b = find(b);
if(a != b){
if(sz[a] >= sz[b]){
parent[b] = a;
sz[a] += sz[b];
}else{
parent[a] = b;
sz[b] += sz[a];
}
}
}
};
/*
int construct_1(vector<vector<int>> p) {
// 21
int n = p.size();
vector<vector<int>> answer(n,vector<int>(n,0));
DSU comp(n);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] >= 2)return 0;
if(p[i][j] == 1)comp.unite(i,j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 0 && comp.find(i) == comp.find(j))return 0;
}
}
vector<bool> vis(n,0);
for(int i=0;i<n;i++){
if(vis[i])continue;
vis[i] = 1;
for(int j=0;j<n;j++){
if(vis[j])continue;
if(comp.find(i) == comp.find(j)){
answer[i][j] = answer[j][i] = 1;
vis[j] = 1;
}
}
}
build(answer);
return 1;
}
int construct_2(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> answer(n,vector<int>(n,0));
DSU comp(n);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 3 || p[i][j] == 1)return 0;
if(p[i][j] == 2)comp.unite(i,j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 0 && comp.find(i) == comp.find(j))return 0;
}
}
vector<bool> vis(n,0);
map<int,vector<int>> comps;
for(int i=0;i<n;i++){
comps[comp.find(i)].push_back(i);
}
for(auto i:comps){
vector<int> &p = i.second;
if((int)p.size() == 2)return 0;
if((int)p.size() < 2)continue;
for(int i=1;i<(int)p.size();i++){
answer[p[i]][p[i-1]] = answer[p[i-1]][p[i]] = 1;
}
answer[p[(int)p.size()-1]][p[0]] = answer[p[0]][p[(int)p.size()-1]] = 1;
}
build(answer);
return 1;
}*/
int construct_3(vector<vector<int>> p) {
//answer exists
int n = p.size();
vector<vector<int>> answer(n,vector<int>(n,0));
DSU comp(n);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 3)return 0;
if(p[i][j] == 1)comp.unite(i,j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 0 && comp.find(i) == comp.find(j))return 0;
}
}
vector<bool> vis(n,0);
vector<bool> is_root(n,0);
for(int i=0;i<n;i++){
if(vis[i])continue;
vis[i] = 1;
is_root[i] = 1;
for(int j=0;j<n;j++){
if(vis[j])continue;
if(comp.find(i) == comp.find(j)){
answer[i][j] = answer[j][i] = 1;
vis[j] = 1;
}
}
}
DSU nxt(n);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 2)nxt.unite(i,j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 0 && nxt.find(i) == nxt.find(j))return 0;
}
}
map<int,vector<int>> comps;
for(int i=0;i<n;i++){
if(is_root[i])comps[nxt.find(i)].push_back(i);
}
for(auto i:comps){
vector<int> &p = i.second;
if((int)p.size() == 2)return 0;
if((int)p.size() < 2)continue;
for(int i=1;i<(int)p.size();i++){
answer[p[i]][p[i-1]] = answer[p[i-1]][p[i]] = 1;
}
answer[p[(int)p.size()-1]][p[0]] = answer[p[0]][p[(int)p.size()-1]] = 1;
}
build(answer);
return 1;
}
int construct(vector<vector<int>> p) {
/*
int n = p.size();
bool one = 1,two = 1;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(p[i][j] == 2)one = 0;
if(p[i][j] == 1)two = 0;
}
}
if(one)return construct_1(p);
else if(two)return construct_2(p);*/
return construct_3(p);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
228 ms |
26052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
228 ms |
26052 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1348 KB |
Output is correct |
13 |
Correct |
241 ms |
26248 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
832 KB |
Output is correct |
17 |
Correct |
102 ms |
16008 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
59 ms |
6700 KB |
Output is correct |
21 |
Correct |
258 ms |
25996 KB |
Output is correct |
22 |
Correct |
266 ms |
26052 KB |
Output is correct |
23 |
Correct |
241 ms |
26036 KB |
Output is correct |
24 |
Correct |
228 ms |
26120 KB |
Output is correct |
25 |
Correct |
94 ms |
15996 KB |
Output is correct |
26 |
Correct |
95 ms |
16004 KB |
Output is correct |
27 |
Correct |
268 ms |
25996 KB |
Output is correct |
28 |
Correct |
225 ms |
26052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
10 ms |
1352 KB |
Output is correct |
9 |
Correct |
226 ms |
26116 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1356 KB |
Output is correct |
13 |
Correct |
238 ms |
25996 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
112 ms |
16008 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
58 ms |
6776 KB |
Output is correct |
22 |
Correct |
234 ms |
25992 KB |
Output is correct |
23 |
Correct |
235 ms |
25924 KB |
Output is correct |
24 |
Correct |
242 ms |
26104 KB |
Output is correct |
25 |
Correct |
98 ms |
16196 KB |
Output is correct |
26 |
Correct |
99 ms |
16000 KB |
Output is correct |
27 |
Correct |
236 ms |
26000 KB |
Output is correct |
28 |
Correct |
242 ms |
25924 KB |
Output is correct |
29 |
Correct |
106 ms |
16132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
59 ms |
6652 KB |
Output is correct |
5 |
Correct |
242 ms |
26000 KB |
Output is correct |
6 |
Correct |
228 ms |
25996 KB |
Output is correct |
7 |
Correct |
281 ms |
25988 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
59 ms |
6708 KB |
Output is correct |
10 |
Correct |
238 ms |
25976 KB |
Output is correct |
11 |
Correct |
230 ms |
26036 KB |
Output is correct |
12 |
Correct |
255 ms |
25960 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
60 ms |
6696 KB |
Output is correct |
17 |
Correct |
237 ms |
25920 KB |
Output is correct |
18 |
Correct |
236 ms |
25976 KB |
Output is correct |
19 |
Correct |
233 ms |
25968 KB |
Output is correct |
20 |
Correct |
234 ms |
26028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
228 ms |
26052 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1348 KB |
Output is correct |
13 |
Correct |
241 ms |
26248 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
832 KB |
Output is correct |
17 |
Correct |
102 ms |
16008 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
59 ms |
6700 KB |
Output is correct |
21 |
Correct |
258 ms |
25996 KB |
Output is correct |
22 |
Correct |
266 ms |
26052 KB |
Output is correct |
23 |
Correct |
241 ms |
26036 KB |
Output is correct |
24 |
Correct |
228 ms |
26120 KB |
Output is correct |
25 |
Correct |
94 ms |
15996 KB |
Output is correct |
26 |
Correct |
95 ms |
16004 KB |
Output is correct |
27 |
Correct |
268 ms |
25996 KB |
Output is correct |
28 |
Correct |
225 ms |
26052 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1352 KB |
Output is correct |
37 |
Correct |
226 ms |
26116 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1356 KB |
Output is correct |
41 |
Correct |
238 ms |
25996 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
112 ms |
16008 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
58 ms |
6776 KB |
Output is correct |
50 |
Correct |
234 ms |
25992 KB |
Output is correct |
51 |
Correct |
235 ms |
25924 KB |
Output is correct |
52 |
Correct |
242 ms |
26104 KB |
Output is correct |
53 |
Correct |
98 ms |
16196 KB |
Output is correct |
54 |
Correct |
99 ms |
16000 KB |
Output is correct |
55 |
Correct |
236 ms |
26000 KB |
Output is correct |
56 |
Correct |
242 ms |
25924 KB |
Output is correct |
57 |
Correct |
106 ms |
16132 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
844 KB |
Output is correct |
61 |
Correct |
101 ms |
16024 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
288 KB |
Output is correct |
65 |
Correct |
76 ms |
7168 KB |
Output is correct |
66 |
Correct |
106 ms |
17120 KB |
Output is correct |
67 |
Correct |
93 ms |
17104 KB |
Output is correct |
68 |
Correct |
100 ms |
17116 KB |
Output is correct |
69 |
Correct |
102 ms |
17160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1356 KB |
Output is correct |
7 |
Correct |
228 ms |
26052 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1348 KB |
Output is correct |
13 |
Correct |
241 ms |
26248 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
832 KB |
Output is correct |
17 |
Correct |
102 ms |
16008 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
59 ms |
6700 KB |
Output is correct |
21 |
Correct |
258 ms |
25996 KB |
Output is correct |
22 |
Correct |
266 ms |
26052 KB |
Output is correct |
23 |
Correct |
241 ms |
26036 KB |
Output is correct |
24 |
Correct |
228 ms |
26120 KB |
Output is correct |
25 |
Correct |
94 ms |
15996 KB |
Output is correct |
26 |
Correct |
95 ms |
16004 KB |
Output is correct |
27 |
Correct |
268 ms |
25996 KB |
Output is correct |
28 |
Correct |
225 ms |
26052 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
10 ms |
1352 KB |
Output is correct |
37 |
Correct |
226 ms |
26116 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1356 KB |
Output is correct |
41 |
Correct |
238 ms |
25996 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
112 ms |
16008 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
58 ms |
6776 KB |
Output is correct |
50 |
Correct |
234 ms |
25992 KB |
Output is correct |
51 |
Correct |
235 ms |
25924 KB |
Output is correct |
52 |
Correct |
242 ms |
26104 KB |
Output is correct |
53 |
Correct |
98 ms |
16196 KB |
Output is correct |
54 |
Correct |
99 ms |
16000 KB |
Output is correct |
55 |
Correct |
236 ms |
26000 KB |
Output is correct |
56 |
Correct |
242 ms |
25924 KB |
Output is correct |
57 |
Correct |
106 ms |
16132 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
59 ms |
6652 KB |
Output is correct |
62 |
Correct |
242 ms |
26000 KB |
Output is correct |
63 |
Correct |
228 ms |
25996 KB |
Output is correct |
64 |
Correct |
281 ms |
25988 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
59 ms |
6708 KB |
Output is correct |
67 |
Correct |
238 ms |
25976 KB |
Output is correct |
68 |
Correct |
230 ms |
26036 KB |
Output is correct |
69 |
Correct |
255 ms |
25960 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
60 ms |
6696 KB |
Output is correct |
74 |
Correct |
237 ms |
25920 KB |
Output is correct |
75 |
Correct |
236 ms |
25976 KB |
Output is correct |
76 |
Correct |
233 ms |
25968 KB |
Output is correct |
77 |
Correct |
234 ms |
26028 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
5 ms |
844 KB |
Output is correct |
81 |
Correct |
101 ms |
16024 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
288 KB |
Output is correct |
85 |
Correct |
76 ms |
7168 KB |
Output is correct |
86 |
Correct |
106 ms |
17120 KB |
Output is correct |
87 |
Correct |
93 ms |
17104 KB |
Output is correct |
88 |
Correct |
100 ms |
17116 KB |
Output is correct |
89 |
Correct |
102 ms |
17160 KB |
Output is correct |
90 |
Correct |
1 ms |
288 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
5 ms |
892 KB |
Output is correct |
93 |
Correct |
112 ms |
17096 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
1 ms |
204 KB |
Output is correct |
97 |
Correct |
25 ms |
4712 KB |
Output is correct |
98 |
Correct |
98 ms |
17464 KB |
Output is correct |
99 |
Correct |
97 ms |
17476 KB |
Output is correct |
100 |
Correct |
92 ms |
17396 KB |
Output is correct |
101 |
Correct |
93 ms |
17412 KB |
Output is correct |
102 |
Correct |
93 ms |
17476 KB |
Output is correct |
103 |
Correct |
91 ms |
17472 KB |
Output is correct |
104 |
Correct |
91 ms |
17412 KB |
Output is correct |
105 |
Correct |
99 ms |
17476 KB |
Output is correct |