#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
int par[MAX];
int find_set(int v){
if(par[v] < 0) return v;
return par[v] = find_set(par[v]);
}
bool unite(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v) return false;
par[u] += par[v];
par[v] = u;
return true;
}
vector<int> g[MAX];
bool visited[MAX];
void dfs(int node, vector<int>& v){
v[node] += 1;
visited[node] = 1;
for(int& to:g[node]){
if(!visited[to]){
dfs(to, v);
}
}
visited[node] = 0;
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
vector<bool> except(n, 0);
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(p[i][j] == 1){
except[i] = 1;
ans[i][j] = 1;
ans[j][i] = 1;
break;
}
}
}
memset(par, -1, sizeof(par));
for (int i = 0; i < n; i++)
{
if(except[i]) continue;
for (int j = 0; j < n; j++)
{
if(except[j]) continue;
if(p[i][j] == 2){
unite(i, j);
}
}
}
vector<int> comps[MAX];
for (int i = 0; i < n; i++)
{
if(except[i]) continue;
comps[find_set(i)].push_back(i);
}
for(auto& v:comps){
for (int i = 0; i < v.size(); i++)
{
if(i != 0){
ans[v[i]][v[i - 1]] = 1;
}
if(i != v.size() - 1){
ans[v[i]][v[i + 1]] = 1;
}
}
if(v.size() > 2){
ans[v.front()][v.back()] = 1;
ans[v.back()][v.front()] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(ans[i][j]){
g[i].push_back(j);
}
}
}
for (int i = 0; i < n; i++)
{
vector<int> v(n, 0);
dfs(i, v);
if(v != p[i]) return 0;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:74:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if(i != v.size() - 1){
| ~~^~~~~~~~~~~~~~~
# |
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 |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
1292 KB |
Output is correct |
7 |
Correct |
195 ms |
24076 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 |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
1292 KB |
Output is correct |
7 |
Correct |
195 ms |
24076 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 |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
181 ms |
24004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
76 ms |
14108 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
6240 KB |
Output is correct |
21 |
Correct |
173 ms |
24012 KB |
Output is correct |
22 |
Correct |
176 ms |
24036 KB |
Output is correct |
23 |
Correct |
194 ms |
24148 KB |
Output is correct |
24 |
Correct |
173 ms |
24112 KB |
Output is correct |
25 |
Correct |
81 ms |
14108 KB |
Output is correct |
26 |
Correct |
72 ms |
14096 KB |
Output is correct |
27 |
Correct |
189 ms |
24056 KB |
Output is correct |
28 |
Correct |
185 ms |
24076 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 |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
8 ms |
1236 KB |
Output is correct |
9 |
Correct |
170 ms |
24120 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
9 ms |
1332 KB |
Output is correct |
13 |
Correct |
193 ms |
24088 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
14284 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 |
46 ms |
6288 KB |
Output is correct |
22 |
Correct |
173 ms |
24068 KB |
Output is correct |
23 |
Correct |
183 ms |
24020 KB |
Output is correct |
24 |
Correct |
189 ms |
24012 KB |
Output is correct |
25 |
Correct |
93 ms |
14156 KB |
Output is correct |
26 |
Correct |
84 ms |
14092 KB |
Output is correct |
27 |
Correct |
181 ms |
24020 KB |
Output is correct |
28 |
Correct |
200 ms |
24012 KB |
Output is correct |
29 |
Correct |
77 ms |
14104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
44 ms |
6240 KB |
Output is correct |
5 |
Correct |
168 ms |
23968 KB |
Output is correct |
6 |
Correct |
191 ms |
24080 KB |
Output is correct |
7 |
Correct |
195 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
49 ms |
6276 KB |
Output is correct |
10 |
Correct |
177 ms |
24064 KB |
Output is correct |
11 |
Correct |
181 ms |
23968 KB |
Output is correct |
12 |
Correct |
203 ms |
24096 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
43 ms |
6248 KB |
Output is correct |
17 |
Correct |
179 ms |
24028 KB |
Output is correct |
18 |
Correct |
202 ms |
24016 KB |
Output is correct |
19 |
Correct |
177 ms |
24012 KB |
Output is correct |
20 |
Correct |
179 ms |
24092 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 |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
1292 KB |
Output is correct |
7 |
Correct |
195 ms |
24076 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 |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
181 ms |
24004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
76 ms |
14108 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
6240 KB |
Output is correct |
21 |
Correct |
173 ms |
24012 KB |
Output is correct |
22 |
Correct |
176 ms |
24036 KB |
Output is correct |
23 |
Correct |
194 ms |
24148 KB |
Output is correct |
24 |
Correct |
173 ms |
24112 KB |
Output is correct |
25 |
Correct |
81 ms |
14108 KB |
Output is correct |
26 |
Correct |
72 ms |
14096 KB |
Output is correct |
27 |
Correct |
189 ms |
24056 KB |
Output is correct |
28 |
Correct |
185 ms |
24076 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
170 ms |
24120 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
324 KB |
Output is correct |
40 |
Correct |
9 ms |
1332 KB |
Output is correct |
41 |
Correct |
193 ms |
24088 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
82 ms |
14284 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 |
46 ms |
6288 KB |
Output is correct |
50 |
Correct |
173 ms |
24068 KB |
Output is correct |
51 |
Correct |
183 ms |
24020 KB |
Output is correct |
52 |
Correct |
189 ms |
24012 KB |
Output is correct |
53 |
Correct |
93 ms |
14156 KB |
Output is correct |
54 |
Correct |
84 ms |
14092 KB |
Output is correct |
55 |
Correct |
181 ms |
24020 KB |
Output is correct |
56 |
Correct |
200 ms |
24012 KB |
Output is correct |
57 |
Correct |
77 ms |
14104 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
80 ms |
14112 KB |
Output is correct |
62 |
Correct |
1 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 |
44 ms |
6220 KB |
Output is correct |
66 |
Correct |
98 ms |
14096 KB |
Output is correct |
67 |
Correct |
92 ms |
14156 KB |
Output is correct |
68 |
Correct |
81 ms |
14140 KB |
Output is correct |
69 |
Correct |
71 ms |
14064 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 |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
1292 KB |
Output is correct |
7 |
Correct |
195 ms |
24076 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 |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
181 ms |
24004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
76 ms |
14108 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
42 ms |
6240 KB |
Output is correct |
21 |
Correct |
173 ms |
24012 KB |
Output is correct |
22 |
Correct |
176 ms |
24036 KB |
Output is correct |
23 |
Correct |
194 ms |
24148 KB |
Output is correct |
24 |
Correct |
173 ms |
24112 KB |
Output is correct |
25 |
Correct |
81 ms |
14108 KB |
Output is correct |
26 |
Correct |
72 ms |
14096 KB |
Output is correct |
27 |
Correct |
189 ms |
24056 KB |
Output is correct |
28 |
Correct |
185 ms |
24076 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
170 ms |
24120 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
324 KB |
Output is correct |
40 |
Correct |
9 ms |
1332 KB |
Output is correct |
41 |
Correct |
193 ms |
24088 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
82 ms |
14284 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 |
46 ms |
6288 KB |
Output is correct |
50 |
Correct |
173 ms |
24068 KB |
Output is correct |
51 |
Correct |
183 ms |
24020 KB |
Output is correct |
52 |
Correct |
189 ms |
24012 KB |
Output is correct |
53 |
Correct |
93 ms |
14156 KB |
Output is correct |
54 |
Correct |
84 ms |
14092 KB |
Output is correct |
55 |
Correct |
181 ms |
24020 KB |
Output is correct |
56 |
Correct |
200 ms |
24012 KB |
Output is correct |
57 |
Correct |
77 ms |
14104 KB |
Output is correct |
58 |
Correct |
1 ms |
328 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
44 ms |
6240 KB |
Output is correct |
62 |
Correct |
168 ms |
23968 KB |
Output is correct |
63 |
Correct |
191 ms |
24080 KB |
Output is correct |
64 |
Correct |
195 ms |
24012 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
49 ms |
6276 KB |
Output is correct |
67 |
Correct |
177 ms |
24064 KB |
Output is correct |
68 |
Correct |
181 ms |
23968 KB |
Output is correct |
69 |
Correct |
203 ms |
24096 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
43 ms |
6248 KB |
Output is correct |
74 |
Correct |
179 ms |
24028 KB |
Output is correct |
75 |
Correct |
202 ms |
24016 KB |
Output is correct |
76 |
Correct |
177 ms |
24012 KB |
Output is correct |
77 |
Correct |
179 ms |
24092 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
80 ms |
14112 KB |
Output is correct |
82 |
Correct |
1 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 |
44 ms |
6220 KB |
Output is correct |
86 |
Correct |
98 ms |
14096 KB |
Output is correct |
87 |
Correct |
92 ms |
14156 KB |
Output is correct |
88 |
Correct |
81 ms |
14140 KB |
Output is correct |
89 |
Correct |
71 ms |
14064 KB |
Output is correct |
90 |
Correct |
1 ms |
324 KB |
Output is correct |
91 |
Correct |
1 ms |
340 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
76 ms |
14144 KB |
Output is correct |
94 |
Correct |
1 ms |
324 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
328 KB |
Output is correct |
97 |
Correct |
18 ms |
3736 KB |
Output is correct |
98 |
Correct |
76 ms |
14104 KB |
Output is correct |
99 |
Correct |
82 ms |
14272 KB |
Output is correct |
100 |
Correct |
85 ms |
14100 KB |
Output is correct |
101 |
Correct |
103 ms |
14096 KB |
Output is correct |
102 |
Correct |
76 ms |
14096 KB |
Output is correct |
103 |
Correct |
87 ms |
14220 KB |
Output is correct |
104 |
Correct |
98 ms |
14104 KB |
Output is correct |
105 |
Correct |
116 ms |
14156 KB |
Output is correct |