#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
vector < int > graph[maxn], graph2[maxn], comp;
int cap[maxn][maxn], spec[maxn];
int used[maxn], N;
vector<vector<int>> answer;
void dfs2(int v)
{
comp.push_back(v);
used[v] = 1;
for (int u : graph2[v])
{
if (!used[u])
dfs2(u);
}
}
void dfs(int v)
{
comp.push_back(v);
used[v] = 1;
for (int u : graph[v])
{
if (!used[u])
dfs(u);
}
}
bool solve_2_component()
{
/**for (int i = 0; i < comp.size(); i ++)
for (int j = 0; j < comp.size(); j ++)
if (cap[comp[i]][comp[j]] != 2)
return false;
if (comp.size() == 2)
return false;*/
if (comp.size() == 1)
return true;
for (int v : comp)
spec[v] = 1;
answer[comp[0]][comp.back()] = 1;
answer[comp.back()][comp[0]] = 1;
for (int i = 0; i < (int)(comp.size()) - 1; i ++)
{
answer[comp[i]][comp[i + 1]] = 1;
answer[comp[i + 1]][comp[i]] = 1;
}
/**for (int i = 0; i < comp.size(); i ++)
for (int j = 0; j < comp.size(); j ++)
cap[comp[i]][comp[j]] = 0;*/
return true;
}
int ways[maxn][maxn], vis[maxn];
vector < int > sp[maxn];
void reach(int v, int st)
{
ways[st][v] ++;
vis[v] = 1;
for (int u : sp[v])
{
if (!vis[u])
{
reach(u, st);
}
}
vis[v] = 0;
}
bool all2;
int nec[maxn];
bool solve_component()
{
for (int i = 0; i < comp.size(); i ++)
for (int j = 0; j < comp.size(); j ++)
if (cap[comp[i]][comp[j]] == 0 || (!all2 && cap[comp[i]][comp[j]] == 2))
return false;
memset(nec, 0, sizeof(nec));
for (int v : comp)
nec[v] = 1;
if (comp.size() != 1 && all2)
{
if (cap[comp[0]][comp[1]] == 2)
{
if (comp.size() == 2)
return false;
answer[comp[0]][comp.back()] = 1;
answer[comp.back()][comp[0]] = 1;
}
}
for (int i = 0; i < (int)(comp.size()) - 1; i ++)
{
answer[comp[i]][comp[i + 1]] = 1;
answer[comp[i + 1]][comp[i]] = 1;
}
for (int i = 1; i < comp.size(); i ++)
for (int j = 0; j < N; j ++)
{
if (cap[comp[i]][j] == 2)
{
cap[comp[i]][j] = 0;
cap[j][comp[i]] = 0;
}
}
return true;
}
int construct(std::vector<std::vector<int>> p)
{
int n = p.size();
N = n;
all2 = true;
for (int i = 0; i < n; i++)
{
std::vector<int> row;
row.resize(n, 0);
answer.push_back(row);
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
{
if (p[i][j] == 1 && i != j)
all2 = false;
cap[i][j] = p[i][j];
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
{
if (i != j && (cap[i][j] == 1 || (cap[i][j] == 2 && all2)))
graph[i].push_back(j);
}
for (int i = 0; i < n; i ++)
{
if (!used[i])
{
comp.clear();
dfs(i);
bool is_fine = solve_component();
if (!is_fine)
return 0;
}
}
if (!all2)
{
memset(used, 0, sizeof(used));
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
{
if (i != j && cap[i][j] == 2)
graph2[i].push_back(j);
}
for (int i = 0; i < n; i ++)
{
if (!used[i])
{
comp.clear();
dfs2(i);
bool is_fine = solve_2_component();
if (!is_fine)
return false;
}
}
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
if (answer[i][j])
sp[i].push_back(j);
for (int i = 0; i < n; i ++)
reach(i, i);
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
if (p[i][j] != ways[i][j])
return false;
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'bool solve_component()':
supertrees.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i < comp.size(); i ++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int j = 0; j < comp.size(); j ++)
| ~~^~~~~~~~~~~~~
supertrees.cpp:106:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int i = 1; i < comp.size(); i ++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
468 KB |
Output is correct |
6 |
Correct |
10 ms |
3028 KB |
Output is correct |
7 |
Correct |
192 ms |
34144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
468 KB |
Output is correct |
6 |
Correct |
10 ms |
3028 KB |
Output is correct |
7 |
Correct |
192 ms |
34144 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
2772 KB |
Output is correct |
13 |
Correct |
179 ms |
30048 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1748 KB |
Output is correct |
17 |
Correct |
96 ms |
18656 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
47 ms |
9876 KB |
Output is correct |
21 |
Correct |
185 ms |
30564 KB |
Output is correct |
22 |
Correct |
180 ms |
30172 KB |
Output is correct |
23 |
Correct |
197 ms |
32096 KB |
Output is correct |
24 |
Correct |
175 ms |
29980 KB |
Output is correct |
25 |
Correct |
84 ms |
16612 KB |
Output is correct |
26 |
Correct |
82 ms |
16104 KB |
Output is correct |
27 |
Correct |
200 ms |
33064 KB |
Output is correct |
28 |
Correct |
180 ms |
30060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
468 KB |
Output is correct |
8 |
Correct |
12 ms |
2860 KB |
Output is correct |
9 |
Correct |
183 ms |
30060 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
10 ms |
3024 KB |
Output is correct |
13 |
Correct |
207 ms |
34124 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1748 KB |
Output is correct |
17 |
Correct |
94 ms |
18652 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
50 ms |
9836 KB |
Output is correct |
22 |
Correct |
185 ms |
30560 KB |
Output is correct |
23 |
Correct |
182 ms |
30200 KB |
Output is correct |
24 |
Correct |
199 ms |
33136 KB |
Output is correct |
25 |
Correct |
81 ms |
16112 KB |
Output is correct |
26 |
Correct |
83 ms |
16604 KB |
Output is correct |
27 |
Correct |
178 ms |
30048 KB |
Output is correct |
28 |
Correct |
206 ms |
32076 KB |
Output is correct |
29 |
Correct |
77 ms |
16112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
53 ms |
9808 KB |
Output is correct |
5 |
Correct |
188 ms |
30516 KB |
Output is correct |
6 |
Correct |
181 ms |
30028 KB |
Output is correct |
7 |
Correct |
202 ms |
32092 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
49 ms |
9760 KB |
Output is correct |
10 |
Correct |
188 ms |
30624 KB |
Output is correct |
11 |
Correct |
182 ms |
30080 KB |
Output is correct |
12 |
Correct |
213 ms |
33076 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
472 KB |
Output is correct |
16 |
Correct |
47 ms |
9824 KB |
Output is correct |
17 |
Correct |
186 ms |
30572 KB |
Output is correct |
18 |
Correct |
186 ms |
30300 KB |
Output is correct |
19 |
Correct |
186 ms |
30292 KB |
Output is correct |
20 |
Correct |
183 ms |
30188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
468 KB |
Output is correct |
6 |
Correct |
10 ms |
3028 KB |
Output is correct |
7 |
Correct |
192 ms |
34144 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
2772 KB |
Output is correct |
13 |
Correct |
179 ms |
30048 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1748 KB |
Output is correct |
17 |
Correct |
96 ms |
18656 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
47 ms |
9876 KB |
Output is correct |
21 |
Correct |
185 ms |
30564 KB |
Output is correct |
22 |
Correct |
180 ms |
30172 KB |
Output is correct |
23 |
Correct |
197 ms |
32096 KB |
Output is correct |
24 |
Correct |
175 ms |
29980 KB |
Output is correct |
25 |
Correct |
84 ms |
16612 KB |
Output is correct |
26 |
Correct |
82 ms |
16104 KB |
Output is correct |
27 |
Correct |
200 ms |
33064 KB |
Output is correct |
28 |
Correct |
180 ms |
30060 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 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 |
468 KB |
Output is correct |
36 |
Correct |
12 ms |
2860 KB |
Output is correct |
37 |
Correct |
183 ms |
30060 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
10 ms |
3024 KB |
Output is correct |
41 |
Correct |
207 ms |
34124 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
1748 KB |
Output is correct |
45 |
Correct |
94 ms |
18652 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
50 ms |
9836 KB |
Output is correct |
50 |
Correct |
185 ms |
30560 KB |
Output is correct |
51 |
Correct |
182 ms |
30200 KB |
Output is correct |
52 |
Correct |
199 ms |
33136 KB |
Output is correct |
53 |
Correct |
81 ms |
16112 KB |
Output is correct |
54 |
Correct |
83 ms |
16604 KB |
Output is correct |
55 |
Correct |
178 ms |
30048 KB |
Output is correct |
56 |
Correct |
206 ms |
32076 KB |
Output is correct |
57 |
Correct |
77 ms |
16112 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
5 ms |
1748 KB |
Output is correct |
61 |
Correct |
95 ms |
18288 KB |
Output is correct |
62 |
Correct |
1 ms |
388 KB |
Output is correct |
63 |
Correct |
1 ms |
392 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
47 ms |
9892 KB |
Output is correct |
66 |
Correct |
93 ms |
22604 KB |
Output is correct |
67 |
Correct |
86 ms |
18132 KB |
Output is correct |
68 |
Correct |
93 ms |
22240 KB |
Output is correct |
69 |
Correct |
89 ms |
21988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
468 KB |
Output is correct |
6 |
Correct |
10 ms |
3028 KB |
Output is correct |
7 |
Correct |
192 ms |
34144 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
2772 KB |
Output is correct |
13 |
Correct |
179 ms |
30048 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1748 KB |
Output is correct |
17 |
Correct |
96 ms |
18656 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
47 ms |
9876 KB |
Output is correct |
21 |
Correct |
185 ms |
30564 KB |
Output is correct |
22 |
Correct |
180 ms |
30172 KB |
Output is correct |
23 |
Correct |
197 ms |
32096 KB |
Output is correct |
24 |
Correct |
175 ms |
29980 KB |
Output is correct |
25 |
Correct |
84 ms |
16612 KB |
Output is correct |
26 |
Correct |
82 ms |
16104 KB |
Output is correct |
27 |
Correct |
200 ms |
33064 KB |
Output is correct |
28 |
Correct |
180 ms |
30060 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 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 |
468 KB |
Output is correct |
36 |
Correct |
12 ms |
2860 KB |
Output is correct |
37 |
Correct |
183 ms |
30060 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
10 ms |
3024 KB |
Output is correct |
41 |
Correct |
207 ms |
34124 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
1748 KB |
Output is correct |
45 |
Correct |
94 ms |
18652 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
50 ms |
9836 KB |
Output is correct |
50 |
Correct |
185 ms |
30560 KB |
Output is correct |
51 |
Correct |
182 ms |
30200 KB |
Output is correct |
52 |
Correct |
199 ms |
33136 KB |
Output is correct |
53 |
Correct |
81 ms |
16112 KB |
Output is correct |
54 |
Correct |
83 ms |
16604 KB |
Output is correct |
55 |
Correct |
178 ms |
30048 KB |
Output is correct |
56 |
Correct |
206 ms |
32076 KB |
Output is correct |
57 |
Correct |
77 ms |
16112 KB |
Output is correct |
58 |
Correct |
1 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 |
53 ms |
9808 KB |
Output is correct |
62 |
Correct |
188 ms |
30516 KB |
Output is correct |
63 |
Correct |
181 ms |
30028 KB |
Output is correct |
64 |
Correct |
202 ms |
32092 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
49 ms |
9760 KB |
Output is correct |
67 |
Correct |
188 ms |
30624 KB |
Output is correct |
68 |
Correct |
182 ms |
30080 KB |
Output is correct |
69 |
Correct |
213 ms |
33076 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
472 KB |
Output is correct |
73 |
Correct |
47 ms |
9824 KB |
Output is correct |
74 |
Correct |
186 ms |
30572 KB |
Output is correct |
75 |
Correct |
186 ms |
30300 KB |
Output is correct |
76 |
Correct |
186 ms |
30292 KB |
Output is correct |
77 |
Correct |
183 ms |
30188 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
5 ms |
1748 KB |
Output is correct |
81 |
Correct |
95 ms |
18288 KB |
Output is correct |
82 |
Correct |
1 ms |
388 KB |
Output is correct |
83 |
Correct |
1 ms |
392 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
47 ms |
9892 KB |
Output is correct |
86 |
Correct |
93 ms |
22604 KB |
Output is correct |
87 |
Correct |
86 ms |
18132 KB |
Output is correct |
88 |
Correct |
93 ms |
22240 KB |
Output is correct |
89 |
Correct |
89 ms |
21988 KB |
Output is correct |
90 |
Correct |
1 ms |
340 KB |
Output is correct |
91 |
Correct |
1 ms |
396 KB |
Output is correct |
92 |
Correct |
5 ms |
1748 KB |
Output is correct |
93 |
Correct |
93 ms |
19400 KB |
Output is correct |
94 |
Correct |
1 ms |
392 KB |
Output is correct |
95 |
Correct |
1 ms |
468 KB |
Output is correct |
96 |
Correct |
1 ms |
392 KB |
Output is correct |
97 |
Correct |
24 ms |
7832 KB |
Output is correct |
98 |
Correct |
95 ms |
22616 KB |
Output is correct |
99 |
Correct |
94 ms |
22348 KB |
Output is correct |
100 |
Correct |
87 ms |
22220 KB |
Output is correct |
101 |
Correct |
84 ms |
21976 KB |
Output is correct |
102 |
Correct |
88 ms |
22096 KB |
Output is correct |
103 |
Correct |
126 ms |
24544 KB |
Output is correct |
104 |
Correct |
111 ms |
24528 KB |
Output is correct |
105 |
Correct |
91 ms |
22092 KB |
Output is correct |