#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
#define sz(x) (int)(x).size()
#define pb push_back
struct DSU{
vi e;
void init(int n){
e = vi(n, -1);
}
int get(int a){
if(e[a] < 0) return a;
e[a] = get(e[a]);
return e[a];
}
bool unite(int a, int b){
a = get(a);
b = get(b);
if(a == b) return 0;
if(-e[a] < -e[b]) swap(a, b);
e[a]+=e[b];
e[b] = a;
return 1;
}
};
const int mx = 1005;
int n;
vector<vi> p;
vector<vi> ans;
vi comps[mx];
vi lins[mx];
bool solve(vi comp){ //solve in sz(comp)*n time
assert(sz(comp) > 0);
vi incomp(n, 0);
for(auto u: comp){
incomp[u] = 1;
}
for(auto u: comp){
for(int i = 0; i < n; i++){
if(incomp[i] == 1){
if(p[u][i] == 0) return 0; //component is not connected to anything
}
else{
assert(p[u][i] == 0);
}
}
}
DSU dsu2;
dsu2.init(sz(comp));
vector<vi> subcomps(sz(comp), vi{});
for(int i = 0; i < sz(comp); i++){
for(int j = i+1; j < sz(comp); j++){
if(p[comp[i]][comp[j]] == 1){
dsu2.unite(i, j);
}
}
}
for(int i = 0; i < sz(comp); i++){
subcomps[dsu2.get(i)].pb(i);
}
vi cyc;
for(int i = 0; i < sz(comp); i++){
if(sz(subcomps[i]) == 0) continue;
vi insub(sz(comp), 0);
for(auto u: subcomps[i]){
insub[u] = 1;
}
for(auto u: subcomps[i]){
for(int j = 0; j < sz(comp); j++){
if(insub[j] == 1){
if(p[comp[u]][comp[j]] != 1) return 0;
}
else{
if(p[comp[u]][comp[j]] != 2) return 0;
}
}
}
for(int j = 0; j+1 < sz(subcomps[i]); j++){
int a = comp[subcomps[i][j]];
int b = comp[subcomps[i][j+1]];
ans[a][b] = 1;
ans[b][a] = 1;
}
cyc.pb(comp[subcomps[i][0]]);
}
if(sz(cyc) == 2) return 0;
if(sz(cyc) >= 3){
for(int i = 0; i < sz(cyc); i++){
int a = cyc[i];
int b = cyc[(i+1) % sz(cyc)];
assert(a != b);
ans[a][b] = 1;
ans[b][a] = 1;
}
}
// if(sz(cyc))
// bool SUB3 = 1;
// for(auto u1: comp){
// for(auto u2: comp){
// assert(p[u1][u2] != 0);
// if(p[u1][u2] != 2) SUB3 = 0;
// }
// }
// if(SUB3){
// vi incomp(n, 0);
// for(auto u: comp) incomp[u] = 1;
// for(auto u: comp){
// for(int i = 0; i < n; i++){
// if(incomp[i] == 0){
// assert(p[u][i] == 0);
// }
// else{
// assert(p[u][i] == 2);
// }
// }
// }
// }
return 1;
}
int construct(vector<vi> _p) {
p = _p;
_p.clear();
n = sz(p);
for(int i = 0; i < n; i++){
if(p[i][i] != 1) return 0;
for(int j = 0; j < n; j++){
if(p[i][j] != p[j][i]) return 0;
if(p[i][j] == 3) return 0;
}
}
for(int i = 0; i < n; i++) {
vi row(n, 0);
ans.pb(row);
}
DSU dsu0;
dsu0.init(n);
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if(p[i][j] > 0){
dsu0.unite(i, j);
}
}
}
for(int i = 0; i < n; i++){
comps[dsu0.get(i)].pb(i);
}
for(int i = 0; i < n; i++){
if(sz(comps[i]) == 0) continue;
int res = solve(comps[i]);
if(res == 0) return 0;
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
258 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
258 ms |
22264 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
233 ms |
22140 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
118 ms |
12280 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
59 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22136 KB |
Output is correct |
22 |
Correct |
239 ms |
22136 KB |
Output is correct |
23 |
Correct |
252 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22244 KB |
Output is correct |
25 |
Correct |
111 ms |
12280 KB |
Output is correct |
26 |
Correct |
104 ms |
12280 KB |
Output is correct |
27 |
Correct |
259 ms |
22300 KB |
Output is correct |
28 |
Correct |
241 ms |
22172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
1280 KB |
Output is correct |
9 |
Correct |
235 ms |
22136 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
257 ms |
22264 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
896 KB |
Output is correct |
17 |
Correct |
118 ms |
12280 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
64 ms |
5880 KB |
Output is correct |
22 |
Correct |
238 ms |
22136 KB |
Output is correct |
23 |
Correct |
241 ms |
22264 KB |
Output is correct |
24 |
Correct |
256 ms |
22136 KB |
Output is correct |
25 |
Correct |
101 ms |
12280 KB |
Output is correct |
26 |
Correct |
103 ms |
12280 KB |
Output is correct |
27 |
Correct |
235 ms |
22136 KB |
Output is correct |
28 |
Correct |
257 ms |
22168 KB |
Output is correct |
29 |
Correct |
102 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
62 ms |
5880 KB |
Output is correct |
5 |
Correct |
245 ms |
22264 KB |
Output is correct |
6 |
Correct |
238 ms |
22264 KB |
Output is correct |
7 |
Correct |
267 ms |
22264 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
60 ms |
5880 KB |
Output is correct |
10 |
Correct |
238 ms |
22136 KB |
Output is correct |
11 |
Correct |
241 ms |
22136 KB |
Output is correct |
12 |
Correct |
258 ms |
22264 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
62 ms |
5884 KB |
Output is correct |
17 |
Correct |
250 ms |
22136 KB |
Output is correct |
18 |
Correct |
250 ms |
22136 KB |
Output is correct |
19 |
Correct |
239 ms |
22136 KB |
Output is correct |
20 |
Correct |
252 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
258 ms |
22264 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
233 ms |
22140 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
118 ms |
12280 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
59 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22136 KB |
Output is correct |
22 |
Correct |
239 ms |
22136 KB |
Output is correct |
23 |
Correct |
252 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22244 KB |
Output is correct |
25 |
Correct |
111 ms |
12280 KB |
Output is correct |
26 |
Correct |
104 ms |
12280 KB |
Output is correct |
27 |
Correct |
259 ms |
22300 KB |
Output is correct |
28 |
Correct |
241 ms |
22172 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
235 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1280 KB |
Output is correct |
41 |
Correct |
257 ms |
22264 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
896 KB |
Output is correct |
45 |
Correct |
118 ms |
12280 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
64 ms |
5880 KB |
Output is correct |
50 |
Correct |
238 ms |
22136 KB |
Output is correct |
51 |
Correct |
241 ms |
22264 KB |
Output is correct |
52 |
Correct |
256 ms |
22136 KB |
Output is correct |
53 |
Correct |
101 ms |
12280 KB |
Output is correct |
54 |
Correct |
103 ms |
12280 KB |
Output is correct |
55 |
Correct |
235 ms |
22136 KB |
Output is correct |
56 |
Correct |
257 ms |
22168 KB |
Output is correct |
57 |
Correct |
102 ms |
12280 KB |
Output is correct |
58 |
Correct |
0 ms |
384 KB |
Output is correct |
59 |
Correct |
0 ms |
384 KB |
Output is correct |
60 |
Correct |
5 ms |
872 KB |
Output is correct |
61 |
Correct |
123 ms |
12288 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
0 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
60 ms |
5880 KB |
Output is correct |
66 |
Correct |
103 ms |
12312 KB |
Output is correct |
67 |
Correct |
104 ms |
12280 KB |
Output is correct |
68 |
Correct |
101 ms |
12280 KB |
Output is correct |
69 |
Correct |
122 ms |
12292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
1280 KB |
Output is correct |
7 |
Correct |
258 ms |
22264 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
233 ms |
22140 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
118 ms |
12280 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
59 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22136 KB |
Output is correct |
22 |
Correct |
239 ms |
22136 KB |
Output is correct |
23 |
Correct |
252 ms |
22136 KB |
Output is correct |
24 |
Correct |
244 ms |
22244 KB |
Output is correct |
25 |
Correct |
111 ms |
12280 KB |
Output is correct |
26 |
Correct |
104 ms |
12280 KB |
Output is correct |
27 |
Correct |
259 ms |
22300 KB |
Output is correct |
28 |
Correct |
241 ms |
22172 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
235 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1280 KB |
Output is correct |
41 |
Correct |
257 ms |
22264 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
896 KB |
Output is correct |
45 |
Correct |
118 ms |
12280 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
64 ms |
5880 KB |
Output is correct |
50 |
Correct |
238 ms |
22136 KB |
Output is correct |
51 |
Correct |
241 ms |
22264 KB |
Output is correct |
52 |
Correct |
256 ms |
22136 KB |
Output is correct |
53 |
Correct |
101 ms |
12280 KB |
Output is correct |
54 |
Correct |
103 ms |
12280 KB |
Output is correct |
55 |
Correct |
235 ms |
22136 KB |
Output is correct |
56 |
Correct |
257 ms |
22168 KB |
Output is correct |
57 |
Correct |
102 ms |
12280 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
62 ms |
5880 KB |
Output is correct |
62 |
Correct |
245 ms |
22264 KB |
Output is correct |
63 |
Correct |
238 ms |
22264 KB |
Output is correct |
64 |
Correct |
267 ms |
22264 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
60 ms |
5880 KB |
Output is correct |
67 |
Correct |
238 ms |
22136 KB |
Output is correct |
68 |
Correct |
241 ms |
22136 KB |
Output is correct |
69 |
Correct |
258 ms |
22264 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
0 ms |
384 KB |
Output is correct |
73 |
Correct |
62 ms |
5884 KB |
Output is correct |
74 |
Correct |
250 ms |
22136 KB |
Output is correct |
75 |
Correct |
250 ms |
22136 KB |
Output is correct |
76 |
Correct |
239 ms |
22136 KB |
Output is correct |
77 |
Correct |
252 ms |
22136 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
0 ms |
384 KB |
Output is correct |
80 |
Correct |
5 ms |
872 KB |
Output is correct |
81 |
Correct |
123 ms |
12288 KB |
Output is correct |
82 |
Correct |
1 ms |
384 KB |
Output is correct |
83 |
Correct |
0 ms |
384 KB |
Output is correct |
84 |
Correct |
1 ms |
384 KB |
Output is correct |
85 |
Correct |
60 ms |
5880 KB |
Output is correct |
86 |
Correct |
103 ms |
12312 KB |
Output is correct |
87 |
Correct |
104 ms |
12280 KB |
Output is correct |
88 |
Correct |
101 ms |
12280 KB |
Output is correct |
89 |
Correct |
122 ms |
12292 KB |
Output is correct |
90 |
Correct |
0 ms |
384 KB |
Output is correct |
91 |
Correct |
0 ms |
384 KB |
Output is correct |
92 |
Correct |
5 ms |
896 KB |
Output is correct |
93 |
Correct |
115 ms |
12152 KB |
Output is correct |
94 |
Correct |
0 ms |
384 KB |
Output is correct |
95 |
Correct |
1 ms |
384 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
25 ms |
3328 KB |
Output is correct |
98 |
Correct |
104 ms |
12152 KB |
Output is correct |
99 |
Correct |
112 ms |
12152 KB |
Output is correct |
100 |
Correct |
101 ms |
12152 KB |
Output is correct |
101 |
Correct |
99 ms |
12152 KB |
Output is correct |
102 |
Correct |
120 ms |
12164 KB |
Output is correct |
103 |
Correct |
113 ms |
12152 KB |
Output is correct |
104 |
Correct |
121 ms |
12152 KB |
Output is correct |
105 |
Correct |
112 ms |
12152 KB |
Output is correct |