#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
bool is_add[N];
int tree_parent[N], circular_parent[N];
int find_tree_parent(int u) {
return tree_parent[u] = (u == tree_parent[u])
? u
: find_tree_parent(tree_parent[u]);
}
int find_circular_parent(int u) {
return circular_parent[u] = (u == circular_parent[u])
? u
: find_circular_parent(circular_parent[u]);
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector<vector<int>> ans(n, vector<int>(n));
for (int i=0 ; i<n ; i++) {
for (int j=0 ; j<n ; j++) {
if (p[i][j] == 3) {
return 0;
}
}
}
for (int i=0 ; i<n ; i++) {
tree_parent[i] = i;
circular_parent[i] = i;
}
for (int i=0 ; i<n ; i++) {
for (int j=i+1 ; j<n ; j++) {
if (p[i][j] == 1) {
int ii = find_tree_parent(i);
int jj = find_tree_parent(j);
if (ii != jj) {
tree_parent[ii] = jj;
ans[i][j] = ans[j][i] = 1;
}
}
}
}
for (int i=0 ; i<n ; i++) {
for (int j=i+1 ; j<n ; j++) {
int ii = find_tree_parent(i);
int jj = find_tree_parent(j);
if (p[i][j] != 1 && ii == jj) {
return 0;
}
}
}
for (int i=0 ; i<n ; i++) {
for (int j=i+1 ; j<n ; j++) {
if (p[i][j] == 2) {
int ii = find_circular_parent(find_tree_parent(i));
int jj = find_circular_parent(find_tree_parent(j));
circular_parent[ii] = jj;
}
}
}
vector<int> group[N];
for (int i=0 ; i<n ; i++) {
int tree_i = find_tree_parent(i);
if (is_add[tree_i]) continue;
is_add[tree_i] = true;
int circular_i = find_circular_parent(tree_i);
group[circular_i].push_back(tree_i);
}
for (int i=0 ; i<N ; i++) {
int sz = group[i].size();
if (sz <= 1) {
continue;
}
else if (sz == 2) {
return 0;
}
else {
for (int j=0 ; j<sz-1 ; j++) {
ans[group[i][j]][group[i][j+1]] = ans[group[i][j+1]][group[i][j]] = 1;
}
ans[group[i][0]][group[i][sz-1]] = ans[group[i][sz-1]][group[i][0]] = 1;
}
}
for (int i=0 ; i<n ; i++) {
for (int j=i+1 ; j<n ; j++) {
int circular_i = find_circular_parent(find_tree_parent(i));
int circular_j = find_circular_parent(find_tree_parent(j));
if (p[i][j] != 2 && circular_i == circular_j) {
return 0;
}
}
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
1388 KB |
Output is correct |
9 |
Correct |
123 ms |
24036 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1372 KB |
Output is correct |
13 |
Correct |
122 ms |
23992 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
65 ms |
14224 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
33 ms |
6268 KB |
Output is correct |
22 |
Correct |
122 ms |
23936 KB |
Output is correct |
23 |
Correct |
124 ms |
24148 KB |
Output is correct |
24 |
Correct |
134 ms |
23936 KB |
Output is correct |
25 |
Correct |
51 ms |
14160 KB |
Output is correct |
26 |
Correct |
63 ms |
14120 KB |
Output is correct |
27 |
Correct |
119 ms |
23944 KB |
Output is correct |
28 |
Correct |
135 ms |
24076 KB |
Output is correct |
29 |
Correct |
54 ms |
14220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 0 while actual possible 1 |
3 |
Halted |
0 ms |
0 KB |
- |