#include "supertrees.h"
#include <vector>
#include <iostream>
using namespace std;
const int mxN = 1000;
vector<int> boss1(mxN), boss2(mxN), under1(mxN, 1), under2(mxN, 1);
int find1(int i) {
if (boss1[i] == i) return i;
return boss1[i] = find1(boss1[i]);
}
int find2(int i) {
if (boss2[i] == i) return i;
return boss2[i] = find2(boss2[i]);
}
void unite1(int i, int j) {
i = find1(i);
j = find1(j);
if (i == j) return;
if (under1[i] > under1[j]) swap(i, j);
under1[j] += under1[i];
boss1[i] = j;
}
void unite2(int i, int j) {
i = find2(i);
j = find2(j);
if (i == j) return;
if (under2[i] > under2[j]) swap(i, j);
under2[j] += under2[i];
boss2[i] = j;
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; ++i) boss1[i] = boss2[i] = i;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i==j) continue;
if (p[i][j] == 1) unite1(i, j);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i==j) continue;
if (p[i][j] == 2) unite2(find1(i), find1(j));
}
}
for (int _i = 0; _i < n; ++_i) {
for (int _j = 0; _j < n; ++_j) {
if (_i == _j) continue;
int i = find1(_i), j = find1(_j);
if (p[_i][_j] == 3) { cerr << "3!\n"; return 0; };
if ((p[_i][_j]==0 || p[_i][_j]==2) && i==j) { cerr << "same tree\n"; return 0; }
if (p[_i][_j]==0 && find2(i) == find2(j)) { cerr << "same cycle\n"; return 0; }
}
}
vector<vector<int>> ans(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
if (find1(i) != i) ans[i][boss1[i]] = ans[boss1[i]][i] = 1;
}
vector<int> cycles[n];
for (int i = 0; i < n; ++i) {
if (boss1[i] == i) {
cycles[find2(i)].push_back(i);
}
}
for (int i = 0; i < n; ++i) {
if (cycles[i].size() == 2) return 0;
if (cycles[i].size() <= 1) continue;
for (int j = 0; j < (int)cycles[i].size(); ++j) {
ans[cycles[i][(j+1)%cycles[i].size()]][cycles[i][j]] =
ans[cycles[i][j]][cycles[i][(j+1)%cycles[i].size()]] = 1;
}
}
build(ans);
return 1;
}
# |
Verdict |
Execution time |
Memory |
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 |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
262 ms |
22252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
262 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 |
1408 KB |
Output is correct |
13 |
Correct |
251 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
122 ms |
10208 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 |
6380 KB |
Output is correct |
21 |
Correct |
280 ms |
24172 KB |
Output is correct |
22 |
Correct |
264 ms |
24044 KB |
Output is correct |
23 |
Correct |
286 ms |
24044 KB |
Output is correct |
24 |
Correct |
280 ms |
24172 KB |
Output is correct |
25 |
Correct |
108 ms |
10220 KB |
Output is correct |
26 |
Correct |
105 ms |
10220 KB |
Output is correct |
27 |
Correct |
286 ms |
24044 KB |
Output is correct |
28 |
Correct |
255 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 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 |
251 ms |
22252 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
12 ms |
1260 KB |
Output is correct |
13 |
Correct |
261 ms |
22272 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
620 KB |
Output is correct |
17 |
Correct |
122 ms |
8300 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
65 ms |
5868 KB |
Output is correct |
22 |
Correct |
260 ms |
22124 KB |
Output is correct |
23 |
Correct |
265 ms |
22252 KB |
Output is correct |
24 |
Correct |
281 ms |
22380 KB |
Output is correct |
25 |
Correct |
115 ms |
12268 KB |
Output is correct |
26 |
Correct |
105 ms |
8176 KB |
Output is correct |
27 |
Correct |
259 ms |
22124 KB |
Output is correct |
28 |
Correct |
289 ms |
22124 KB |
Output is correct |
29 |
Correct |
116 ms |
12268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
67 ms |
5864 KB |
Output is correct |
5 |
Correct |
276 ms |
22124 KB |
Output is correct |
6 |
Correct |
256 ms |
22252 KB |
Output is correct |
7 |
Correct |
287 ms |
22124 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
69 ms |
5868 KB |
Output is correct |
10 |
Correct |
261 ms |
22252 KB |
Output is correct |
11 |
Correct |
260 ms |
22380 KB |
Output is correct |
12 |
Correct |
281 ms |
22256 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 |
67 ms |
5868 KB |
Output is correct |
17 |
Correct |
277 ms |
22256 KB |
Output is correct |
18 |
Correct |
281 ms |
22128 KB |
Output is correct |
19 |
Correct |
260 ms |
22252 KB |
Output is correct |
20 |
Correct |
261 ms |
22252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
262 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 |
1408 KB |
Output is correct |
13 |
Correct |
251 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
122 ms |
10208 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 |
6380 KB |
Output is correct |
21 |
Correct |
280 ms |
24172 KB |
Output is correct |
22 |
Correct |
264 ms |
24044 KB |
Output is correct |
23 |
Correct |
286 ms |
24044 KB |
Output is correct |
24 |
Correct |
280 ms |
24172 KB |
Output is correct |
25 |
Correct |
108 ms |
10220 KB |
Output is correct |
26 |
Correct |
105 ms |
10220 KB |
Output is correct |
27 |
Correct |
286 ms |
24044 KB |
Output is correct |
28 |
Correct |
255 ms |
24172 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
0 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 |
251 ms |
22252 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
12 ms |
1260 KB |
Output is correct |
41 |
Correct |
261 ms |
22272 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
620 KB |
Output is correct |
45 |
Correct |
122 ms |
8300 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
65 ms |
5868 KB |
Output is correct |
50 |
Correct |
260 ms |
22124 KB |
Output is correct |
51 |
Correct |
265 ms |
22252 KB |
Output is correct |
52 |
Correct |
281 ms |
22380 KB |
Output is correct |
53 |
Correct |
115 ms |
12268 KB |
Output is correct |
54 |
Correct |
105 ms |
8176 KB |
Output is correct |
55 |
Correct |
259 ms |
22124 KB |
Output is correct |
56 |
Correct |
289 ms |
22124 KB |
Output is correct |
57 |
Correct |
116 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 |
6 ms |
748 KB |
Output is correct |
61 |
Correct |
133 ms |
10220 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 |
106 ms |
10220 KB |
Output is correct |
67 |
Correct |
108 ms |
10144 KB |
Output is correct |
68 |
Correct |
102 ms |
10220 KB |
Output is correct |
69 |
Correct |
117 ms |
14316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
11 ms |
1260 KB |
Output is correct |
7 |
Correct |
262 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 |
1408 KB |
Output is correct |
13 |
Correct |
251 ms |
22252 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
122 ms |
10208 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 |
6380 KB |
Output is correct |
21 |
Correct |
280 ms |
24172 KB |
Output is correct |
22 |
Correct |
264 ms |
24044 KB |
Output is correct |
23 |
Correct |
286 ms |
24044 KB |
Output is correct |
24 |
Correct |
280 ms |
24172 KB |
Output is correct |
25 |
Correct |
108 ms |
10220 KB |
Output is correct |
26 |
Correct |
105 ms |
10220 KB |
Output is correct |
27 |
Correct |
286 ms |
24044 KB |
Output is correct |
28 |
Correct |
255 ms |
24172 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
0 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 |
251 ms |
22252 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
12 ms |
1260 KB |
Output is correct |
41 |
Correct |
261 ms |
22272 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
620 KB |
Output is correct |
45 |
Correct |
122 ms |
8300 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
65 ms |
5868 KB |
Output is correct |
50 |
Correct |
260 ms |
22124 KB |
Output is correct |
51 |
Correct |
265 ms |
22252 KB |
Output is correct |
52 |
Correct |
281 ms |
22380 KB |
Output is correct |
53 |
Correct |
115 ms |
12268 KB |
Output is correct |
54 |
Correct |
105 ms |
8176 KB |
Output is correct |
55 |
Correct |
259 ms |
22124 KB |
Output is correct |
56 |
Correct |
289 ms |
22124 KB |
Output is correct |
57 |
Correct |
116 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 |
67 ms |
5864 KB |
Output is correct |
62 |
Correct |
276 ms |
22124 KB |
Output is correct |
63 |
Correct |
256 ms |
22252 KB |
Output is correct |
64 |
Correct |
287 ms |
22124 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
69 ms |
5868 KB |
Output is correct |
67 |
Correct |
261 ms |
22252 KB |
Output is correct |
68 |
Correct |
260 ms |
22380 KB |
Output is correct |
69 |
Correct |
281 ms |
22256 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 |
67 ms |
5868 KB |
Output is correct |
74 |
Correct |
277 ms |
22256 KB |
Output is correct |
75 |
Correct |
281 ms |
22128 KB |
Output is correct |
76 |
Correct |
260 ms |
22252 KB |
Output is correct |
77 |
Correct |
261 ms |
22252 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
6 ms |
748 KB |
Output is correct |
81 |
Correct |
133 ms |
10220 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 |
106 ms |
10220 KB |
Output is correct |
87 |
Correct |
108 ms |
10144 KB |
Output is correct |
88 |
Correct |
102 ms |
10220 KB |
Output is correct |
89 |
Correct |
117 ms |
14316 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 |
748 KB |
Output is correct |
93 |
Correct |
141 ms |
10244 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 |
35 ms |
2796 KB |
Output is correct |
98 |
Correct |
109 ms |
10220 KB |
Output is correct |
99 |
Correct |
121 ms |
10220 KB |
Output is correct |
100 |
Correct |
110 ms |
10220 KB |
Output is correct |
101 |
Correct |
109 ms |
10220 KB |
Output is correct |
102 |
Correct |
108 ms |
10220 KB |
Output is correct |
103 |
Correct |
115 ms |
10220 KB |
Output is correct |
104 |
Correct |
115 ms |
10348 KB |
Output is correct |
105 |
Correct |
110 ms |
10220 KB |
Output is correct |