#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
vector<int> root;
int getroot(int u) {
return root[u] == u ? u : (root[u] = getroot(root[u]));
}
bool unite(int u, int v) {
u = getroot(u), v = getroot(v);
if (u == v) return false;
root[v] = u;
return true;
}
int construct(vector<vector<int>> p) {
int n = _size(p);
root.resize(n);
REP(i, n) root[i] = i;
vector<pair<int, int>> ans;
REP(i, n) REP(j, n) {
if (i == j) {
if (p[i][j] != 1) return 0;
continue;
}
if (p[i][j] == 1) if (unite(i, j)) ans.push_back({i, j});
if (p[i][j] > 2) return 0;
}
vector<vector<int>> adj(n);
REP(i, n) adj[getroot(i)].push_back(i);
vector<int> used(n);
vector<vector<int>> cnt(n, vector<int>(n));
REP(i, n) {
FORALL(u, adj[i]) FORALL(v, adj[i]) cnt[u][v] = 1;
if (!_size(adj[i]) || used[i]) continue;
vector<int> g;
REP(j, n) if (p[i][j] == 2) g.push_back(getroot(j));
sort(g.begin(), g.end());
g.resize(unique(g.begin(), g.end()) - g.begin());
if (!_size(g)) {
FORALL(u, adj[i]) used[u]++;
continue;
}
if (_size(g) == 1) return 0;
g.push_back(i);
REP(j, _size(g) - 1) ans.push_back({g[j], g[j + 1]});
ans.push_back({g.back(), g[0]});
vector<int> allVtx;
FORALL(id, g) FORALL(u, adj[id]) allVtx.push_back(u), used[u]++;
FORALL(u, allVtx) FORALL(v, allVtx) {
if (getroot(u) == getroot(v)) continue;
cnt[u][v] = 2;
}
}
REP(i, n) REP(j, n) if (cnt[i][j] != p[i][j]) return 0;
vector<vector<int>> b(n, vector<int>(n));
FORALL(e, ans) b[e.fi][e.se] = b[e.se][e.fi] = 1;
build(b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
1472 KB |
Output is correct |
7 |
Correct |
165 ms |
27216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
1472 KB |
Output is correct |
7 |
Correct |
165 ms |
27216 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
27472 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
13448 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
7248 KB |
Output is correct |
21 |
Correct |
156 ms |
27256 KB |
Output is correct |
22 |
Correct |
157 ms |
27216 KB |
Output is correct |
23 |
Correct |
162 ms |
27216 KB |
Output is correct |
24 |
Correct |
157 ms |
27472 KB |
Output is correct |
25 |
Correct |
67 ms |
13576 KB |
Output is correct |
26 |
Correct |
68 ms |
13792 KB |
Output is correct |
27 |
Correct |
165 ms |
27216 KB |
Output is correct |
28 |
Correct |
157 ms |
27292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
156 ms |
27404 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
157 ms |
27472 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
71 ms |
13580 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
42 ms |
7788 KB |
Output is correct |
22 |
Correct |
163 ms |
27472 KB |
Output is correct |
23 |
Correct |
158 ms |
27728 KB |
Output is correct |
24 |
Correct |
158 ms |
27472 KB |
Output is correct |
25 |
Correct |
67 ms |
13648 KB |
Output is correct |
26 |
Correct |
66 ms |
13392 KB |
Output is correct |
27 |
Correct |
157 ms |
27384 KB |
Output is correct |
28 |
Correct |
159 ms |
27256 KB |
Output is correct |
29 |
Correct |
65 ms |
13392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
46 ms |
7248 KB |
Output is correct |
5 |
Correct |
157 ms |
27216 KB |
Output is correct |
6 |
Correct |
157 ms |
27472 KB |
Output is correct |
7 |
Correct |
163 ms |
27220 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
40 ms |
7200 KB |
Output is correct |
10 |
Correct |
156 ms |
27476 KB |
Output is correct |
11 |
Correct |
158 ms |
27472 KB |
Output is correct |
12 |
Correct |
160 ms |
27452 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
44 ms |
7248 KB |
Output is correct |
17 |
Correct |
152 ms |
28024 KB |
Output is correct |
18 |
Correct |
159 ms |
28024 KB |
Output is correct |
19 |
Correct |
173 ms |
28224 KB |
Output is correct |
20 |
Correct |
159 ms |
28072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
1472 KB |
Output is correct |
7 |
Correct |
165 ms |
27216 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
27472 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
13448 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
7248 KB |
Output is correct |
21 |
Correct |
156 ms |
27256 KB |
Output is correct |
22 |
Correct |
157 ms |
27216 KB |
Output is correct |
23 |
Correct |
162 ms |
27216 KB |
Output is correct |
24 |
Correct |
157 ms |
27472 KB |
Output is correct |
25 |
Correct |
67 ms |
13576 KB |
Output is correct |
26 |
Correct |
68 ms |
13792 KB |
Output is correct |
27 |
Correct |
165 ms |
27216 KB |
Output is correct |
28 |
Correct |
157 ms |
27292 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
156 ms |
27404 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
157 ms |
27472 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
856 KB |
Output is correct |
45 |
Correct |
71 ms |
13580 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
42 ms |
7788 KB |
Output is correct |
50 |
Correct |
163 ms |
27472 KB |
Output is correct |
51 |
Correct |
158 ms |
27728 KB |
Output is correct |
52 |
Correct |
158 ms |
27472 KB |
Output is correct |
53 |
Correct |
67 ms |
13648 KB |
Output is correct |
54 |
Correct |
66 ms |
13392 KB |
Output is correct |
55 |
Correct |
157 ms |
27384 KB |
Output is correct |
56 |
Correct |
159 ms |
27256 KB |
Output is correct |
57 |
Correct |
65 ms |
13392 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
860 KB |
Output is correct |
61 |
Correct |
73 ms |
13392 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
344 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
40 ms |
7252 KB |
Output is correct |
66 |
Correct |
66 ms |
14152 KB |
Output is correct |
67 |
Correct |
67 ms |
14160 KB |
Output is correct |
68 |
Correct |
65 ms |
14160 KB |
Output is correct |
69 |
Correct |
65 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
1472 KB |
Output is correct |
7 |
Correct |
165 ms |
27216 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
158 ms |
27472 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
73 ms |
13448 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
40 ms |
7248 KB |
Output is correct |
21 |
Correct |
156 ms |
27256 KB |
Output is correct |
22 |
Correct |
157 ms |
27216 KB |
Output is correct |
23 |
Correct |
162 ms |
27216 KB |
Output is correct |
24 |
Correct |
157 ms |
27472 KB |
Output is correct |
25 |
Correct |
67 ms |
13576 KB |
Output is correct |
26 |
Correct |
68 ms |
13792 KB |
Output is correct |
27 |
Correct |
165 ms |
27216 KB |
Output is correct |
28 |
Correct |
157 ms |
27292 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1372 KB |
Output is correct |
37 |
Correct |
156 ms |
27404 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
157 ms |
27472 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
856 KB |
Output is correct |
45 |
Correct |
71 ms |
13580 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
42 ms |
7788 KB |
Output is correct |
50 |
Correct |
163 ms |
27472 KB |
Output is correct |
51 |
Correct |
158 ms |
27728 KB |
Output is correct |
52 |
Correct |
158 ms |
27472 KB |
Output is correct |
53 |
Correct |
67 ms |
13648 KB |
Output is correct |
54 |
Correct |
66 ms |
13392 KB |
Output is correct |
55 |
Correct |
157 ms |
27384 KB |
Output is correct |
56 |
Correct |
159 ms |
27256 KB |
Output is correct |
57 |
Correct |
65 ms |
13392 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
46 ms |
7248 KB |
Output is correct |
62 |
Correct |
157 ms |
27216 KB |
Output is correct |
63 |
Correct |
157 ms |
27472 KB |
Output is correct |
64 |
Correct |
163 ms |
27220 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
40 ms |
7200 KB |
Output is correct |
67 |
Correct |
156 ms |
27476 KB |
Output is correct |
68 |
Correct |
158 ms |
27472 KB |
Output is correct |
69 |
Correct |
160 ms |
27452 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
44 ms |
7248 KB |
Output is correct |
74 |
Correct |
152 ms |
28024 KB |
Output is correct |
75 |
Correct |
159 ms |
28024 KB |
Output is correct |
76 |
Correct |
173 ms |
28224 KB |
Output is correct |
77 |
Correct |
159 ms |
28072 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
860 KB |
Output is correct |
81 |
Correct |
73 ms |
13392 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
344 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
40 ms |
7252 KB |
Output is correct |
86 |
Correct |
66 ms |
14152 KB |
Output is correct |
87 |
Correct |
67 ms |
14160 KB |
Output is correct |
88 |
Correct |
65 ms |
14160 KB |
Output is correct |
89 |
Correct |
65 ms |
14160 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
344 KB |
Output is correct |
92 |
Correct |
3 ms |
600 KB |
Output is correct |
93 |
Correct |
61 ms |
10248 KB |
Output is correct |
94 |
Correct |
1 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
1 ms |
348 KB |
Output is correct |
97 |
Correct |
16 ms |
2640 KB |
Output is correct |
98 |
Correct |
64 ms |
10228 KB |
Output is correct |
99 |
Correct |
64 ms |
10068 KB |
Output is correct |
100 |
Correct |
64 ms |
10152 KB |
Output is correct |
101 |
Correct |
63 ms |
10064 KB |
Output is correct |
102 |
Correct |
63 ms |
10064 KB |
Output is correct |
103 |
Correct |
63 ms |
10104 KB |
Output is correct |
104 |
Correct |
58 ms |
10072 KB |
Output is correct |
105 |
Correct |
64 ms |
10068 KB |
Output is correct |