#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int pr[N];
int sz[N];
int get(int x){
if (pr[x] == x){
return x;
}
return (pr[x] = get(pr[x]));
}
void unit(int a, int b){
a = get(a);
b = get(b);
if (a != b){
if (sz[a] < sz[b]){
swap(a, b);
}
pr[b] = a;
sz[a] += sz[b];
}
}
int get2(int x, vector<int> &pr2){
if (pr2[x] == x){
return x;
}
return (pr2[x] = get(pr2[x]));
}
void unit2(int a, int b, vector <int> &pr2, vector <int> &sz2){
a = get2(a, pr2);
b = get2(b, pr2);
if (a != b){
if (sz2[a] < sz2[b]){
swap(a, b);
}
pr2[b] = a;
sz2[a] += sz2[b];
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++){
pr[i] = i; sz[i] = 1;
}
int check = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j]){
unit(i, j);
// check = max(check, p[i][j]);
}
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (p[i][j] == 0 and get(i) == get(j)){ //impossible
return 0;
}
}
}
vector <vector<int>> answer(n, vector<int>(n));
// if(!check){
// build(answer);
// return 1;
// }
for (int i = 0; i < n; i++){
vector <int> branch;
for (int j = 0; j < n; j++){
if (get(j) == i){
branch.push_back(j);
}
}
vector <int> pr2(branch.size()), sz2(branch.size(), 1);
for (int j = 0; j < branch.size(); j++){
pr2[j] = j;
}
for (int j = 0; j < branch.size(); j++){
for (int k = 0; k < branch.size(); k++){
if (p[branch[j]][branch[k]] == 1){
unit2(j, k, pr2, sz2);
}
}
}
for (int j = 0; j < branch.size(); j++){
for (int k = 0; k < branch.size(); k++){
if (p[branch[j]][branch[k]] == 2 and get2(j, pr2) == get2(k, pr2)){
return 0;
}
}
}
vector <vector <int>> forest(branch.size());
for (int i = 0; i < branch.size(); i++){
forest[get2(i, pr2)].push_back(branch[i]);
}
vector <int> q;
for (int j = 0; j < forest.size(); j++){
if (forest[j].size()){
q.push_back(forest[j][0]);
}
for (int k = 0; k + 1 < forest[j].size(); k++){
answer[forest[j][k]][forest[j][k + 1]] = 1;
answer[forest[j][k + 1]][forest[j][k]] = 1;
}
}
if (q.size() == 2){
return 0;
}
if (q.size() > 2){
for (int j = 0; j < q.size(); j++){
answer[q[j]][q[(j + 1)%q.size()]] = 1;
answer[q[(j + 1)%q.size()]][q[j]] = 1;
}
}
// for (int j = 0; j + 1 < branch.size(); j++){
// answer[branch[j]][branch[j + 1]] = 1;
// answer[branch[j + 1]][branch[j]] = 1;
// }
// if (check == 2){
// if(branch.size() == 2){
// return 0;
// }
// if (branch.size() > 2){
// answer[branch[0]][branch.back()] = 1;
// answer[branch.back()][branch[0]] = 1;
// }
// }
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int k = 0; k < branch.size(); k++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int j = 0; j < branch.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:87:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int k = 0; k < branch.size(); k++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < branch.size(); i++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:98:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int j = 0; j < forest.size(); j++){
| ~~^~~~~~~~~~~~~~~
supertrees.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int k = 0; k + 1 < forest[j].size(); k++){
| ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:111:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int j = 0; j < q.size(); j++){
| ~~^~~~~~~~~~
supertrees.cpp:47:6: warning: unused variable 'check' [-Wunused-variable]
47 | int check = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
164 ms |
22016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
164 ms |
22016 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 |
6 ms |
1108 KB |
Output is correct |
13 |
Correct |
157 ms |
22076 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 |
596 KB |
Output is correct |
17 |
Correct |
69 ms |
8180 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5720 KB |
Output is correct |
21 |
Correct |
157 ms |
22020 KB |
Output is correct |
22 |
Correct |
153 ms |
22032 KB |
Output is correct |
23 |
Correct |
168 ms |
22024 KB |
Output is correct |
24 |
Correct |
151 ms |
22024 KB |
Output is correct |
25 |
Correct |
59 ms |
8084 KB |
Output is correct |
26 |
Correct |
57 ms |
8068 KB |
Output is correct |
27 |
Correct |
163 ms |
21996 KB |
Output is correct |
28 |
Correct |
152 ms |
22020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 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 |
1108 KB |
Output is correct |
9 |
Correct |
152 ms |
22024 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 |
161 ms |
21984 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 |
596 KB |
Output is correct |
17 |
Correct |
75 ms |
8076 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
39 ms |
5768 KB |
Output is correct |
22 |
Correct |
174 ms |
22024 KB |
Output is correct |
23 |
Correct |
159 ms |
22036 KB |
Output is correct |
24 |
Correct |
165 ms |
22008 KB |
Output is correct |
25 |
Correct |
61 ms |
12052 KB |
Output is correct |
26 |
Correct |
58 ms |
8084 KB |
Output is correct |
27 |
Correct |
155 ms |
22032 KB |
Output is correct |
28 |
Correct |
164 ms |
22016 KB |
Output is correct |
29 |
Correct |
61 ms |
12108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
39 ms |
5664 KB |
Output is correct |
5 |
Correct |
160 ms |
22032 KB |
Output is correct |
6 |
Correct |
153 ms |
22036 KB |
Output is correct |
7 |
Correct |
163 ms |
22020 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
39 ms |
5708 KB |
Output is correct |
10 |
Correct |
157 ms |
22032 KB |
Output is correct |
11 |
Correct |
157 ms |
22020 KB |
Output is correct |
12 |
Correct |
161 ms |
22012 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
39 ms |
5744 KB |
Output is correct |
17 |
Correct |
156 ms |
22020 KB |
Output is correct |
18 |
Incorrect |
64 ms |
12108 KB |
Answer gives possible 0 while actual possible 1 |
19 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
164 ms |
22016 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 |
6 ms |
1108 KB |
Output is correct |
13 |
Correct |
157 ms |
22076 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 |
596 KB |
Output is correct |
17 |
Correct |
69 ms |
8180 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5720 KB |
Output is correct |
21 |
Correct |
157 ms |
22020 KB |
Output is correct |
22 |
Correct |
153 ms |
22032 KB |
Output is correct |
23 |
Correct |
168 ms |
22024 KB |
Output is correct |
24 |
Correct |
151 ms |
22024 KB |
Output is correct |
25 |
Correct |
59 ms |
8084 KB |
Output is correct |
26 |
Correct |
57 ms |
8068 KB |
Output is correct |
27 |
Correct |
163 ms |
21996 KB |
Output is correct |
28 |
Correct |
152 ms |
22020 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1108 KB |
Output is correct |
37 |
Correct |
152 ms |
22024 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1108 KB |
Output is correct |
41 |
Correct |
161 ms |
21984 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
75 ms |
8076 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5768 KB |
Output is correct |
50 |
Correct |
174 ms |
22024 KB |
Output is correct |
51 |
Correct |
159 ms |
22036 KB |
Output is correct |
52 |
Correct |
165 ms |
22008 KB |
Output is correct |
53 |
Correct |
61 ms |
12052 KB |
Output is correct |
54 |
Correct |
58 ms |
8084 KB |
Output is correct |
55 |
Correct |
155 ms |
22032 KB |
Output is correct |
56 |
Correct |
164 ms |
22016 KB |
Output is correct |
57 |
Correct |
61 ms |
12108 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
608 KB |
Output is correct |
61 |
Correct |
66 ms |
8068 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Incorrect |
16 ms |
3216 KB |
Answer gives possible 0 while actual possible 1 |
66 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
164 ms |
22016 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 |
6 ms |
1108 KB |
Output is correct |
13 |
Correct |
157 ms |
22076 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 |
596 KB |
Output is correct |
17 |
Correct |
69 ms |
8180 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
39 ms |
5720 KB |
Output is correct |
21 |
Correct |
157 ms |
22020 KB |
Output is correct |
22 |
Correct |
153 ms |
22032 KB |
Output is correct |
23 |
Correct |
168 ms |
22024 KB |
Output is correct |
24 |
Correct |
151 ms |
22024 KB |
Output is correct |
25 |
Correct |
59 ms |
8084 KB |
Output is correct |
26 |
Correct |
57 ms |
8068 KB |
Output is correct |
27 |
Correct |
163 ms |
21996 KB |
Output is correct |
28 |
Correct |
152 ms |
22020 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1108 KB |
Output is correct |
37 |
Correct |
152 ms |
22024 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1108 KB |
Output is correct |
41 |
Correct |
161 ms |
21984 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
75 ms |
8076 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
39 ms |
5768 KB |
Output is correct |
50 |
Correct |
174 ms |
22024 KB |
Output is correct |
51 |
Correct |
159 ms |
22036 KB |
Output is correct |
52 |
Correct |
165 ms |
22008 KB |
Output is correct |
53 |
Correct |
61 ms |
12052 KB |
Output is correct |
54 |
Correct |
58 ms |
8084 KB |
Output is correct |
55 |
Correct |
155 ms |
22032 KB |
Output is correct |
56 |
Correct |
164 ms |
22016 KB |
Output is correct |
57 |
Correct |
61 ms |
12108 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
39 ms |
5664 KB |
Output is correct |
62 |
Correct |
160 ms |
22032 KB |
Output is correct |
63 |
Correct |
153 ms |
22036 KB |
Output is correct |
64 |
Correct |
163 ms |
22020 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
39 ms |
5708 KB |
Output is correct |
67 |
Correct |
157 ms |
22032 KB |
Output is correct |
68 |
Correct |
157 ms |
22020 KB |
Output is correct |
69 |
Correct |
161 ms |
22012 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
39 ms |
5744 KB |
Output is correct |
74 |
Correct |
156 ms |
22020 KB |
Output is correct |
75 |
Incorrect |
64 ms |
12108 KB |
Answer gives possible 0 while actual possible 1 |
76 |
Halted |
0 ms |
0 KB |
- |