#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int construct(vector<vector<int>> p)
{
int n = p.size();
vector<bool> used(n, false);
vector<vector<int>> adjmatrix(n, vector<int> (n, 0));
for(int i = 0; i < n; i++)
{
if(used[i]) continue;
//Collect the component:
vector<int> component(0);
for(int j = 0; j < n; j++)
{
if(p[i][j] == 3)
{
return 0;
}
if(p[i][j])
{
component.push_back(j);
used[j] = true;
}
}
for(int j : component)
{
if(count(p[j].begin(), p[j].end(), 0) != n- (int) component.size())
{
return 0;
}
for(int k : component)
{
if(p[i][j] == 0)
{
return 0;
}
}
}
//Build the forest:
vector<bool> tree(component.size(), false);
vector<vector<int>> forest(0);
for(int j = 0; j < (int) component.size(); j++)
{
if(tree[j]) continue;
vector<int> t(0);
for(int k = 0; k < (int) component.size(); k++)
{
if(p[j][k] == 1)
{
t.push_back(k);
tree[k] = true;
}
}
forest.push_back(t);
//Build the tree:
for(int k = 0; k < (int) t.size()-1; k++)
{
adjmatrix[t[k]][t[k+1]] = 1;
adjmatrix[t[k+1]][t[k]] = 1;
}
}
//Test the partitioning:
for(int j = 0; j < (int) forest.size(); j++)
{
for(int a : forest[j])
{
for(int k = 0; k < (int) forest.size(); k++)
{
for(int b : forest[k])
{
if((k == j && p[a][b] != 1) ||
(k != j && p[a][b] != 2))
{
return 0;
}
}
}
}
}
//Connect the trees:
if(forest.size() == 2)
{
return 0;
}
if(forest.size() > 2)
{
for(int j = 0; j < (int) forest.size()-1; j++)
{
adjmatrix[forest[j][0]][forest[j+1][0]] = 1;
adjmatrix[forest[j+1][0]][forest[j][0]] = 1;
}
adjmatrix[forest[0][0]][forest[forest.size()-1][0]] = 1;
adjmatrix[forest[forest.size()-1][0]][forest[0][0]] = 1;
}
}
build(adjmatrix);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:36:12: warning: unused variable 'k' [-Wunused-variable]
36 | for(int k : component)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1188 KB |
Output is correct |
7 |
Correct |
159 ms |
21980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1188 KB |
Output is correct |
7 |
Correct |
159 ms |
21980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1124 KB |
Output is correct |
13 |
Correct |
184 ms |
22016 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
63 ms |
12048 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Incorrect |
17 ms |
3216 KB |
Answer gives possible 0 while actual possible 1 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
232 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1152 KB |
Output is correct |
9 |
Correct |
157 ms |
22036 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
167 ms |
22052 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
64 ms |
12040 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Incorrect |
16 ms |
3208 KB |
Answer gives possible 0 while actual possible 1 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Answer gives possible 0 while actual possible 1 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1188 KB |
Output is correct |
7 |
Correct |
159 ms |
21980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1124 KB |
Output is correct |
13 |
Correct |
184 ms |
22016 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
63 ms |
12048 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Incorrect |
17 ms |
3216 KB |
Answer gives possible 0 while actual possible 1 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1188 KB |
Output is correct |
7 |
Correct |
159 ms |
21980 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1124 KB |
Output is correct |
13 |
Correct |
184 ms |
22016 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
63 ms |
12048 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Incorrect |
17 ms |
3216 KB |
Answer gives possible 0 while actual possible 1 |
21 |
Halted |
0 ms |
0 KB |
- |