#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int parent[maxn], parent2[maxn];
vector<int> comp[maxn], comp2[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 get2(int x) {
return parent2[x] == x ? x : parent2[x] = get2(parent2[x]);
}
bool merge2(int x, int y) {
int xroot = get2(x), yroot = get2(y);
if (comp2[xroot].size() < comp2[yroot].size()) {
swap(xroot, yroot);
}
if (xroot != yroot) {
parent2[xroot] = yroot;
comp2[yroot].insert(comp2[yroot].end(), comp2[xroot].begin(), comp2[xroot].end());
comp2[xroot].clear();
return true;
}
return false;
}
bool same2(int x, int y) {
return get2(x) == get2(y);
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; ++i) {
parent[i] = i;
parent2[i] = i;
comp[i].push_back(i);
comp2[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] == 1) {
merge(i, j);
} else if (p[i][j] == 2) {
merge2(i, j);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (!p[i][j] && (same(i, j) || same2(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 (comp2[i].size() > 1) {
for (int j = 0; j < (int) comp2[i].size() - 1; ++j) {
sol[comp2[i][j]][comp2[i][j + 1]] = 1;
sol[comp2[i][j + 1]][comp2[i][j]] = 1;
}
if (comp2[i].size() == 2) {
return 0;
}
sol[comp2[i][0]][comp2[i][comp2[i].size() - 1]] = 1;
sol[comp2[i][comp2[i].size() - 1]][comp2[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 |
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 |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
1388 KB |
Output is correct |
7 |
Correct |
242 ms |
24212 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 |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
1388 KB |
Output is correct |
7 |
Correct |
242 ms |
24212 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
1260 KB |
Output is correct |
13 |
Correct |
245 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 |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
110 ms |
14316 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
5868 KB |
Output is correct |
21 |
Correct |
251 ms |
22508 KB |
Output is correct |
22 |
Correct |
253 ms |
22252 KB |
Output is correct |
23 |
Correct |
260 ms |
23188 KB |
Output is correct |
24 |
Correct |
252 ms |
22252 KB |
Output is correct |
25 |
Correct |
103 ms |
12524 KB |
Output is correct |
26 |
Correct |
100 ms |
12268 KB |
Output is correct |
27 |
Correct |
259 ms |
23188 KB |
Output is correct |
28 |
Correct |
248 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 |
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 |
1388 KB |
Output is correct |
9 |
Correct |
249 ms |
23916 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 |
242 ms |
25964 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 |
109 ms |
15980 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 |
64 ms |
6380 KB |
Output is correct |
22 |
Correct |
258 ms |
24172 KB |
Output is correct |
23 |
Correct |
249 ms |
24044 KB |
Output is correct |
24 |
Correct |
263 ms |
24940 KB |
Output is correct |
25 |
Correct |
104 ms |
14096 KB |
Output is correct |
26 |
Correct |
101 ms |
14188 KB |
Output is correct |
27 |
Correct |
248 ms |
23916 KB |
Output is correct |
28 |
Correct |
258 ms |
25068 KB |
Output is correct |
29 |
Correct |
105 ms |
14232 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 |
64 ms |
6380 KB |
Output is correct |
5 |
Correct |
251 ms |
24196 KB |
Output is correct |
6 |
Correct |
254 ms |
24044 KB |
Output is correct |
7 |
Correct |
261 ms |
24940 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
63 ms |
6380 KB |
Output is correct |
10 |
Correct |
253 ms |
24300 KB |
Output is correct |
11 |
Correct |
249 ms |
24044 KB |
Output is correct |
12 |
Correct |
258 ms |
24984 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Incorrect |
1 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 |
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 |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
1388 KB |
Output is correct |
7 |
Correct |
242 ms |
24212 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
1260 KB |
Output is correct |
13 |
Correct |
245 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 |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
110 ms |
14316 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
5868 KB |
Output is correct |
21 |
Correct |
251 ms |
22508 KB |
Output is correct |
22 |
Correct |
253 ms |
22252 KB |
Output is correct |
23 |
Correct |
260 ms |
23188 KB |
Output is correct |
24 |
Correct |
252 ms |
22252 KB |
Output is correct |
25 |
Correct |
103 ms |
12524 KB |
Output is correct |
26 |
Correct |
100 ms |
12268 KB |
Output is correct |
27 |
Correct |
259 ms |
23188 KB |
Output is correct |
28 |
Correct |
248 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 |
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 |
1388 KB |
Output is correct |
37 |
Correct |
249 ms |
23916 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 |
242 ms |
25964 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 |
109 ms |
15980 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 |
64 ms |
6380 KB |
Output is correct |
50 |
Correct |
258 ms |
24172 KB |
Output is correct |
51 |
Correct |
249 ms |
24044 KB |
Output is correct |
52 |
Correct |
263 ms |
24940 KB |
Output is correct |
53 |
Correct |
104 ms |
14096 KB |
Output is correct |
54 |
Correct |
101 ms |
14188 KB |
Output is correct |
55 |
Correct |
248 ms |
23916 KB |
Output is correct |
56 |
Correct |
258 ms |
25068 KB |
Output is correct |
57 |
Correct |
105 ms |
14232 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 |
1132 KB |
Output is correct |
61 |
Correct |
111 ms |
17900 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 |
64 ms |
6400 KB |
Too many ways to get from 1 to 183, should be 1 found no less than 2 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
1388 KB |
Output is correct |
7 |
Correct |
242 ms |
24212 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
1260 KB |
Output is correct |
13 |
Correct |
245 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 |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
110 ms |
14316 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
64 ms |
5868 KB |
Output is correct |
21 |
Correct |
251 ms |
22508 KB |
Output is correct |
22 |
Correct |
253 ms |
22252 KB |
Output is correct |
23 |
Correct |
260 ms |
23188 KB |
Output is correct |
24 |
Correct |
252 ms |
22252 KB |
Output is correct |
25 |
Correct |
103 ms |
12524 KB |
Output is correct |
26 |
Correct |
100 ms |
12268 KB |
Output is correct |
27 |
Correct |
259 ms |
23188 KB |
Output is correct |
28 |
Correct |
248 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 |
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 |
1388 KB |
Output is correct |
37 |
Correct |
249 ms |
23916 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 |
242 ms |
25964 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 |
109 ms |
15980 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 |
64 ms |
6380 KB |
Output is correct |
50 |
Correct |
258 ms |
24172 KB |
Output is correct |
51 |
Correct |
249 ms |
24044 KB |
Output is correct |
52 |
Correct |
263 ms |
24940 KB |
Output is correct |
53 |
Correct |
104 ms |
14096 KB |
Output is correct |
54 |
Correct |
101 ms |
14188 KB |
Output is correct |
55 |
Correct |
248 ms |
23916 KB |
Output is correct |
56 |
Correct |
258 ms |
25068 KB |
Output is correct |
57 |
Correct |
105 ms |
14232 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 |
64 ms |
6380 KB |
Output is correct |
62 |
Correct |
251 ms |
24196 KB |
Output is correct |
63 |
Correct |
254 ms |
24044 KB |
Output is correct |
64 |
Correct |
261 ms |
24940 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
63 ms |
6380 KB |
Output is correct |
67 |
Correct |
253 ms |
24300 KB |
Output is correct |
68 |
Correct |
249 ms |
24044 KB |
Output is correct |
69 |
Correct |
258 ms |
24984 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Incorrect |
1 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 |
- |