#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> lab;
int get_root(int u) {
return lab[u] == u ? u : lab[u] = get_root(lab[u]);
}
bool unite(int u, int v) {
u = get_root(u);
v = get_root(v);
if(u == v) return false;
lab[v] = u;
return true;
}
int construct(vector<vector<int>> p) {
int n = p.size();
lab.resize(n);
for(int i = 0; i < n; i ++) lab[i] = i;
vector<vector<int>> answer(n, vector<int> (n, 0));
for (int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(get_root(i) == get_root(j) || p[i][j] != 1) continue ;
answer[i][j] = answer[j][i] = 1;
unite(i, j);
}
}
for(int i = 0; i < n; i ++) {
if(get_root(i) != i) continue ;
vector<int> row;
for(int j = 0; j < n; j ++) {
if(p[i][j] != 2) continue ;
row.push_back(j);
}
if(row.size() == 0) continue ;
int root = i;
int k = i;
int cnt = 1;
for(int j = 0; j < row.size(); j ++) {
if(unite(k, row[j])) {
answer[row[j]][k] = 1;
answer[k][row[j]] = 1;
k = row[j];
cnt ++ ;
}
}
answer[root][k] = 1;
answer[k][root] = 1;
if(cnt <= 2) return 0;
}
for(int i = 0; i < n; i ++) {
if(get_root(i) != i) continue ;
vector<int> row;
for(int j = 0; j < n; j ++) {
if(p[i][j] != 3) continue ;
row.push_back(j);
}
if(row.size() == 0) continue ;
int root = i;
int k = i, prev_k = 0;
int cnt = 1;
for(int j = 0; j < row.size(); j ++) {
if(unite(k, row[j])) {
answer[j][k] = 1;
answer[k][j] = 1;
prev_k = k;
k = j;
cnt ++ ;
}
}
answer[root][k] = 1;
answer[k][root] = 1;
answer[root][prev_k] = 1;
answer[prev_k][root] = 1;
if(cnt <= 3) return 0;
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:54:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j = 0; j < row.size(); j ++) {
| ~~^~~~~~~~~~~~
supertrees.cpp:85:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j = 0; j < row.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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
152 ms |
22032 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
152 ms |
22032 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
156 ms |
22024 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
8 ms |
1372 KB |
Output is correct |
9 |
Correct |
155 ms |
23968 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
684 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
154 ms |
23932 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
39 ms |
6368 KB |
Output is correct |
5 |
Correct |
155 ms |
23892 KB |
Output is correct |
6 |
Correct |
154 ms |
24144 KB |
Output is correct |
7 |
Correct |
163 ms |
24164 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
38 ms |
6148 KB |
Output is correct |
10 |
Correct |
153 ms |
24144 KB |
Output is correct |
11 |
Correct |
150 ms |
24144 KB |
Output is correct |
12 |
Correct |
154 ms |
24148 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
38 ms |
6212 KB |
Output is correct |
17 |
Correct |
151 ms |
24056 KB |
Output is correct |
18 |
Correct |
156 ms |
23948 KB |
Output is correct |
19 |
Correct |
152 ms |
23928 KB |
Output is correct |
20 |
Correct |
164 ms |
24144 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
152 ms |
22032 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
156 ms |
22024 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
1116 KB |
Output is correct |
7 |
Correct |
152 ms |
22032 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
156 ms |
22024 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Answer gives possible 1 while actual possible 0 |
15 |
Halted |
0 ms |
0 KB |
- |