#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct DisjointSetUnion {
int n;
vector<int> par;
vector<int> sz;
DisjointSetUnion(int n) {
par.resize(n);
sz.assign(n, 1);
iota(par.begin(), par.end(), 0);
}
int find(int x) {
return par[x] = (x == par[x] ? x : find(par[x]));
}
void unite(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
}
bool same(int u, int v) {
return (find(u) == find(v));
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
bool bad = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
bad |= (p[i][j] == 3);
}
}
if (bad) return 0;
vector<vector<int>> b(n, vector<int> (n, 0));
DisjointSetUnion dsu(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 1) {
dsu.unite(i, j);
}
}
}
vector<int> trees[n];
vector<int> roots;
for (int i = 0; i < n; i++) {
if (dsu.par[i] == i) {
vector<int> nodes;
for (int j = 0; j < n; j++) {
if (dsu.par[j] == i) {
nodes.push_back(j);
}
}
for (int u : nodes) {
for (int v : nodes) {
bad |= (p[u][v] != 1);
}
}
trees[i] = nodes;
roots.push_back(i);
for (int j = 1; j < (int) nodes.size(); j++) b[nodes[j]][nodes[j - 1]] = b[nodes[j - 1]][nodes[j]] = 1;
}
}
if (bad) return 0;
DisjointSetUnion rot(n);
for (int u : roots) {
for (int v : roots) {
if (p[u][v] == 2) {
rot.unite(u, v);
}
}
}
for (int u : roots) {
if (u == rot.par[u] && rot.sz[u] > 2) {
vector<int> nodes;
for (int v : roots) {
if (u == rot.par[v]) {
nodes.push_back(v);
}
}
for (int i : nodes) {
for (int j : nodes) {
if (i == j) continue;
for (int x : trees[i]) {
for (int y : trees[j]) {
bad |= (p[x][y] != 2);
}
}
}
}
int sz = nodes.size();
for (int i = 0; i < sz; i++) {
b[nodes[i]][nodes[(i + 1) % sz]] = b[nodes[(i + 1) % sz]][nodes[i]] = 1;
}
} else if (u == rot.par[u] && rot.sz[u] == 2) {
bad = true;
}
}
if (bad) return 0;
build(b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1100 KB |
Output is correct |
7 |
Correct |
238 ms |
22012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1100 KB |
Output is correct |
7 |
Correct |
238 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1176 KB |
Output is correct |
13 |
Correct |
238 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
118 ms |
12160 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
57 ms |
5756 KB |
Output is correct |
21 |
Correct |
226 ms |
21996 KB |
Output is correct |
22 |
Correct |
227 ms |
22084 KB |
Output is correct |
23 |
Correct |
245 ms |
22084 KB |
Output is correct |
24 |
Correct |
231 ms |
22084 KB |
Output is correct |
25 |
Correct |
98 ms |
12152 KB |
Output is correct |
26 |
Correct |
96 ms |
12168 KB |
Output is correct |
27 |
Correct |
244 ms |
22024 KB |
Output is correct |
28 |
Correct |
235 ms |
22148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
11 ms |
1228 KB |
Output is correct |
9 |
Correct |
233 ms |
24116 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 |
1200 KB |
Output is correct |
13 |
Correct |
244 ms |
24004 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
788 KB |
Output is correct |
17 |
Correct |
121 ms |
14064 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
59 ms |
6252 KB |
Output is correct |
22 |
Correct |
245 ms |
24048 KB |
Output is correct |
23 |
Correct |
228 ms |
24116 KB |
Output is correct |
24 |
Correct |
253 ms |
24060 KB |
Output is correct |
25 |
Correct |
100 ms |
14148 KB |
Output is correct |
26 |
Correct |
104 ms |
14064 KB |
Output is correct |
27 |
Correct |
230 ms |
24064 KB |
Output is correct |
28 |
Correct |
253 ms |
24004 KB |
Output is correct |
29 |
Correct |
102 ms |
14060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
59 ms |
5776 KB |
Output is correct |
5 |
Correct |
227 ms |
21960 KB |
Output is correct |
6 |
Correct |
225 ms |
21988 KB |
Output is correct |
7 |
Correct |
241 ms |
22076 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
63 ms |
5864 KB |
Output is correct |
10 |
Correct |
238 ms |
22008 KB |
Output is correct |
11 |
Correct |
241 ms |
22008 KB |
Output is correct |
12 |
Correct |
246 ms |
22084 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
60 ms |
5756 KB |
Output is correct |
17 |
Correct |
233 ms |
22060 KB |
Output is correct |
18 |
Correct |
232 ms |
22084 KB |
Output is correct |
19 |
Correct |
268 ms |
21984 KB |
Output is correct |
20 |
Correct |
240 ms |
22084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1100 KB |
Output is correct |
7 |
Correct |
238 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1176 KB |
Output is correct |
13 |
Correct |
238 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
118 ms |
12160 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
57 ms |
5756 KB |
Output is correct |
21 |
Correct |
226 ms |
21996 KB |
Output is correct |
22 |
Correct |
227 ms |
22084 KB |
Output is correct |
23 |
Correct |
245 ms |
22084 KB |
Output is correct |
24 |
Correct |
231 ms |
22084 KB |
Output is correct |
25 |
Correct |
98 ms |
12152 KB |
Output is correct |
26 |
Correct |
96 ms |
12168 KB |
Output is correct |
27 |
Correct |
244 ms |
22024 KB |
Output is correct |
28 |
Correct |
235 ms |
22148 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1228 KB |
Output is correct |
37 |
Correct |
233 ms |
24116 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1200 KB |
Output is correct |
41 |
Correct |
244 ms |
24004 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
788 KB |
Output is correct |
45 |
Correct |
121 ms |
14064 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
6252 KB |
Output is correct |
50 |
Correct |
245 ms |
24048 KB |
Output is correct |
51 |
Correct |
228 ms |
24116 KB |
Output is correct |
52 |
Correct |
253 ms |
24060 KB |
Output is correct |
53 |
Correct |
100 ms |
14148 KB |
Output is correct |
54 |
Correct |
104 ms |
14064 KB |
Output is correct |
55 |
Correct |
230 ms |
24064 KB |
Output is correct |
56 |
Correct |
253 ms |
24004 KB |
Output is correct |
57 |
Correct |
102 ms |
14060 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
844 KB |
Output is correct |
61 |
Correct |
121 ms |
14064 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
204 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
58 ms |
6252 KB |
Output is correct |
66 |
Correct |
102 ms |
14068 KB |
Output is correct |
67 |
Correct |
102 ms |
14020 KB |
Output is correct |
68 |
Correct |
106 ms |
14208 KB |
Output is correct |
69 |
Correct |
99 ms |
14064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
1100 KB |
Output is correct |
7 |
Correct |
238 ms |
22012 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1176 KB |
Output is correct |
13 |
Correct |
238 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
118 ms |
12160 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
57 ms |
5756 KB |
Output is correct |
21 |
Correct |
226 ms |
21996 KB |
Output is correct |
22 |
Correct |
227 ms |
22084 KB |
Output is correct |
23 |
Correct |
245 ms |
22084 KB |
Output is correct |
24 |
Correct |
231 ms |
22084 KB |
Output is correct |
25 |
Correct |
98 ms |
12152 KB |
Output is correct |
26 |
Correct |
96 ms |
12168 KB |
Output is correct |
27 |
Correct |
244 ms |
22024 KB |
Output is correct |
28 |
Correct |
235 ms |
22148 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1228 KB |
Output is correct |
37 |
Correct |
233 ms |
24116 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1200 KB |
Output is correct |
41 |
Correct |
244 ms |
24004 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
6 ms |
788 KB |
Output is correct |
45 |
Correct |
121 ms |
14064 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
6252 KB |
Output is correct |
50 |
Correct |
245 ms |
24048 KB |
Output is correct |
51 |
Correct |
228 ms |
24116 KB |
Output is correct |
52 |
Correct |
253 ms |
24060 KB |
Output is correct |
53 |
Correct |
100 ms |
14148 KB |
Output is correct |
54 |
Correct |
104 ms |
14064 KB |
Output is correct |
55 |
Correct |
230 ms |
24064 KB |
Output is correct |
56 |
Correct |
253 ms |
24004 KB |
Output is correct |
57 |
Correct |
102 ms |
14060 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
59 ms |
5776 KB |
Output is correct |
62 |
Correct |
227 ms |
21960 KB |
Output is correct |
63 |
Correct |
225 ms |
21988 KB |
Output is correct |
64 |
Correct |
241 ms |
22076 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
63 ms |
5864 KB |
Output is correct |
67 |
Correct |
238 ms |
22008 KB |
Output is correct |
68 |
Correct |
241 ms |
22008 KB |
Output is correct |
69 |
Correct |
246 ms |
22084 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
60 ms |
5756 KB |
Output is correct |
74 |
Correct |
233 ms |
22060 KB |
Output is correct |
75 |
Correct |
232 ms |
22084 KB |
Output is correct |
76 |
Correct |
268 ms |
21984 KB |
Output is correct |
77 |
Correct |
240 ms |
22084 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
5 ms |
844 KB |
Output is correct |
81 |
Correct |
121 ms |
14064 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |
84 |
Correct |
0 ms |
204 KB |
Output is correct |
85 |
Correct |
58 ms |
6252 KB |
Output is correct |
86 |
Correct |
102 ms |
14068 KB |
Output is correct |
87 |
Correct |
102 ms |
14020 KB |
Output is correct |
88 |
Correct |
106 ms |
14208 KB |
Output is correct |
89 |
Correct |
99 ms |
14064 KB |
Output is correct |
90 |
Correct |
1 ms |
204 KB |
Output is correct |
91 |
Correct |
0 ms |
292 KB |
Output is correct |
92 |
Correct |
5 ms |
668 KB |
Output is correct |
93 |
Correct |
96 ms |
10108 KB |
Output is correct |
94 |
Correct |
0 ms |
204 KB |
Output is correct |
95 |
Correct |
1 ms |
204 KB |
Output is correct |
96 |
Correct |
0 ms |
204 KB |
Output is correct |
97 |
Correct |
25 ms |
2668 KB |
Output is correct |
98 |
Correct |
92 ms |
10100 KB |
Output is correct |
99 |
Correct |
93 ms |
10108 KB |
Output is correct |
100 |
Correct |
92 ms |
10096 KB |
Output is correct |
101 |
Correct |
92 ms |
10052 KB |
Output is correct |
102 |
Correct |
93 ms |
10112 KB |
Output is correct |
103 |
Correct |
94 ms |
10180 KB |
Output is correct |
104 |
Correct |
94 ms |
10052 KB |
Output is correct |
105 |
Correct |
98 ms |
10108 KB |
Output is correct |