#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
class DisjointSet {
private:
vector<int> par, sz;
public:
DisjointSet(int n) {
par = sz = vector<int>(n);
for (int i = 0; i < n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int root(int x) {
if (x == par[x]) {
return x;
}
return par[x] = root(par[x]);
}
void join(int a, int b) {
a = root(a);
b = root(b);
if (a == b) {
return;
}
if (sz[a] < sz[b]) {
swap(a, b);
}
sz[a] += sz[b];
par[b] = a;
}
bool is_connected(int a, int b) {
return root(a) == root(b);
}
};
const int MAX_N = 1e3 + 5;
vector<vector<int>> g(MAX_N);
vector<int> cnt_two(MAX_N);
bitset<MAX_N> vis;
int construct(vector<vector<int>> mat) {
const int n = (int) mat.size();
DisjointSet DSU(n);
vector<vector<int>> ans(n, vector<int>(n));
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
if (mat[i][j] == 3) {
return 0;
}
cnt += (mat[i][j] == 2);
if (mat[i][j] == 1) {
g[i].emplace_back(j);
}
if (mat[i][j] > 0) {
DSU.join(i, j);
}
}
cnt_two[i] = cnt;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0 && DSU.is_connected(i, j)) {
return 0;
}
}
}
vector<vector<int>> root(MAX_N);
for (int i = 0; i < n; i++) {
root[DSU.root(i)].emplace_back(i);
}
auto add_edge = [&](int st, int et) {
if (st == et) {
return;
}
ans[st][et] = ans[et][st] = 1;
};
auto build_cycle = [&](const vector<int> &arr) {
for (int i = 1; i < (int) arr.size(); i++) {
add_edge(arr[i], arr[i - 1]);
}
add_edge(arr.front(), arr.back());
};
vis.reset();
for (auto to : root) {
if (to.empty()) {
continue;
}
const int sz = (int) to.size();
vector<int> main_cycle;
for (auto cur : to) {
if (cnt_two[cur] == sz - 1) {
main_cycle.emplace_back(cur);
} else {
if (vis[cur]) {
continue;
}
main_cycle.emplace_back(cur);
vis[cur] = true;
vector<int> chain = {cur};
for (auto add : g[cur]) {
assert(vis[add] == false);
vis[add] = true;
add_edge(cur, add);
}
}
}
if (main_cycle.size() == 2) {
return 0;
}
build_cycle(main_cycle);
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1408 KB |
Output is correct |
7 |
Correct |
265 ms |
26224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1408 KB |
Output is correct |
7 |
Correct |
265 ms |
26224 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1280 KB |
Output is correct |
13 |
Correct |
248 ms |
22128 KB |
Output is correct |
14 |
Correct |
1 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 |
128 ms |
14580 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
63 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22648 KB |
Output is correct |
22 |
Correct |
247 ms |
22136 KB |
Output is correct |
23 |
Correct |
263 ms |
24184 KB |
Output is correct |
24 |
Correct |
249 ms |
22136 KB |
Output is correct |
25 |
Correct |
111 ms |
12664 KB |
Output is correct |
26 |
Correct |
114 ms |
12284 KB |
Output is correct |
27 |
Correct |
268 ms |
25208 KB |
Output is correct |
28 |
Correct |
252 ms |
22132 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 |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
247 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 |
255 ms |
22136 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 |
768 KB |
Output is correct |
17 |
Correct |
123 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
63 ms |
5880 KB |
Output is correct |
22 |
Correct |
251 ms |
22264 KB |
Output is correct |
23 |
Correct |
248 ms |
22160 KB |
Output is correct |
24 |
Correct |
257 ms |
22136 KB |
Output is correct |
25 |
Correct |
116 ms |
12312 KB |
Output is correct |
26 |
Correct |
113 ms |
12152 KB |
Output is correct |
27 |
Correct |
251 ms |
22264 KB |
Output is correct |
28 |
Correct |
263 ms |
22264 KB |
Output is correct |
29 |
Correct |
110 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 |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
64 ms |
5880 KB |
Output is correct |
5 |
Correct |
248 ms |
22648 KB |
Output is correct |
6 |
Correct |
251 ms |
22136 KB |
Output is correct |
7 |
Correct |
267 ms |
24308 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
62 ms |
5880 KB |
Output is correct |
10 |
Correct |
254 ms |
22136 KB |
Output is correct |
11 |
Correct |
252 ms |
22136 KB |
Output is correct |
12 |
Correct |
258 ms |
22136 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
62 ms |
5884 KB |
Output is correct |
17 |
Correct |
265 ms |
22136 KB |
Output is correct |
18 |
Correct |
248 ms |
22264 KB |
Output is correct |
19 |
Correct |
249 ms |
22264 KB |
Output is correct |
20 |
Correct |
246 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1408 KB |
Output is correct |
7 |
Correct |
265 ms |
26224 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1280 KB |
Output is correct |
13 |
Correct |
248 ms |
22128 KB |
Output is correct |
14 |
Correct |
1 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 |
128 ms |
14580 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
63 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22648 KB |
Output is correct |
22 |
Correct |
247 ms |
22136 KB |
Output is correct |
23 |
Correct |
263 ms |
24184 KB |
Output is correct |
24 |
Correct |
249 ms |
22136 KB |
Output is correct |
25 |
Correct |
111 ms |
12664 KB |
Output is correct |
26 |
Correct |
114 ms |
12284 KB |
Output is correct |
27 |
Correct |
268 ms |
25208 KB |
Output is correct |
28 |
Correct |
252 ms |
22132 KB |
Output is correct |
29 |
Correct |
1 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 |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
247 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 |
255 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
123 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
63 ms |
5880 KB |
Output is correct |
50 |
Correct |
251 ms |
22264 KB |
Output is correct |
51 |
Correct |
248 ms |
22160 KB |
Output is correct |
52 |
Correct |
257 ms |
22136 KB |
Output is correct |
53 |
Correct |
116 ms |
12312 KB |
Output is correct |
54 |
Correct |
113 ms |
12152 KB |
Output is correct |
55 |
Correct |
251 ms |
22264 KB |
Output is correct |
56 |
Correct |
263 ms |
22264 KB |
Output is correct |
57 |
Correct |
110 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 |
6 ms |
896 KB |
Output is correct |
61 |
Correct |
131 ms |
14196 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 |
62 ms |
5880 KB |
Output is correct |
66 |
Correct |
114 ms |
12152 KB |
Output is correct |
67 |
Correct |
109 ms |
12152 KB |
Output is correct |
68 |
Correct |
108 ms |
12280 KB |
Output is correct |
69 |
Correct |
114 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1408 KB |
Output is correct |
7 |
Correct |
265 ms |
26224 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1280 KB |
Output is correct |
13 |
Correct |
248 ms |
22128 KB |
Output is correct |
14 |
Correct |
1 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 |
128 ms |
14580 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
63 ms |
5880 KB |
Output is correct |
21 |
Correct |
248 ms |
22648 KB |
Output is correct |
22 |
Correct |
247 ms |
22136 KB |
Output is correct |
23 |
Correct |
263 ms |
24184 KB |
Output is correct |
24 |
Correct |
249 ms |
22136 KB |
Output is correct |
25 |
Correct |
111 ms |
12664 KB |
Output is correct |
26 |
Correct |
114 ms |
12284 KB |
Output is correct |
27 |
Correct |
268 ms |
25208 KB |
Output is correct |
28 |
Correct |
252 ms |
22132 KB |
Output is correct |
29 |
Correct |
1 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 |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
247 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 |
255 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
6 ms |
768 KB |
Output is correct |
45 |
Correct |
123 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
63 ms |
5880 KB |
Output is correct |
50 |
Correct |
251 ms |
22264 KB |
Output is correct |
51 |
Correct |
248 ms |
22160 KB |
Output is correct |
52 |
Correct |
257 ms |
22136 KB |
Output is correct |
53 |
Correct |
116 ms |
12312 KB |
Output is correct |
54 |
Correct |
113 ms |
12152 KB |
Output is correct |
55 |
Correct |
251 ms |
22264 KB |
Output is correct |
56 |
Correct |
263 ms |
22264 KB |
Output is correct |
57 |
Correct |
110 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 |
0 ms |
384 KB |
Output is correct |
61 |
Correct |
64 ms |
5880 KB |
Output is correct |
62 |
Correct |
248 ms |
22648 KB |
Output is correct |
63 |
Correct |
251 ms |
22136 KB |
Output is correct |
64 |
Correct |
267 ms |
24308 KB |
Output is correct |
65 |
Correct |
0 ms |
384 KB |
Output is correct |
66 |
Correct |
62 ms |
5880 KB |
Output is correct |
67 |
Correct |
254 ms |
22136 KB |
Output is correct |
68 |
Correct |
252 ms |
22136 KB |
Output is correct |
69 |
Correct |
258 ms |
22136 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
0 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
62 ms |
5884 KB |
Output is correct |
74 |
Correct |
265 ms |
22136 KB |
Output is correct |
75 |
Correct |
248 ms |
22264 KB |
Output is correct |
76 |
Correct |
249 ms |
22264 KB |
Output is correct |
77 |
Correct |
246 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 |
6 ms |
896 KB |
Output is correct |
81 |
Correct |
131 ms |
14196 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 |
62 ms |
5880 KB |
Output is correct |
86 |
Correct |
114 ms |
12152 KB |
Output is correct |
87 |
Correct |
109 ms |
12152 KB |
Output is correct |
88 |
Correct |
108 ms |
12280 KB |
Output is correct |
89 |
Correct |
114 ms |
12280 KB |
Output is correct |
90 |
Correct |
0 ms |
384 KB |
Output is correct |
91 |
Correct |
1 ms |
384 KB |
Output is correct |
92 |
Correct |
5 ms |
768 KB |
Output is correct |
93 |
Correct |
115 ms |
12152 KB |
Output is correct |
94 |
Correct |
0 ms |
384 KB |
Output is correct |
95 |
Correct |
0 ms |
384 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
26 ms |
3328 KB |
Output is correct |
98 |
Correct |
104 ms |
12152 KB |
Output is correct |
99 |
Correct |
108 ms |
12152 KB |
Output is correct |
100 |
Correct |
108 ms |
12152 KB |
Output is correct |
101 |
Correct |
103 ms |
12152 KB |
Output is correct |
102 |
Correct |
114 ms |
12236 KB |
Output is correct |
103 |
Correct |
113 ms |
12152 KB |
Output is correct |
104 |
Correct |
113 ms |
12152 KB |
Output is correct |
105 |
Correct |
112 ms |
12152 KB |
Output is correct |