#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){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
10 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
10 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22484 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
192 ms |
22436 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
820 KB |
Output is correct |
17 |
Correct |
82 ms |
14112 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
49 ms |
6224 KB |
Output is correct |
21 |
Correct |
179 ms |
24080 KB |
Output is correct |
22 |
Correct |
175 ms |
23968 KB |
Output is correct |
23 |
Correct |
210 ms |
24092 KB |
Output is correct |
24 |
Correct |
181 ms |
24012 KB |
Output is correct |
25 |
Correct |
72 ms |
14156 KB |
Output is correct |
26 |
Correct |
73 ms |
14096 KB |
Output is correct |
27 |
Correct |
186 ms |
24008 KB |
Output is correct |
28 |
Correct |
172 ms |
24012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
178 ms |
24068 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
184 ms |
24092 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 |
852 KB |
Output is correct |
17 |
Correct |
83 ms |
14224 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 |
43 ms |
6224 KB |
Output is correct |
22 |
Correct |
174 ms |
24076 KB |
Output is correct |
23 |
Correct |
175 ms |
24092 KB |
Output is correct |
24 |
Correct |
205 ms |
24092 KB |
Output is correct |
25 |
Correct |
74 ms |
14100 KB |
Output is correct |
26 |
Correct |
74 ms |
14156 KB |
Output is correct |
27 |
Correct |
174 ms |
24076 KB |
Output is correct |
28 |
Correct |
190 ms |
24016 KB |
Output is correct |
29 |
Correct |
70 ms |
14156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
44 ms |
5932 KB |
Output is correct |
5 |
Correct |
182 ms |
22428 KB |
Output is correct |
6 |
Correct |
175 ms |
22344 KB |
Output is correct |
7 |
Correct |
186 ms |
22352 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
47 ms |
5880 KB |
Output is correct |
10 |
Correct |
180 ms |
22392 KB |
Output is correct |
11 |
Correct |
174 ms |
22356 KB |
Output is correct |
12 |
Correct |
192 ms |
22348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
50 ms |
5892 KB |
Output is correct |
17 |
Correct |
175 ms |
22400 KB |
Output is correct |
18 |
Correct |
173 ms |
22316 KB |
Output is correct |
19 |
Correct |
175 ms |
22348 KB |
Output is correct |
20 |
Correct |
171 ms |
22348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
10 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22484 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
192 ms |
22436 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
820 KB |
Output is correct |
17 |
Correct |
82 ms |
14112 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
49 ms |
6224 KB |
Output is correct |
21 |
Correct |
179 ms |
24080 KB |
Output is correct |
22 |
Correct |
175 ms |
23968 KB |
Output is correct |
23 |
Correct |
210 ms |
24092 KB |
Output is correct |
24 |
Correct |
181 ms |
24012 KB |
Output is correct |
25 |
Correct |
72 ms |
14156 KB |
Output is correct |
26 |
Correct |
73 ms |
14096 KB |
Output is correct |
27 |
Correct |
186 ms |
24008 KB |
Output is correct |
28 |
Correct |
172 ms |
24012 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
178 ms |
24068 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
324 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
184 ms |
24092 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
83 ms |
14224 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 |
43 ms |
6224 KB |
Output is correct |
50 |
Correct |
174 ms |
24076 KB |
Output is correct |
51 |
Correct |
175 ms |
24092 KB |
Output is correct |
52 |
Correct |
205 ms |
24092 KB |
Output is correct |
53 |
Correct |
74 ms |
14100 KB |
Output is correct |
54 |
Correct |
74 ms |
14156 KB |
Output is correct |
55 |
Correct |
174 ms |
24076 KB |
Output is correct |
56 |
Correct |
190 ms |
24016 KB |
Output is correct |
57 |
Correct |
70 ms |
14156 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
328 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
78 ms |
14152 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 |
45 ms |
6276 KB |
Output is correct |
66 |
Correct |
71 ms |
14092 KB |
Output is correct |
67 |
Correct |
74 ms |
14100 KB |
Output is correct |
68 |
Correct |
77 ms |
14156 KB |
Output is correct |
69 |
Correct |
72 ms |
14096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
10 ms |
1236 KB |
Output is correct |
7 |
Correct |
185 ms |
22484 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
192 ms |
22436 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
820 KB |
Output is correct |
17 |
Correct |
82 ms |
14112 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
49 ms |
6224 KB |
Output is correct |
21 |
Correct |
179 ms |
24080 KB |
Output is correct |
22 |
Correct |
175 ms |
23968 KB |
Output is correct |
23 |
Correct |
210 ms |
24092 KB |
Output is correct |
24 |
Correct |
181 ms |
24012 KB |
Output is correct |
25 |
Correct |
72 ms |
14156 KB |
Output is correct |
26 |
Correct |
73 ms |
14096 KB |
Output is correct |
27 |
Correct |
186 ms |
24008 KB |
Output is correct |
28 |
Correct |
172 ms |
24012 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
178 ms |
24068 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
324 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
184 ms |
24092 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
83 ms |
14224 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 |
43 ms |
6224 KB |
Output is correct |
50 |
Correct |
174 ms |
24076 KB |
Output is correct |
51 |
Correct |
175 ms |
24092 KB |
Output is correct |
52 |
Correct |
205 ms |
24092 KB |
Output is correct |
53 |
Correct |
74 ms |
14100 KB |
Output is correct |
54 |
Correct |
74 ms |
14156 KB |
Output is correct |
55 |
Correct |
174 ms |
24076 KB |
Output is correct |
56 |
Correct |
190 ms |
24016 KB |
Output is correct |
57 |
Correct |
70 ms |
14156 KB |
Output is correct |
58 |
Correct |
1 ms |
328 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
44 ms |
5932 KB |
Output is correct |
62 |
Correct |
182 ms |
22428 KB |
Output is correct |
63 |
Correct |
175 ms |
22344 KB |
Output is correct |
64 |
Correct |
186 ms |
22352 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
47 ms |
5880 KB |
Output is correct |
67 |
Correct |
180 ms |
22392 KB |
Output is correct |
68 |
Correct |
174 ms |
22356 KB |
Output is correct |
69 |
Correct |
192 ms |
22348 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
328 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
50 ms |
5892 KB |
Output is correct |
74 |
Correct |
175 ms |
22400 KB |
Output is correct |
75 |
Correct |
173 ms |
22316 KB |
Output is correct |
76 |
Correct |
175 ms |
22348 KB |
Output is correct |
77 |
Correct |
171 ms |
22348 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
328 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
78 ms |
14152 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 |
45 ms |
6276 KB |
Output is correct |
86 |
Correct |
71 ms |
14092 KB |
Output is correct |
87 |
Correct |
74 ms |
14100 KB |
Output is correct |
88 |
Correct |
77 ms |
14156 KB |
Output is correct |
89 |
Correct |
72 ms |
14096 KB |
Output is correct |
90 |
Correct |
1 ms |
340 KB |
Output is correct |
91 |
Correct |
1 ms |
340 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
72 ms |
14096 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
328 KB |
Output is correct |
97 |
Correct |
20 ms |
3832 KB |
Output is correct |
98 |
Correct |
74 ms |
14108 KB |
Output is correct |
99 |
Correct |
73 ms |
14232 KB |
Output is correct |
100 |
Correct |
83 ms |
14092 KB |
Output is correct |
101 |
Correct |
71 ms |
14164 KB |
Output is correct |
102 |
Correct |
78 ms |
14096 KB |
Output is correct |
103 |
Correct |
85 ms |
14156 KB |
Output is correct |
104 |
Correct |
72 ms |
14092 KB |
Output is correct |
105 |
Correct |
79 ms |
14160 KB |
Output is correct |