#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int parent[maxn];
vector<int> comp[maxn];
int get(int x) {
return parent[x] == x ? x : parent[x] = get(parent[x]);
}
bool merge(int x, int y) {
int xroot = get(x), yroot = get(y);
if (comp[xroot].size() < comp[yroot].size()) {
swap(xroot, yroot);
}
if (xroot != yroot) {
parent[xroot] = yroot;
comp[yroot].insert(comp[yroot].end(), comp[xroot].begin(), comp[xroot].end());
comp[xroot].clear();
return true;
}
return false;
}
bool same(int x, int y) {
return get(x) == get(y);
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; ++i) {
parent[i] = i;
comp[i].push_back(i);
if (p[i][i] != 1) {
return 0;
}
}
vector<vector<int>> sol(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j]) {
merge(i, j);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (!p[i][j] && same(i, j)) {
return 0;
}
}
}
for (int i = 0; i < n; i++) {
if (comp[i].size() > 1) {
for (int j = 0; j < (int) comp[i].size() - 1; ++j) {
sol[comp[i][j]][comp[i][j + 1]] = 1;
sol[comp[i][j + 1]][comp[i][j]] = 1;
}
if (p[comp[i][0]][comp[i][1]] == 2) {
if (comp[i].size() == 2) {
return 0;
}
sol[comp[i][0]][comp[i][comp[i].size() - 1]] = 1;
sol[comp[i][comp[i].size() - 1]][comp[i][0]] = 1;
}
}
}
build(sol);
return 1;
}
#ifdef DEBUG
int main() {
freopen("1.in", "r", stdin);
int n;
cin >> n;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
cout << construct(a) << "\n";
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
10 ms |
1260 KB |
Output is correct |
7 |
Correct |
241 ms |
24172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
10 ms |
1260 KB |
Output is correct |
7 |
Correct |
241 ms |
24172 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 |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
10 ms |
1260 KB |
Output is correct |
13 |
Correct |
246 ms |
22124 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 |
876 KB |
Output is correct |
17 |
Correct |
110 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
62 ms |
5868 KB |
Output is correct |
21 |
Correct |
259 ms |
22508 KB |
Output is correct |
22 |
Correct |
247 ms |
22252 KB |
Output is correct |
23 |
Correct |
253 ms |
23420 KB |
Output is correct |
24 |
Correct |
247 ms |
22380 KB |
Output is correct |
25 |
Correct |
104 ms |
12396 KB |
Output is correct |
26 |
Correct |
102 ms |
12396 KB |
Output is correct |
27 |
Correct |
255 ms |
23148 KB |
Output is correct |
28 |
Correct |
244 ms |
22124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 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 |
23276 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
10 ms |
1388 KB |
Output is correct |
13 |
Correct |
257 ms |
25312 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 |
1004 KB |
Output is correct |
17 |
Correct |
108 ms |
15340 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 |
63 ms |
6380 KB |
Output is correct |
22 |
Correct |
247 ms |
23532 KB |
Output is correct |
23 |
Correct |
253 ms |
23404 KB |
Output is correct |
24 |
Correct |
254 ms |
24300 KB |
Output is correct |
25 |
Correct |
101 ms |
13292 KB |
Output is correct |
26 |
Correct |
104 ms |
13548 KB |
Output is correct |
27 |
Correct |
268 ms |
23404 KB |
Output is correct |
28 |
Correct |
258 ms |
24404 KB |
Output is correct |
29 |
Correct |
105 ms |
13292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
62 ms |
5868 KB |
Output is correct |
5 |
Correct |
250 ms |
22380 KB |
Output is correct |
6 |
Correct |
246 ms |
22124 KB |
Output is correct |
7 |
Correct |
255 ms |
23148 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
63 ms |
5868 KB |
Output is correct |
10 |
Correct |
254 ms |
22380 KB |
Output is correct |
11 |
Correct |
262 ms |
22252 KB |
Output is correct |
12 |
Correct |
256 ms |
23148 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Incorrect |
0 ms |
364 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
10 ms |
1260 KB |
Output is correct |
7 |
Correct |
241 ms |
24172 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 |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
10 ms |
1260 KB |
Output is correct |
13 |
Correct |
246 ms |
22124 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 |
876 KB |
Output is correct |
17 |
Correct |
110 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
62 ms |
5868 KB |
Output is correct |
21 |
Correct |
259 ms |
22508 KB |
Output is correct |
22 |
Correct |
247 ms |
22252 KB |
Output is correct |
23 |
Correct |
253 ms |
23420 KB |
Output is correct |
24 |
Correct |
247 ms |
22380 KB |
Output is correct |
25 |
Correct |
104 ms |
12396 KB |
Output is correct |
26 |
Correct |
102 ms |
12396 KB |
Output is correct |
27 |
Correct |
255 ms |
23148 KB |
Output is correct |
28 |
Correct |
244 ms |
22124 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
0 ms |
364 KB |
Output is correct |
31 |
Correct |
0 ms |
364 KB |
Output is correct |
32 |
Correct |
0 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 |
23276 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
10 ms |
1388 KB |
Output is correct |
41 |
Correct |
257 ms |
25312 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 |
1004 KB |
Output is correct |
45 |
Correct |
108 ms |
15340 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 |
63 ms |
6380 KB |
Output is correct |
50 |
Correct |
247 ms |
23532 KB |
Output is correct |
51 |
Correct |
253 ms |
23404 KB |
Output is correct |
52 |
Correct |
254 ms |
24300 KB |
Output is correct |
53 |
Correct |
101 ms |
13292 KB |
Output is correct |
54 |
Correct |
104 ms |
13548 KB |
Output is correct |
55 |
Correct |
268 ms |
23404 KB |
Output is correct |
56 |
Correct |
258 ms |
24404 KB |
Output is correct |
57 |
Correct |
105 ms |
13292 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
5 ms |
1004 KB |
Output is correct |
61 |
Correct |
106 ms |
15340 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 |
Incorrect |
63 ms |
6380 KB |
Too many ways to get from 7 to 50, should be 1 found no less than 2 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
10 ms |
1260 KB |
Output is correct |
7 |
Correct |
241 ms |
24172 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 |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
10 ms |
1260 KB |
Output is correct |
13 |
Correct |
246 ms |
22124 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 |
876 KB |
Output is correct |
17 |
Correct |
110 ms |
14188 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
62 ms |
5868 KB |
Output is correct |
21 |
Correct |
259 ms |
22508 KB |
Output is correct |
22 |
Correct |
247 ms |
22252 KB |
Output is correct |
23 |
Correct |
253 ms |
23420 KB |
Output is correct |
24 |
Correct |
247 ms |
22380 KB |
Output is correct |
25 |
Correct |
104 ms |
12396 KB |
Output is correct |
26 |
Correct |
102 ms |
12396 KB |
Output is correct |
27 |
Correct |
255 ms |
23148 KB |
Output is correct |
28 |
Correct |
244 ms |
22124 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
0 ms |
364 KB |
Output is correct |
31 |
Correct |
0 ms |
364 KB |
Output is correct |
32 |
Correct |
0 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 |
23276 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
10 ms |
1388 KB |
Output is correct |
41 |
Correct |
257 ms |
25312 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 |
1004 KB |
Output is correct |
45 |
Correct |
108 ms |
15340 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 |
63 ms |
6380 KB |
Output is correct |
50 |
Correct |
247 ms |
23532 KB |
Output is correct |
51 |
Correct |
253 ms |
23404 KB |
Output is correct |
52 |
Correct |
254 ms |
24300 KB |
Output is correct |
53 |
Correct |
101 ms |
13292 KB |
Output is correct |
54 |
Correct |
104 ms |
13548 KB |
Output is correct |
55 |
Correct |
268 ms |
23404 KB |
Output is correct |
56 |
Correct |
258 ms |
24404 KB |
Output is correct |
57 |
Correct |
105 ms |
13292 KB |
Output is correct |
58 |
Correct |
0 ms |
364 KB |
Output is correct |
59 |
Correct |
0 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
62 ms |
5868 KB |
Output is correct |
62 |
Correct |
250 ms |
22380 KB |
Output is correct |
63 |
Correct |
246 ms |
22124 KB |
Output is correct |
64 |
Correct |
255 ms |
23148 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
63 ms |
5868 KB |
Output is correct |
67 |
Correct |
254 ms |
22380 KB |
Output is correct |
68 |
Correct |
262 ms |
22252 KB |
Output is correct |
69 |
Correct |
256 ms |
23148 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Incorrect |
0 ms |
364 KB |
Too many ways to get from 0 to 4, should be 1 found no less than 2 |
73 |
Halted |
0 ms |
0 KB |
- |