#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
struct UF {
vi p, rnk;
int nsets;
UF(int n) {
p = vi(n);
for (int i = 0; i < n; ++i)p[i] = i;
rnk = vi(n);
nsets = n;
}
int parent(int i) {
if (p[i] == i)return i;
return p[i] = parent(p[i]);
}
bool check(int a, int b) {
return parent(a) == parent(b);
}
void join(int a, int b) {
if (check(a,b))return;
nsets--;
a = parent(a);
b = parent(b);
if (rnk[a] > rnk[b]) {
p[b] = a;
} else {
p[b] = a;
if (rnk[a] == rnk[b]) {
rnk[b]++;
}
}
}
};
int construct2(vector<vi> p) {
int n = p.size();
UF uf(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j)continue;
if (p[i][j] == 2) {
uf.join(i,j);
} else {
if (uf.check(i,j))return 0;
}
}
}
vector<vi> ring(n);
for (int i = 0; i < n; ++i) {
ring[uf.parent(i)].push_back(i);
}
vector<vi> ans(n, vi(n, 0));
for (auto vv : ring) {
if (vv.size() <= 1)continue;
if (vv.size() == 2)return 0;
for (int i = 0; i < vv.size(); ++i) {
int pp = (i == 0 ? vv.back() : vv[i-1]);
ans[vv[i]][pp] = 1;
ans[pp][vv[i]] = 1;
}
}
build(ans);
return 1;
}
int construct(vector<vi> p) {
int n = p.size();
for (auto vv : p) {
for (auto x : vv) {
if (x == 2)return construct2(p);
}
}
UF uf(n);
vector<vi> ans(n, vi(n, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j)continue;
if (p[i][j]) {
uf.join(i,j);
} else {
if (uf.check(i,j))return 0;
}
}
}
for (int i = 0; i < n; ++i) {
int par = uf.parent(i);
if (par == i)continue;
ans[i][par] = 1;
ans[par][i] = 1;
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct2(std::vector<std::vector<int> >)':
supertrees.cpp:72:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < vv.size(); ++i) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1180 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1180 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1100 KB |
Output is correct |
13 |
Correct |
269 ms |
22024 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
704 KB |
Output is correct |
17 |
Correct |
105 ms |
12024 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
61 ms |
5700 KB |
Output is correct |
21 |
Correct |
253 ms |
22016 KB |
Output is correct |
22 |
Correct |
293 ms |
21988 KB |
Output is correct |
23 |
Correct |
254 ms |
21928 KB |
Output is correct |
24 |
Correct |
231 ms |
21960 KB |
Output is correct |
25 |
Correct |
101 ms |
12028 KB |
Output is correct |
26 |
Correct |
113 ms |
12044 KB |
Output is correct |
27 |
Correct |
300 ms |
22020 KB |
Output is correct |
28 |
Correct |
241 ms |
21956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
12 ms |
1108 KB |
Output is correct |
9 |
Correct |
237 ms |
21940 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
296 ms |
25924 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
104 ms |
12044 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
59 ms |
6656 KB |
Output is correct |
22 |
Correct |
236 ms |
25976 KB |
Output is correct |
23 |
Correct |
234 ms |
25924 KB |
Output is correct |
24 |
Correct |
260 ms |
26076 KB |
Output is correct |
25 |
Correct |
107 ms |
16124 KB |
Output is correct |
26 |
Correct |
96 ms |
12032 KB |
Output is correct |
27 |
Correct |
240 ms |
25992 KB |
Output is correct |
28 |
Correct |
259 ms |
25980 KB |
Output is correct |
29 |
Correct |
98 ms |
16120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
59 ms |
5728 KB |
Output is correct |
5 |
Correct |
239 ms |
22012 KB |
Output is correct |
6 |
Correct |
237 ms |
22084 KB |
Output is correct |
7 |
Correct |
248 ms |
22084 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
74 ms |
6720 KB |
Output is correct |
10 |
Correct |
267 ms |
25932 KB |
Output is correct |
11 |
Correct |
236 ms |
25924 KB |
Output is correct |
12 |
Correct |
269 ms |
25980 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Incorrect |
0 ms |
204 KB |
Too few ways to get from 4 to 5, should be 1 found 0 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1180 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1100 KB |
Output is correct |
13 |
Correct |
269 ms |
22024 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
704 KB |
Output is correct |
17 |
Correct |
105 ms |
12024 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
61 ms |
5700 KB |
Output is correct |
21 |
Correct |
253 ms |
22016 KB |
Output is correct |
22 |
Correct |
293 ms |
21988 KB |
Output is correct |
23 |
Correct |
254 ms |
21928 KB |
Output is correct |
24 |
Correct |
231 ms |
21960 KB |
Output is correct |
25 |
Correct |
101 ms |
12028 KB |
Output is correct |
26 |
Correct |
113 ms |
12044 KB |
Output is correct |
27 |
Correct |
300 ms |
22020 KB |
Output is correct |
28 |
Correct |
241 ms |
21956 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
12 ms |
1108 KB |
Output is correct |
37 |
Correct |
237 ms |
21940 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1280 KB |
Output is correct |
41 |
Correct |
296 ms |
25924 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
104 ms |
12044 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
6656 KB |
Output is correct |
50 |
Correct |
236 ms |
25976 KB |
Output is correct |
51 |
Correct |
234 ms |
25924 KB |
Output is correct |
52 |
Correct |
260 ms |
26076 KB |
Output is correct |
53 |
Correct |
107 ms |
16124 KB |
Output is correct |
54 |
Correct |
96 ms |
12032 KB |
Output is correct |
55 |
Correct |
240 ms |
25992 KB |
Output is correct |
56 |
Correct |
259 ms |
25980 KB |
Output is correct |
57 |
Correct |
98 ms |
16120 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
5 ms |
716 KB |
Output is correct |
61 |
Correct |
98 ms |
12040 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Incorrect |
1 ms |
204 KB |
Too few ways to get from 0 to 1, should be 1 found 0 |
64 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
1180 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
10 ms |
1100 KB |
Output is correct |
13 |
Correct |
269 ms |
22024 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
704 KB |
Output is correct |
17 |
Correct |
105 ms |
12024 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
61 ms |
5700 KB |
Output is correct |
21 |
Correct |
253 ms |
22016 KB |
Output is correct |
22 |
Correct |
293 ms |
21988 KB |
Output is correct |
23 |
Correct |
254 ms |
21928 KB |
Output is correct |
24 |
Correct |
231 ms |
21960 KB |
Output is correct |
25 |
Correct |
101 ms |
12028 KB |
Output is correct |
26 |
Correct |
113 ms |
12044 KB |
Output is correct |
27 |
Correct |
300 ms |
22020 KB |
Output is correct |
28 |
Correct |
241 ms |
21956 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
12 ms |
1108 KB |
Output is correct |
37 |
Correct |
237 ms |
21940 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
1280 KB |
Output is correct |
41 |
Correct |
296 ms |
25924 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
104 ms |
12044 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
59 ms |
6656 KB |
Output is correct |
50 |
Correct |
236 ms |
25976 KB |
Output is correct |
51 |
Correct |
234 ms |
25924 KB |
Output is correct |
52 |
Correct |
260 ms |
26076 KB |
Output is correct |
53 |
Correct |
107 ms |
16124 KB |
Output is correct |
54 |
Correct |
96 ms |
12032 KB |
Output is correct |
55 |
Correct |
240 ms |
25992 KB |
Output is correct |
56 |
Correct |
259 ms |
25980 KB |
Output is correct |
57 |
Correct |
98 ms |
16120 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
59 ms |
5728 KB |
Output is correct |
62 |
Correct |
239 ms |
22012 KB |
Output is correct |
63 |
Correct |
237 ms |
22084 KB |
Output is correct |
64 |
Correct |
248 ms |
22084 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
74 ms |
6720 KB |
Output is correct |
67 |
Correct |
267 ms |
25932 KB |
Output is correct |
68 |
Correct |
236 ms |
25924 KB |
Output is correct |
69 |
Correct |
269 ms |
25980 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Incorrect |
0 ms |
204 KB |
Too few ways to get from 4 to 5, should be 1 found 0 |
72 |
Halted |
0 ms |
0 KB |
- |