#include "supertrees.h"
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const ll N = 1005;
ll dsu[N];
ll repr(ll x)
{
return dsu[x] < 0 ? x : dsu[x] = repr(dsu[x]);
}
bool Union(ll x, ll y)
{
x = repr(x), y = repr(y);
if (x == y)
return false;
if (dsu[x] < dsu[y])
swap(x, y);
dsu[y] += dsu[x];
dsu[x] = y;
return true;
}
vector<int> g[N];
ll cnt[N][N];
bool used[N];
void dfs(ll v, ll st)
{
cnt[v][st]++;
used[v] = 1;
for (ll to : g[v])
if (!used[to])
dfs(to, st);
used[v] = 0;
}
int construct(vector<vector<int>> p)
{
ll n = p.size();
vector<vector<int>> ans(n, vector<int>(n, 0));
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
if (p[i][j] == 3)
return false;
for (ll i = 0; i < n; i++)
dsu[i] = -1;
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
if (p[i][j] == 1)
Union(i, j);
vector<int> v[n];
for (ll i = 0; i < n; i++)
v[repr(i)].push_back(i);
vector<int> check;
for (ll i = 0; i < n; i++)
{
if (!v[i].empty())
check.push_back(i);
for (ll j = 0; j + 1 < v[i].size(); j++)
ans[v[i][j]][v[i][j + 1]] = ans[v[i][j + 1]][v[i][j]] = 1, g[v[i][j]].push_back(v[i][j + 1]), g[v[i][j + 1]].push_back(v[i][j]);
}
for (ll i = 0; i < n; i++)
dsu[i] = -1;
for (ll i = 0; i < check.size(); i++)
for (ll j = 0; j < check.size(); j++)
if (p[check[i]][check[j]] == 2)
Union(check[i], check[j]);
for (ll i = 0; i < n; i++)
v[i].clear();
for (ll i = 0; i < n; i++)
v[repr(i)].push_back(i);
for (ll i = 0; i < n; i++)
if (v[i].size() > 2)
{
for (ll j = 0; j < v[i].size(); j++)
ans[v[i][j]][v[i][(j + 1) % v[i].size()]] = ans[v[i][(j + 1) % v[i].size()]][v[i][j]] = 1, g[v[i][j]].push_back(v[i][(j + 1) % v[i].size()]), g[v[i][(j + 1) % v[i].size()]].push_back(v[i][j]);
}
for (ll i = 0; i < n; i++)
dfs(i, i);
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
if (cnt[i][j] != p[i][j])
return false;
build(ans);
return true;
}
/*
*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:57:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (ll j = 0; j + 1 < v[i].size(); j++)
| ~~~~~~^~~~~~~~~~~~~
supertrees.cpp:62:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (ll i = 0; i < check.size(); i++)
| ~~^~~~~~~~~~~~~~
supertrees.cpp:63:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (ll j = 0; j < check.size(); j++)
| ~~^~~~~~~~~~~~~~
supertrees.cpp:73:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (ll j = 0; j < v[i].size(); j++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
7 ms |
3420 KB |
Output is correct |
7 |
Correct |
163 ms |
30032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
7 ms |
3420 KB |
Output is correct |
7 |
Correct |
163 ms |
30032 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
3420 KB |
Output is correct |
13 |
Correct |
164 ms |
29128 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
3048 KB |
Output is correct |
17 |
Correct |
83 ms |
20304 KB |
Output is correct |
18 |
Correct |
1 ms |
548 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
10092 KB |
Output is correct |
21 |
Correct |
155 ms |
30136 KB |
Output is correct |
22 |
Correct |
165 ms |
29844 KB |
Output is correct |
23 |
Correct |
165 ms |
30016 KB |
Output is correct |
24 |
Correct |
159 ms |
29172 KB |
Output is correct |
25 |
Correct |
64 ms |
20048 KB |
Output is correct |
26 |
Correct |
62 ms |
19768 KB |
Output is correct |
27 |
Correct |
171 ms |
29996 KB |
Output is correct |
28 |
Correct |
156 ms |
29116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
3420 KB |
Output is correct |
9 |
Correct |
166 ms |
31152 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
8 ms |
3416 KB |
Output is correct |
13 |
Correct |
171 ms |
32084 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
3084 KB |
Output is correct |
17 |
Correct |
90 ms |
22184 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
39 ms |
10652 KB |
Output is correct |
22 |
Correct |
156 ms |
32084 KB |
Output is correct |
23 |
Correct |
155 ms |
32080 KB |
Output is correct |
24 |
Correct |
168 ms |
32080 KB |
Output is correct |
25 |
Correct |
63 ms |
21332 KB |
Output is correct |
26 |
Correct |
72 ms |
22360 KB |
Output is correct |
27 |
Correct |
153 ms |
31960 KB |
Output is correct |
28 |
Correct |
169 ms |
32080 KB |
Output is correct |
29 |
Correct |
63 ms |
21328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
39 ms |
10536 KB |
Output is correct |
5 |
Correct |
155 ms |
32044 KB |
Output is correct |
6 |
Correct |
157 ms |
31824 KB |
Output is correct |
7 |
Correct |
165 ms |
31916 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
41 ms |
10540 KB |
Output is correct |
10 |
Correct |
188 ms |
31948 KB |
Output is correct |
11 |
Correct |
179 ms |
31824 KB |
Output is correct |
12 |
Correct |
168 ms |
32080 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
40 ms |
10544 KB |
Output is correct |
17 |
Correct |
167 ms |
32108 KB |
Output is correct |
18 |
Correct |
165 ms |
32048 KB |
Output is correct |
19 |
Correct |
174 ms |
32044 KB |
Output is correct |
20 |
Correct |
154 ms |
31252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
7 ms |
3420 KB |
Output is correct |
7 |
Correct |
163 ms |
30032 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
3420 KB |
Output is correct |
13 |
Correct |
164 ms |
29128 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
3048 KB |
Output is correct |
17 |
Correct |
83 ms |
20304 KB |
Output is correct |
18 |
Correct |
1 ms |
548 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
10092 KB |
Output is correct |
21 |
Correct |
155 ms |
30136 KB |
Output is correct |
22 |
Correct |
165 ms |
29844 KB |
Output is correct |
23 |
Correct |
165 ms |
30016 KB |
Output is correct |
24 |
Correct |
159 ms |
29172 KB |
Output is correct |
25 |
Correct |
64 ms |
20048 KB |
Output is correct |
26 |
Correct |
62 ms |
19768 KB |
Output is correct |
27 |
Correct |
171 ms |
29996 KB |
Output is correct |
28 |
Correct |
156 ms |
29116 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
3420 KB |
Output is correct |
37 |
Correct |
166 ms |
31152 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
3416 KB |
Output is correct |
41 |
Correct |
171 ms |
32084 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
5 ms |
3084 KB |
Output is correct |
45 |
Correct |
90 ms |
22184 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
39 ms |
10652 KB |
Output is correct |
50 |
Correct |
156 ms |
32084 KB |
Output is correct |
51 |
Correct |
155 ms |
32080 KB |
Output is correct |
52 |
Correct |
168 ms |
32080 KB |
Output is correct |
53 |
Correct |
63 ms |
21332 KB |
Output is correct |
54 |
Correct |
72 ms |
22360 KB |
Output is correct |
55 |
Correct |
153 ms |
31960 KB |
Output is correct |
56 |
Correct |
169 ms |
32080 KB |
Output is correct |
57 |
Correct |
63 ms |
21328 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
4 ms |
3016 KB |
Output is correct |
61 |
Correct |
81 ms |
22176 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
468 KB |
Output is correct |
64 |
Correct |
1 ms |
344 KB |
Output is correct |
65 |
Correct |
49 ms |
10576 KB |
Output is correct |
66 |
Correct |
66 ms |
22184 KB |
Output is correct |
67 |
Correct |
65 ms |
22056 KB |
Output is correct |
68 |
Correct |
65 ms |
22048 KB |
Output is correct |
69 |
Correct |
63 ms |
21284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
7 ms |
3420 KB |
Output is correct |
7 |
Correct |
163 ms |
30032 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
3420 KB |
Output is correct |
13 |
Correct |
164 ms |
29128 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
3048 KB |
Output is correct |
17 |
Correct |
83 ms |
20304 KB |
Output is correct |
18 |
Correct |
1 ms |
548 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
10092 KB |
Output is correct |
21 |
Correct |
155 ms |
30136 KB |
Output is correct |
22 |
Correct |
165 ms |
29844 KB |
Output is correct |
23 |
Correct |
165 ms |
30016 KB |
Output is correct |
24 |
Correct |
159 ms |
29172 KB |
Output is correct |
25 |
Correct |
64 ms |
20048 KB |
Output is correct |
26 |
Correct |
62 ms |
19768 KB |
Output is correct |
27 |
Correct |
171 ms |
29996 KB |
Output is correct |
28 |
Correct |
156 ms |
29116 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
3420 KB |
Output is correct |
37 |
Correct |
166 ms |
31152 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
3416 KB |
Output is correct |
41 |
Correct |
171 ms |
32084 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
5 ms |
3084 KB |
Output is correct |
45 |
Correct |
90 ms |
22184 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
39 ms |
10652 KB |
Output is correct |
50 |
Correct |
156 ms |
32084 KB |
Output is correct |
51 |
Correct |
155 ms |
32080 KB |
Output is correct |
52 |
Correct |
168 ms |
32080 KB |
Output is correct |
53 |
Correct |
63 ms |
21332 KB |
Output is correct |
54 |
Correct |
72 ms |
22360 KB |
Output is correct |
55 |
Correct |
153 ms |
31960 KB |
Output is correct |
56 |
Correct |
169 ms |
32080 KB |
Output is correct |
57 |
Correct |
63 ms |
21328 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
600 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
39 ms |
10536 KB |
Output is correct |
62 |
Correct |
155 ms |
32044 KB |
Output is correct |
63 |
Correct |
157 ms |
31824 KB |
Output is correct |
64 |
Correct |
165 ms |
31916 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
41 ms |
10540 KB |
Output is correct |
67 |
Correct |
188 ms |
31948 KB |
Output is correct |
68 |
Correct |
179 ms |
31824 KB |
Output is correct |
69 |
Correct |
168 ms |
32080 KB |
Output is correct |
70 |
Correct |
1 ms |
604 KB |
Output is correct |
71 |
Correct |
0 ms |
472 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
40 ms |
10544 KB |
Output is correct |
74 |
Correct |
167 ms |
32108 KB |
Output is correct |
75 |
Correct |
165 ms |
32048 KB |
Output is correct |
76 |
Correct |
174 ms |
32044 KB |
Output is correct |
77 |
Correct |
154 ms |
31252 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
344 KB |
Output is correct |
80 |
Correct |
4 ms |
3016 KB |
Output is correct |
81 |
Correct |
81 ms |
22176 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
344 KB |
Output is correct |
85 |
Correct |
49 ms |
10576 KB |
Output is correct |
86 |
Correct |
66 ms |
22184 KB |
Output is correct |
87 |
Correct |
65 ms |
22056 KB |
Output is correct |
88 |
Correct |
65 ms |
22048 KB |
Output is correct |
89 |
Correct |
63 ms |
21284 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
468 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
61 ms |
14108 KB |
Output is correct |
94 |
Correct |
0 ms |
344 KB |
Output is correct |
95 |
Correct |
1 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
15 ms |
3932 KB |
Output is correct |
98 |
Correct |
69 ms |
14248 KB |
Output is correct |
99 |
Correct |
79 ms |
14116 KB |
Output is correct |
100 |
Correct |
60 ms |
14252 KB |
Output is correct |
101 |
Correct |
62 ms |
14260 KB |
Output is correct |
102 |
Correct |
91 ms |
14164 KB |
Output is correct |
103 |
Correct |
67 ms |
14164 KB |
Output is correct |
104 |
Correct |
59 ms |
14160 KB |
Output is correct |
105 |
Correct |
70 ms |
14164 KB |
Output is correct |