#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
struct unionFind{
int par[1002];
void init(int n){
for(int i=0; i<n; i++) par[i] = i;
}
int find(int x){
if(x == par[x]) return x;
return par[x] = find(par[x]);
}
void merge(int x, int y){
x = find(x), y = find(y);
par[x] = y;
}
} uf1, uf2;
int n;
int mat[1002][1002];
vector<vector<int> > ans;
vector<int> degreeTwos[1002];
bool inCycle[1002];
int oneRoot[1002];
int construct(vector<vector<int>> _p){
int n = _p.size();
uf1.init(n);
uf2.init(n);
for(int i=0; i<n; i++) oneRoot[i] = -1;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
mat[i][j] = _p[i][j];
if(mat[i][j] == 3) return 0;
if(mat[i][j] == 1) uf1.merge(i, j);
if(mat[i][j] == 1 || mat[i][j] == 2) uf2.merge(i, j);
}
}
ans.resize(n);
for(int i=0; i<n; i++) ans[i].resize(n);
/// 트리 만들기
for(int i=0; i<n; i++){
if(i != uf1.find(i)) continue;
for(int j=0; j<n; j++){
if(i == uf1.find(j) && j != i) ans[i][j] = ans[j][i] = 1;
}
degreeTwos[uf2.find(i)].push_back(i);
}
/// 사이클 만들기
for(int i=0; i<n; i++){
if(i != uf2.find(i) || (int)degreeTwos[i].size() <= 1) continue;
if((int)degreeTwos[i].size() == 2) return 0;
for(int j = 0; j < (int)degreeTwos[i].size() - 1; j++){
ans[degreeTwos[i][j]][degreeTwos[i][j+1]] = 1;
ans[degreeTwos[i][j+1]][degreeTwos[i][j]] = 1;
}
ans[degreeTwos[i].front()][degreeTwos[i].back()] = 1;
ans[degreeTwos[i].back()][degreeTwos[i].front()] = 1;
}
/// 올바른지 확인하기
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(uf1.find(i) == uf1.find(j)){
if(mat[i][j] != 1) return 0;
}
else if(uf2.find(i) == uf2.find(j)){
if(mat[i][j] != 2) return 0;
}
else{
if(mat[i][j] != 0) return 0;
}
}
}
build(ans);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
1996 KB |
Output is correct |
7 |
Correct |
239 ms |
25904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
1996 KB |
Output is correct |
7 |
Correct |
239 ms |
25904 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
14 ms |
2040 KB |
Output is correct |
13 |
Correct |
227 ms |
26548 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
6 ms |
1600 KB |
Output is correct |
17 |
Correct |
117 ms |
16612 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
60 ms |
8132 KB |
Output is correct |
21 |
Correct |
235 ms |
26472 KB |
Output is correct |
22 |
Correct |
274 ms |
26492 KB |
Output is correct |
23 |
Correct |
247 ms |
26564 KB |
Output is correct |
24 |
Correct |
262 ms |
26612 KB |
Output is correct |
25 |
Correct |
101 ms |
16580 KB |
Output is correct |
26 |
Correct |
96 ms |
16612 KB |
Output is correct |
27 |
Correct |
251 ms |
26580 KB |
Output is correct |
28 |
Correct |
235 ms |
26520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
11 ms |
2096 KB |
Output is correct |
9 |
Correct |
228 ms |
26600 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
1996 KB |
Output is correct |
13 |
Correct |
271 ms |
26564 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
1592 KB |
Output is correct |
17 |
Correct |
125 ms |
16672 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
60 ms |
8208 KB |
Output is correct |
22 |
Correct |
249 ms |
26632 KB |
Output is correct |
23 |
Correct |
246 ms |
26596 KB |
Output is correct |
24 |
Correct |
295 ms |
26552 KB |
Output is correct |
25 |
Correct |
95 ms |
16584 KB |
Output is correct |
26 |
Correct |
105 ms |
16608 KB |
Output is correct |
27 |
Correct |
240 ms |
26492 KB |
Output is correct |
28 |
Correct |
250 ms |
26600 KB |
Output is correct |
29 |
Correct |
94 ms |
16708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
59 ms |
8212 KB |
Output is correct |
5 |
Correct |
235 ms |
26576 KB |
Output is correct |
6 |
Correct |
249 ms |
26500 KB |
Output is correct |
7 |
Correct |
250 ms |
26576 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
61 ms |
8200 KB |
Output is correct |
10 |
Correct |
265 ms |
26488 KB |
Output is correct |
11 |
Correct |
240 ms |
26564 KB |
Output is correct |
12 |
Correct |
254 ms |
26692 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
64 ms |
8200 KB |
Output is correct |
17 |
Correct |
236 ms |
26496 KB |
Output is correct |
18 |
Correct |
255 ms |
26564 KB |
Output is correct |
19 |
Correct |
294 ms |
26504 KB |
Output is correct |
20 |
Correct |
235 ms |
26548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
1996 KB |
Output is correct |
7 |
Correct |
239 ms |
25904 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
14 ms |
2040 KB |
Output is correct |
13 |
Correct |
227 ms |
26548 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
6 ms |
1600 KB |
Output is correct |
17 |
Correct |
117 ms |
16612 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
60 ms |
8132 KB |
Output is correct |
21 |
Correct |
235 ms |
26472 KB |
Output is correct |
22 |
Correct |
274 ms |
26492 KB |
Output is correct |
23 |
Correct |
247 ms |
26564 KB |
Output is correct |
24 |
Correct |
262 ms |
26612 KB |
Output is correct |
25 |
Correct |
101 ms |
16580 KB |
Output is correct |
26 |
Correct |
96 ms |
16612 KB |
Output is correct |
27 |
Correct |
251 ms |
26580 KB |
Output is correct |
28 |
Correct |
235 ms |
26520 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
11 ms |
2096 KB |
Output is correct |
37 |
Correct |
228 ms |
26600 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
10 ms |
1996 KB |
Output is correct |
41 |
Correct |
271 ms |
26564 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
7 ms |
1592 KB |
Output is correct |
45 |
Correct |
125 ms |
16672 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
60 ms |
8208 KB |
Output is correct |
50 |
Correct |
249 ms |
26632 KB |
Output is correct |
51 |
Correct |
246 ms |
26596 KB |
Output is correct |
52 |
Correct |
295 ms |
26552 KB |
Output is correct |
53 |
Correct |
95 ms |
16584 KB |
Output is correct |
54 |
Correct |
105 ms |
16608 KB |
Output is correct |
55 |
Correct |
240 ms |
26492 KB |
Output is correct |
56 |
Correct |
250 ms |
26600 KB |
Output is correct |
57 |
Correct |
94 ms |
16708 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
6 ms |
1612 KB |
Output is correct |
61 |
Correct |
118 ms |
16648 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
82 ms |
8220 KB |
Output is correct |
66 |
Correct |
101 ms |
16580 KB |
Output is correct |
67 |
Correct |
102 ms |
16708 KB |
Output is correct |
68 |
Correct |
108 ms |
16628 KB |
Output is correct |
69 |
Correct |
100 ms |
16648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
1996 KB |
Output is correct |
7 |
Correct |
239 ms |
25904 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
14 ms |
2040 KB |
Output is correct |
13 |
Correct |
227 ms |
26548 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
6 ms |
1600 KB |
Output is correct |
17 |
Correct |
117 ms |
16612 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
60 ms |
8132 KB |
Output is correct |
21 |
Correct |
235 ms |
26472 KB |
Output is correct |
22 |
Correct |
274 ms |
26492 KB |
Output is correct |
23 |
Correct |
247 ms |
26564 KB |
Output is correct |
24 |
Correct |
262 ms |
26612 KB |
Output is correct |
25 |
Correct |
101 ms |
16580 KB |
Output is correct |
26 |
Correct |
96 ms |
16612 KB |
Output is correct |
27 |
Correct |
251 ms |
26580 KB |
Output is correct |
28 |
Correct |
235 ms |
26520 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
11 ms |
2096 KB |
Output is correct |
37 |
Correct |
228 ms |
26600 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
10 ms |
1996 KB |
Output is correct |
41 |
Correct |
271 ms |
26564 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
7 ms |
1592 KB |
Output is correct |
45 |
Correct |
125 ms |
16672 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
60 ms |
8208 KB |
Output is correct |
50 |
Correct |
249 ms |
26632 KB |
Output is correct |
51 |
Correct |
246 ms |
26596 KB |
Output is correct |
52 |
Correct |
295 ms |
26552 KB |
Output is correct |
53 |
Correct |
95 ms |
16584 KB |
Output is correct |
54 |
Correct |
105 ms |
16608 KB |
Output is correct |
55 |
Correct |
240 ms |
26492 KB |
Output is correct |
56 |
Correct |
250 ms |
26600 KB |
Output is correct |
57 |
Correct |
94 ms |
16708 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
59 ms |
8212 KB |
Output is correct |
62 |
Correct |
235 ms |
26576 KB |
Output is correct |
63 |
Correct |
249 ms |
26500 KB |
Output is correct |
64 |
Correct |
250 ms |
26576 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
61 ms |
8200 KB |
Output is correct |
67 |
Correct |
265 ms |
26488 KB |
Output is correct |
68 |
Correct |
240 ms |
26564 KB |
Output is correct |
69 |
Correct |
254 ms |
26692 KB |
Output is correct |
70 |
Correct |
1 ms |
324 KB |
Output is correct |
71 |
Correct |
1 ms |
448 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
64 ms |
8200 KB |
Output is correct |
74 |
Correct |
236 ms |
26496 KB |
Output is correct |
75 |
Correct |
255 ms |
26564 KB |
Output is correct |
76 |
Correct |
294 ms |
26504 KB |
Output is correct |
77 |
Correct |
235 ms |
26548 KB |
Output is correct |
78 |
Correct |
1 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
6 ms |
1612 KB |
Output is correct |
81 |
Correct |
118 ms |
16648 KB |
Output is correct |
82 |
Correct |
1 ms |
332 KB |
Output is correct |
83 |
Correct |
1 ms |
332 KB |
Output is correct |
84 |
Correct |
1 ms |
332 KB |
Output is correct |
85 |
Correct |
82 ms |
8220 KB |
Output is correct |
86 |
Correct |
101 ms |
16580 KB |
Output is correct |
87 |
Correct |
102 ms |
16708 KB |
Output is correct |
88 |
Correct |
108 ms |
16628 KB |
Output is correct |
89 |
Correct |
100 ms |
16648 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
332 KB |
Output is correct |
92 |
Correct |
4 ms |
716 KB |
Output is correct |
93 |
Correct |
95 ms |
8700 KB |
Output is correct |
94 |
Correct |
1 ms |
332 KB |
Output is correct |
95 |
Correct |
1 ms |
332 KB |
Output is correct |
96 |
Correct |
1 ms |
336 KB |
Output is correct |
97 |
Correct |
24 ms |
2816 KB |
Output is correct |
98 |
Correct |
113 ms |
8932 KB |
Output is correct |
99 |
Correct |
94 ms |
9368 KB |
Output is correct |
100 |
Correct |
90 ms |
9060 KB |
Output is correct |
101 |
Correct |
91 ms |
10084 KB |
Output is correct |
102 |
Correct |
92 ms |
8776 KB |
Output is correct |
103 |
Correct |
88 ms |
8100 KB |
Output is correct |
104 |
Correct |
88 ms |
8108 KB |
Output is correct |
105 |
Correct |
104 ms |
8100 KB |
Output is correct |