#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> ans;
struct DisjointSet{
int par[1005];
DisjointSet(){
for (int i=0; i<1005; i++) par[i] = i;
}
int root(int u){
if (par[u] == u) return u;
return par[u] = root(par[u]);
}
bool chk(int u, int v){
return root(u) == root(v);
}
void merge(int u, int v){
u = root(u); v = root(v);
if (u != v) par[u] = v;
}
} disj, uf;
const bool ST4 = false, ST5 = false;
bool chk[1005];
int construct(vector<vector<int>> p) {
int N = p.size();
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++) {
vector<int> row;
for (int j=0; j<N; j++) row.push_back(0);
ans.push_back(row);
}
{
bool subtask1 = true;
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] != 1){
subtask1 = false;
}
}
if (!subtask1) break;
}
if (subtask1){
for (int i=0; i<N-1; i++){
ans[i][i+1] = 1;
ans[i+1][i] = 1;
}
build(ans);
return 1;
}
}
{
bool subtask2 = true;
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] > 1){
subtask2 = false;
break;
}
}
}
if (subtask2){
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] && !disj.chk(i, j)){
disj.merge(i, j);
ans[i][j] = ans[j][i] = 1;
}
}
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if ((bool) p[i][j] != disj.chk(i, j)){
return 0;
}
}
}
build(ans);
return 1;
}
}
{
bool subtask3 = true;
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (i != j && p[i][j] % 2 == 1){
subtask3 = false;
break;
}
}
if (subtask3 == false) break;
}
if (subtask3){
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] > 0 && !disj.chk(i, j)){
disj.merge(i, j);
}
}
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] > 0 != disj.chk(i, j)){
return 0;
}
}
}
for (int i=0; i<N; i++){
if (disj.root(i) == i){
vector<int> C;
for (int j=0; j<N; j++){
if (disj.chk(i, j)) C.push_back(j);
}
int M = C.size();
if (M == 1) continue;
if (M == 2) return 0;
for (int j=0; j<M; j++){
int x = C[j];
int y = C[(j+1) % M];
ans[x][y] = ans[y][x] = 1;
}
}
}
build(ans);
return 1;
}
}
if (!ST4){
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] == 1 && !uf.chk(i, j)) uf.merge(i, j);
}
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] == 2 && !disj.chk(uf.root(i), uf.root(j))){
disj.merge(uf.root(i), uf.root(j));
}
}
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] == 0){
if (uf.chk(i, j) || disj.chk(uf.root(i), uf.root(j))) return 0;
}
if (p[i][j] == 1){
if (uf.root(i) != uf.root(j)) return 0;
//if (i != j && disj.chk(i, j)) return 0;
}
if (p[i][j] == 2){
if (uf.chk(i, j)) return 0;
if (!disj.chk(uf.root(i), uf.root(j))) return 0;
}
}
}
uf = DisjointSet();
disj = DisjointSet();
}
if (!ST5){
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] == 1 && !uf.chk(i, j)){
uf.merge(i, j);
}
}
}
for (int i=0; i<N; i++){
if (uf.root(i) == i){
vector<int> I;
for (int j=0; j<N; j++){
if (uf.chk(i, j)) I.push_back(j);
}
int M = I.size();
if (M == 1) continue;
for (int j=0; j<M-1; j++){
int x = I[j];
int y = I[j+1];
ans[x][y] = ans[y][x] = 1;
}
}
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (p[i][j] == 2 && !disj.chk(uf.root(i), uf.root(j))){
disj.merge(uf.root(i), uf.root(j));
}
}
}
for (int i=0; i<N; i++){
if (disj.root(i) == i){
vector<int> C;
for (int j=0; j<N; j++){
if (disj.chk(i, j)) C.push_back(j);
}
int M = C.size();
if (M == 2) return 0;
if (M <= 2) continue;
for (int j=0; j<M; j++){
int x = C[j];
int y = C[(j+1) % M];
ans[x][y] = ans[y][x] = 1;
}
}
}
build(ans);
return 1;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:109:18: warning: suggest parentheses around comparison in operand of '!=' [-Wparentheses]
109 | if (p[i][j] > 0 != disj.chk(i, j)){
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
257 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
135 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
64 ms |
5880 KB |
Output is correct |
21 |
Correct |
298 ms |
22112 KB |
Output is correct |
22 |
Correct |
256 ms |
22136 KB |
Output is correct |
23 |
Correct |
270 ms |
22232 KB |
Output is correct |
24 |
Correct |
259 ms |
22136 KB |
Output is correct |
25 |
Correct |
115 ms |
12152 KB |
Output is correct |
26 |
Correct |
117 ms |
12152 KB |
Output is correct |
27 |
Correct |
267 ms |
22136 KB |
Output is correct |
28 |
Correct |
257 ms |
22116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
252 ms |
22152 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
274 ms |
22176 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
7 ms |
768 KB |
Output is correct |
17 |
Correct |
132 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
66 ms |
5880 KB |
Output is correct |
22 |
Correct |
259 ms |
22144 KB |
Output is correct |
23 |
Correct |
258 ms |
22136 KB |
Output is correct |
24 |
Correct |
261 ms |
22136 KB |
Output is correct |
25 |
Correct |
120 ms |
12152 KB |
Output is correct |
26 |
Correct |
118 ms |
12152 KB |
Output is correct |
27 |
Correct |
263 ms |
22136 KB |
Output is correct |
28 |
Correct |
275 ms |
22228 KB |
Output is correct |
29 |
Correct |
125 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
64 ms |
5856 KB |
Output is correct |
5 |
Correct |
258 ms |
22136 KB |
Output is correct |
6 |
Correct |
260 ms |
22136 KB |
Output is correct |
7 |
Correct |
267 ms |
22136 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
65 ms |
5884 KB |
Output is correct |
10 |
Correct |
258 ms |
22136 KB |
Output is correct |
11 |
Correct |
257 ms |
22136 KB |
Output is correct |
12 |
Correct |
262 ms |
22136 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
68 ms |
5880 KB |
Output is correct |
17 |
Correct |
258 ms |
22136 KB |
Output is correct |
18 |
Correct |
281 ms |
22136 KB |
Output is correct |
19 |
Correct |
278 ms |
22136 KB |
Output is correct |
20 |
Correct |
269 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
257 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
135 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
64 ms |
5880 KB |
Output is correct |
21 |
Correct |
298 ms |
22112 KB |
Output is correct |
22 |
Correct |
256 ms |
22136 KB |
Output is correct |
23 |
Correct |
270 ms |
22232 KB |
Output is correct |
24 |
Correct |
259 ms |
22136 KB |
Output is correct |
25 |
Correct |
115 ms |
12152 KB |
Output is correct |
26 |
Correct |
117 ms |
12152 KB |
Output is correct |
27 |
Correct |
267 ms |
22136 KB |
Output is correct |
28 |
Correct |
257 ms |
22116 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
252 ms |
22152 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
274 ms |
22176 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
132 ms |
12152 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
66 ms |
5880 KB |
Output is correct |
50 |
Correct |
259 ms |
22144 KB |
Output is correct |
51 |
Correct |
258 ms |
22136 KB |
Output is correct |
52 |
Correct |
261 ms |
22136 KB |
Output is correct |
53 |
Correct |
120 ms |
12152 KB |
Output is correct |
54 |
Correct |
118 ms |
12152 KB |
Output is correct |
55 |
Correct |
263 ms |
22136 KB |
Output is correct |
56 |
Correct |
275 ms |
22228 KB |
Output is correct |
57 |
Correct |
125 ms |
12152 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
256 KB |
Output is correct |
60 |
Correct |
6 ms |
768 KB |
Output is correct |
61 |
Correct |
132 ms |
12132 KB |
Output is correct |
62 |
Correct |
0 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
256 KB |
Output is correct |
65 |
Correct |
69 ms |
5884 KB |
Output is correct |
66 |
Correct |
119 ms |
12152 KB |
Output is correct |
67 |
Correct |
121 ms |
12152 KB |
Output is correct |
68 |
Correct |
117 ms |
12156 KB |
Output is correct |
69 |
Correct |
130 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
257 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
135 ms |
12152 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
64 ms |
5880 KB |
Output is correct |
21 |
Correct |
298 ms |
22112 KB |
Output is correct |
22 |
Correct |
256 ms |
22136 KB |
Output is correct |
23 |
Correct |
270 ms |
22232 KB |
Output is correct |
24 |
Correct |
259 ms |
22136 KB |
Output is correct |
25 |
Correct |
115 ms |
12152 KB |
Output is correct |
26 |
Correct |
117 ms |
12152 KB |
Output is correct |
27 |
Correct |
267 ms |
22136 KB |
Output is correct |
28 |
Correct |
257 ms |
22116 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
252 ms |
22152 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
274 ms |
22176 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
132 ms |
12152 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
66 ms |
5880 KB |
Output is correct |
50 |
Correct |
259 ms |
22144 KB |
Output is correct |
51 |
Correct |
258 ms |
22136 KB |
Output is correct |
52 |
Correct |
261 ms |
22136 KB |
Output is correct |
53 |
Correct |
120 ms |
12152 KB |
Output is correct |
54 |
Correct |
118 ms |
12152 KB |
Output is correct |
55 |
Correct |
263 ms |
22136 KB |
Output is correct |
56 |
Correct |
275 ms |
22228 KB |
Output is correct |
57 |
Correct |
125 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
1 ms |
256 KB |
Output is correct |
61 |
Correct |
64 ms |
5856 KB |
Output is correct |
62 |
Correct |
258 ms |
22136 KB |
Output is correct |
63 |
Correct |
260 ms |
22136 KB |
Output is correct |
64 |
Correct |
267 ms |
22136 KB |
Output is correct |
65 |
Correct |
1 ms |
256 KB |
Output is correct |
66 |
Correct |
65 ms |
5884 KB |
Output is correct |
67 |
Correct |
258 ms |
22136 KB |
Output is correct |
68 |
Correct |
257 ms |
22136 KB |
Output is correct |
69 |
Correct |
262 ms |
22136 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
68 ms |
5880 KB |
Output is correct |
74 |
Correct |
258 ms |
22136 KB |
Output is correct |
75 |
Correct |
281 ms |
22136 KB |
Output is correct |
76 |
Correct |
278 ms |
22136 KB |
Output is correct |
77 |
Correct |
269 ms |
22264 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
1 ms |
256 KB |
Output is correct |
80 |
Correct |
6 ms |
768 KB |
Output is correct |
81 |
Correct |
132 ms |
12132 KB |
Output is correct |
82 |
Correct |
0 ms |
384 KB |
Output is correct |
83 |
Correct |
1 ms |
384 KB |
Output is correct |
84 |
Correct |
1 ms |
256 KB |
Output is correct |
85 |
Correct |
69 ms |
5884 KB |
Output is correct |
86 |
Correct |
119 ms |
12152 KB |
Output is correct |
87 |
Correct |
121 ms |
12152 KB |
Output is correct |
88 |
Correct |
117 ms |
12156 KB |
Output is correct |
89 |
Correct |
130 ms |
12152 KB |
Output is correct |
90 |
Correct |
1 ms |
256 KB |
Output is correct |
91 |
Correct |
0 ms |
256 KB |
Output is correct |
92 |
Correct |
5 ms |
640 KB |
Output is correct |
93 |
Correct |
110 ms |
8184 KB |
Output is correct |
94 |
Correct |
1 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
0 ms |
256 KB |
Output is correct |
97 |
Correct |
27 ms |
2296 KB |
Output is correct |
98 |
Correct |
107 ms |
8188 KB |
Output is correct |
99 |
Correct |
105 ms |
8184 KB |
Output is correct |
100 |
Correct |
107 ms |
8184 KB |
Output is correct |
101 |
Correct |
109 ms |
8312 KB |
Output is correct |
102 |
Correct |
110 ms |
8184 KB |
Output is correct |
103 |
Correct |
108 ms |
8184 KB |
Output is correct |
104 |
Correct |
108 ms |
8184 KB |
Output is correct |
105 |
Correct |
112 ms |
8160 KB |
Output is correct |