#include "supertrees.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e3 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
vector <int> s[N];
int ans[N][N];
int a[N][N];
int c[N];
int n,comp;
bool vis[N];
int solve() {
for (int i = 0; i < n; i++) if (!vis[i]) {
queue <int> q({i});
vis[i] = true;
while (q.size()) {
int u = q.front(); q.pop();
s[comp].push_back(u);
c[u] = comp;
for (int v = 0; v < n; v++) if (a[u][v] > 0) {
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
comp++;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == 3)
return 0;
for (int i = 0; i < n; i++)
vis[i] = 0;
for (int i = 0; i < comp; i++) {
for (int j : s[i])
for (int k = 0; k < n; k++) {
if (c[j] == c[k] && a[j][k] == 0)
return 0;
if (c[j] != c[k] && a[j][k] > 0)
return 0;
}
vector <int> cir;
for (int j : s[i]) if (!vis[j]) {
int ok = true;
vector <int> tree;
for (int k : s[i])
if (a[j][k] == 1)
tree.push_back(k);
for (int x : tree)
vis[x] = true;
for (int it = 0; it + 1 < (int) tree.size(); it++) {
int u = tree[it];
int v = tree[it + 1];
ans[u][v] = ans[v][u] = 1;
}
for (int x : tree)
for (int y : s[i])
if (!vis[y] && a[x][y] != 2)
return 0;
cir.push_back(tree.back());
}
if (cir.size() == 2)
return 0;
if (cir.size() == 1)
continue;
for (int j = 0; j < (int) cir.size(); j++) {
int u = cir[j];
int v = cir[(j + 1) % cir.size()];
ans[u][v] = ans[v][u] = 1;
}
}
return 1;
}
int construct(vector<vector<int>> p) {
n = p.size();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = p[i][j];
int res = solve();
if (!res)
return 0;
vector<vector<int>> answer;
for (int i = 0; i < n; i++) {
vector<int> row;
for (int j = 0; j < n; j++)
row.push_back(ans[i][j]);
answer.push_back(row);
}
build(answer);
return res;
}
//#include "grader.cpp"
Compilation message
supertrees.cpp: In function 'int solve()':
supertrees.cpp:74:8: warning: unused variable 'ok' [-Wunused-variable]
74 | int ok = true;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2836 KB |
Output is correct |
7 |
Correct |
207 ms |
29964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2836 KB |
Output is correct |
7 |
Correct |
207 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
13 ms |
2032 KB |
Output is correct |
13 |
Correct |
202 ms |
26156 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1444 KB |
Output is correct |
17 |
Correct |
85 ms |
12068 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
60 ms |
9536 KB |
Output is correct |
21 |
Correct |
206 ms |
29900 KB |
Output is correct |
22 |
Correct |
191 ms |
29488 KB |
Output is correct |
23 |
Correct |
236 ms |
29856 KB |
Output is correct |
24 |
Correct |
191 ms |
26244 KB |
Output is correct |
25 |
Correct |
82 ms |
14116 KB |
Output is correct |
26 |
Correct |
99 ms |
12492 KB |
Output is correct |
27 |
Correct |
187 ms |
29876 KB |
Output is correct |
28 |
Correct |
201 ms |
26280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
2004 KB |
Output is correct |
9 |
Correct |
198 ms |
26032 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
2772 KB |
Output is correct |
13 |
Correct |
183 ms |
29944 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
1364 KB |
Output is correct |
17 |
Correct |
85 ms |
12084 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
47 ms |
9588 KB |
Output is correct |
22 |
Correct |
187 ms |
29884 KB |
Output is correct |
23 |
Correct |
196 ms |
29876 KB |
Output is correct |
24 |
Correct |
198 ms |
29876 KB |
Output is correct |
25 |
Correct |
107 ms |
12364 KB |
Output is correct |
26 |
Correct |
73 ms |
12876 KB |
Output is correct |
27 |
Correct |
219 ms |
29924 KB |
Output is correct |
28 |
Correct |
202 ms |
29932 KB |
Output is correct |
29 |
Correct |
76 ms |
12328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
52 ms |
9548 KB |
Output is correct |
5 |
Correct |
186 ms |
29872 KB |
Output is correct |
6 |
Correct |
196 ms |
29488 KB |
Output is correct |
7 |
Correct |
201 ms |
29892 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
46 ms |
9612 KB |
Output is correct |
10 |
Correct |
216 ms |
30000 KB |
Output is correct |
11 |
Correct |
181 ms |
29908 KB |
Output is correct |
12 |
Correct |
255 ms |
29936 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
49 ms |
9588 KB |
Output is correct |
17 |
Correct |
178 ms |
31820 KB |
Output is correct |
18 |
Correct |
204 ms |
31736 KB |
Output is correct |
19 |
Correct |
199 ms |
31756 KB |
Output is correct |
20 |
Correct |
209 ms |
28172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2836 KB |
Output is correct |
7 |
Correct |
207 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
13 ms |
2032 KB |
Output is correct |
13 |
Correct |
202 ms |
26156 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1444 KB |
Output is correct |
17 |
Correct |
85 ms |
12068 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
60 ms |
9536 KB |
Output is correct |
21 |
Correct |
206 ms |
29900 KB |
Output is correct |
22 |
Correct |
191 ms |
29488 KB |
Output is correct |
23 |
Correct |
236 ms |
29856 KB |
Output is correct |
24 |
Correct |
191 ms |
26244 KB |
Output is correct |
25 |
Correct |
82 ms |
14116 KB |
Output is correct |
26 |
Correct |
99 ms |
12492 KB |
Output is correct |
27 |
Correct |
187 ms |
29876 KB |
Output is correct |
28 |
Correct |
201 ms |
26280 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
9 ms |
2004 KB |
Output is correct |
37 |
Correct |
198 ms |
26032 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
8 ms |
2772 KB |
Output is correct |
41 |
Correct |
183 ms |
29944 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
7 ms |
1364 KB |
Output is correct |
45 |
Correct |
85 ms |
12084 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
47 ms |
9588 KB |
Output is correct |
50 |
Correct |
187 ms |
29884 KB |
Output is correct |
51 |
Correct |
196 ms |
29876 KB |
Output is correct |
52 |
Correct |
198 ms |
29876 KB |
Output is correct |
53 |
Correct |
107 ms |
12364 KB |
Output is correct |
54 |
Correct |
73 ms |
12876 KB |
Output is correct |
55 |
Correct |
219 ms |
29924 KB |
Output is correct |
56 |
Correct |
202 ms |
29932 KB |
Output is correct |
57 |
Correct |
76 ms |
12328 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
1364 KB |
Output is correct |
61 |
Correct |
83 ms |
12076 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
49 ms |
9584 KB |
Output is correct |
66 |
Correct |
74 ms |
14484 KB |
Output is correct |
67 |
Correct |
101 ms |
15776 KB |
Output is correct |
68 |
Correct |
100 ms |
14492 KB |
Output is correct |
69 |
Correct |
78 ms |
14104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2836 KB |
Output is correct |
7 |
Correct |
207 ms |
29964 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
13 ms |
2032 KB |
Output is correct |
13 |
Correct |
202 ms |
26156 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1444 KB |
Output is correct |
17 |
Correct |
85 ms |
12068 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
60 ms |
9536 KB |
Output is correct |
21 |
Correct |
206 ms |
29900 KB |
Output is correct |
22 |
Correct |
191 ms |
29488 KB |
Output is correct |
23 |
Correct |
236 ms |
29856 KB |
Output is correct |
24 |
Correct |
191 ms |
26244 KB |
Output is correct |
25 |
Correct |
82 ms |
14116 KB |
Output is correct |
26 |
Correct |
99 ms |
12492 KB |
Output is correct |
27 |
Correct |
187 ms |
29876 KB |
Output is correct |
28 |
Correct |
201 ms |
26280 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
9 ms |
2004 KB |
Output is correct |
37 |
Correct |
198 ms |
26032 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
8 ms |
2772 KB |
Output is correct |
41 |
Correct |
183 ms |
29944 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
7 ms |
1364 KB |
Output is correct |
45 |
Correct |
85 ms |
12084 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
47 ms |
9588 KB |
Output is correct |
50 |
Correct |
187 ms |
29884 KB |
Output is correct |
51 |
Correct |
196 ms |
29876 KB |
Output is correct |
52 |
Correct |
198 ms |
29876 KB |
Output is correct |
53 |
Correct |
107 ms |
12364 KB |
Output is correct |
54 |
Correct |
73 ms |
12876 KB |
Output is correct |
55 |
Correct |
219 ms |
29924 KB |
Output is correct |
56 |
Correct |
202 ms |
29932 KB |
Output is correct |
57 |
Correct |
76 ms |
12328 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
52 ms |
9548 KB |
Output is correct |
62 |
Correct |
186 ms |
29872 KB |
Output is correct |
63 |
Correct |
196 ms |
29488 KB |
Output is correct |
64 |
Correct |
201 ms |
29892 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
46 ms |
9612 KB |
Output is correct |
67 |
Correct |
216 ms |
30000 KB |
Output is correct |
68 |
Correct |
181 ms |
29908 KB |
Output is correct |
69 |
Correct |
255 ms |
29936 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
49 ms |
9588 KB |
Output is correct |
74 |
Correct |
178 ms |
31820 KB |
Output is correct |
75 |
Correct |
204 ms |
31736 KB |
Output is correct |
76 |
Correct |
199 ms |
31756 KB |
Output is correct |
77 |
Correct |
209 ms |
28172 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
1364 KB |
Output is correct |
81 |
Correct |
83 ms |
12076 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
49 ms |
9584 KB |
Output is correct |
86 |
Correct |
74 ms |
14484 KB |
Output is correct |
87 |
Correct |
101 ms |
15776 KB |
Output is correct |
88 |
Correct |
100 ms |
14492 KB |
Output is correct |
89 |
Correct |
78 ms |
14104 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
332 KB |
Output is correct |
92 |
Correct |
4 ms |
1492 KB |
Output is correct |
93 |
Correct |
103 ms |
14100 KB |
Output is correct |
94 |
Correct |
1 ms |
332 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
340 KB |
Output is correct |
97 |
Correct |
30 ms |
4756 KB |
Output is correct |
98 |
Correct |
94 ms |
14100 KB |
Output is correct |
99 |
Correct |
98 ms |
14104 KB |
Output is correct |
100 |
Correct |
85 ms |
14120 KB |
Output is correct |
101 |
Correct |
76 ms |
14028 KB |
Output is correct |
102 |
Correct |
70 ms |
14104 KB |
Output is correct |
103 |
Correct |
73 ms |
14028 KB |
Output is correct |
104 |
Correct |
78 ms |
14100 KB |
Output is correct |
105 |
Correct |
126 ms |
14116 KB |
Output is correct |