#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3+3;
const int inf = 1e9;
std::vector<std::vector<int>> answer;
int par1[N], ran1[N], par2[N], ran2[N];
int find_p1(int a) {
return (a == par1[a]) ? a : find_p1(par1[a]);
}
int find_p2(int a) {
return (a == par2[a]) ? a : find_p2(par2[a]);
}
void unitee1(int a, int b) {
a = find_p1(a), b = find_p1(b);
if(a == b) return;
if(ran1[a] < ran1[b]) swap(a, b);
par1[b] = par1[a];
if(ran1[a]==ran1[b]) ran1[a]++;
}
void unitee2(int a, int b) {
a = find_p2(a), b = find_p2(b);
if(a == b) return;
if(ran2[a] < ran2[b]) swap(a, b);
par2[b] = par2[a];
if(ran2[a]==ran2[b]) ran2[a]++;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
for(int i = 0; i < n; i++) par2[i] = par1[i] = i, ran2[i] = ran1[i] = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j]==3) return 0;
if(p[i][j] == 1) {
unitee1(i, j);
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
answer[i][j] = 0;
if(i==j) continue;
if(p[i][j] == 0 && find_p1(i)==find_p1(j)) return 0;
if(p[i][j]==1) {
int pp = find_p1(i);
if(pp != i)answer[pp][i] = 1, answer[i][pp] = 1;
if(pp != j)answer[pp][j] = 1, answer[j][pp] = 1;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j] == 2) {
unitee2(find_p1(i), find_p1(j));
}
}
}
map<int, set<int>> M;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i==j) {
answer[i][j] = 0;
continue;
}
int a = find_p1(i), b = find_p1(j);
if(p[i][j] == 0 && find_p2(a)==find_p2(b)) return 0;
if(p[i][j]== 2 && find_p2(a)==find_p2(b)) {
int pp = find_p2(a);
if(pp != a)M[pp].insert(a);
if(pp != b)M[pp].insert(b);
}
}
}
for(auto c : M) {
int f = c.first, prev = c.first;
for(auto x : c.second) {
answer[prev][x] = 1, answer[x][prev] = 1;
prev = x;
}
if(c.second.size() > 1){
answer[f][prev] = answer[prev][f] = 1;
}
else {
return 0;
}
}
build(answer);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
12 ms |
1260 KB |
Output is correct |
7 |
Correct |
322 ms |
22252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
12 ms |
1260 KB |
Output is correct |
7 |
Correct |
322 ms |
22252 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
259 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
876 KB |
Output is correct |
17 |
Correct |
134 ms |
12396 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
66 ms |
5868 KB |
Output is correct |
21 |
Correct |
270 ms |
22252 KB |
Output is correct |
22 |
Correct |
268 ms |
22252 KB |
Output is correct |
23 |
Correct |
286 ms |
22252 KB |
Output is correct |
24 |
Correct |
294 ms |
22200 KB |
Output is correct |
25 |
Correct |
117 ms |
12268 KB |
Output is correct |
26 |
Correct |
149 ms |
12268 KB |
Output is correct |
27 |
Correct |
314 ms |
22508 KB |
Output is correct |
28 |
Correct |
268 ms |
22252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
11 ms |
1260 KB |
Output is correct |
9 |
Correct |
269 ms |
22184 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
16 ms |
1260 KB |
Output is correct |
13 |
Correct |
405 ms |
22236 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
9 ms |
876 KB |
Output is correct |
17 |
Correct |
127 ms |
12268 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
80 ms |
6124 KB |
Output is correct |
22 |
Correct |
277 ms |
22360 KB |
Output is correct |
23 |
Correct |
266 ms |
22252 KB |
Output is correct |
24 |
Correct |
370 ms |
22252 KB |
Output is correct |
25 |
Correct |
117 ms |
12396 KB |
Output is correct |
26 |
Correct |
111 ms |
12396 KB |
Output is correct |
27 |
Correct |
270 ms |
22380 KB |
Output is correct |
28 |
Correct |
317 ms |
22252 KB |
Output is correct |
29 |
Correct |
114 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
68 ms |
5960 KB |
Output is correct |
5 |
Correct |
270 ms |
22208 KB |
Output is correct |
6 |
Correct |
264 ms |
22252 KB |
Output is correct |
7 |
Correct |
345 ms |
22244 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
69 ms |
5996 KB |
Output is correct |
10 |
Correct |
304 ms |
22252 KB |
Output is correct |
11 |
Correct |
288 ms |
22508 KB |
Output is correct |
12 |
Correct |
340 ms |
22892 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
68 ms |
6380 KB |
Output is correct |
17 |
Correct |
274 ms |
24096 KB |
Output is correct |
18 |
Correct |
283 ms |
24300 KB |
Output is correct |
19 |
Correct |
282 ms |
24172 KB |
Output is correct |
20 |
Correct |
269 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
12 ms |
1260 KB |
Output is correct |
7 |
Correct |
322 ms |
22252 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
259 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
876 KB |
Output is correct |
17 |
Correct |
134 ms |
12396 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
66 ms |
5868 KB |
Output is correct |
21 |
Correct |
270 ms |
22252 KB |
Output is correct |
22 |
Correct |
268 ms |
22252 KB |
Output is correct |
23 |
Correct |
286 ms |
22252 KB |
Output is correct |
24 |
Correct |
294 ms |
22200 KB |
Output is correct |
25 |
Correct |
117 ms |
12268 KB |
Output is correct |
26 |
Correct |
149 ms |
12268 KB |
Output is correct |
27 |
Correct |
314 ms |
22508 KB |
Output is correct |
28 |
Correct |
268 ms |
22252 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
11 ms |
1260 KB |
Output is correct |
37 |
Correct |
269 ms |
22184 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
16 ms |
1260 KB |
Output is correct |
41 |
Correct |
405 ms |
22236 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
9 ms |
876 KB |
Output is correct |
45 |
Correct |
127 ms |
12268 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
80 ms |
6124 KB |
Output is correct |
50 |
Correct |
277 ms |
22360 KB |
Output is correct |
51 |
Correct |
266 ms |
22252 KB |
Output is correct |
52 |
Correct |
370 ms |
22252 KB |
Output is correct |
53 |
Correct |
117 ms |
12396 KB |
Output is correct |
54 |
Correct |
111 ms |
12396 KB |
Output is correct |
55 |
Correct |
270 ms |
22380 KB |
Output is correct |
56 |
Correct |
317 ms |
22252 KB |
Output is correct |
57 |
Correct |
114 ms |
12268 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
7 ms |
876 KB |
Output is correct |
61 |
Correct |
117 ms |
12908 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
68 ms |
6380 KB |
Output is correct |
66 |
Correct |
114 ms |
14188 KB |
Output is correct |
67 |
Correct |
106 ms |
14188 KB |
Output is correct |
68 |
Correct |
117 ms |
14188 KB |
Output is correct |
69 |
Correct |
126 ms |
14188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
12 ms |
1260 KB |
Output is correct |
7 |
Correct |
322 ms |
22252 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1260 KB |
Output is correct |
13 |
Correct |
259 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
876 KB |
Output is correct |
17 |
Correct |
134 ms |
12396 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
66 ms |
5868 KB |
Output is correct |
21 |
Correct |
270 ms |
22252 KB |
Output is correct |
22 |
Correct |
268 ms |
22252 KB |
Output is correct |
23 |
Correct |
286 ms |
22252 KB |
Output is correct |
24 |
Correct |
294 ms |
22200 KB |
Output is correct |
25 |
Correct |
117 ms |
12268 KB |
Output is correct |
26 |
Correct |
149 ms |
12268 KB |
Output is correct |
27 |
Correct |
314 ms |
22508 KB |
Output is correct |
28 |
Correct |
268 ms |
22252 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
11 ms |
1260 KB |
Output is correct |
37 |
Correct |
269 ms |
22184 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
16 ms |
1260 KB |
Output is correct |
41 |
Correct |
405 ms |
22236 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
9 ms |
876 KB |
Output is correct |
45 |
Correct |
127 ms |
12268 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
80 ms |
6124 KB |
Output is correct |
50 |
Correct |
277 ms |
22360 KB |
Output is correct |
51 |
Correct |
266 ms |
22252 KB |
Output is correct |
52 |
Correct |
370 ms |
22252 KB |
Output is correct |
53 |
Correct |
117 ms |
12396 KB |
Output is correct |
54 |
Correct |
111 ms |
12396 KB |
Output is correct |
55 |
Correct |
270 ms |
22380 KB |
Output is correct |
56 |
Correct |
317 ms |
22252 KB |
Output is correct |
57 |
Correct |
114 ms |
12268 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
68 ms |
5960 KB |
Output is correct |
62 |
Correct |
270 ms |
22208 KB |
Output is correct |
63 |
Correct |
264 ms |
22252 KB |
Output is correct |
64 |
Correct |
345 ms |
22244 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
69 ms |
5996 KB |
Output is correct |
67 |
Correct |
304 ms |
22252 KB |
Output is correct |
68 |
Correct |
288 ms |
22508 KB |
Output is correct |
69 |
Correct |
340 ms |
22892 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
68 ms |
6380 KB |
Output is correct |
74 |
Correct |
274 ms |
24096 KB |
Output is correct |
75 |
Correct |
283 ms |
24300 KB |
Output is correct |
76 |
Correct |
282 ms |
24172 KB |
Output is correct |
77 |
Correct |
269 ms |
24044 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
7 ms |
876 KB |
Output is correct |
81 |
Correct |
117 ms |
12908 KB |
Output is correct |
82 |
Correct |
1 ms |
364 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
1 ms |
364 KB |
Output is correct |
85 |
Correct |
68 ms |
6380 KB |
Output is correct |
86 |
Correct |
114 ms |
14188 KB |
Output is correct |
87 |
Correct |
106 ms |
14188 KB |
Output is correct |
88 |
Correct |
117 ms |
14188 KB |
Output is correct |
89 |
Correct |
126 ms |
14188 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |
91 |
Correct |
1 ms |
364 KB |
Output is correct |
92 |
Correct |
5 ms |
876 KB |
Output is correct |
93 |
Correct |
118 ms |
14188 KB |
Output is correct |
94 |
Correct |
1 ms |
364 KB |
Output is correct |
95 |
Correct |
1 ms |
364 KB |
Output is correct |
96 |
Correct |
1 ms |
364 KB |
Output is correct |
97 |
Correct |
27 ms |
3820 KB |
Output is correct |
98 |
Correct |
106 ms |
14188 KB |
Output is correct |
99 |
Correct |
110 ms |
14188 KB |
Output is correct |
100 |
Correct |
105 ms |
14188 KB |
Output is correct |
101 |
Correct |
104 ms |
14188 KB |
Output is correct |
102 |
Correct |
111 ms |
14188 KB |
Output is correct |
103 |
Correct |
104 ms |
14188 KB |
Output is correct |
104 |
Correct |
104 ms |
14188 KB |
Output is correct |
105 |
Correct |
125 ms |
14164 KB |
Output is correct |