#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000000
#define M 20
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int n, m;
int *ej[N], *eg[N], eo[N];
int uu[M], vv[M];
void append(int i, int j, int g) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
eg[i] = (int *) realloc(eg[i], o * 2 * sizeof *eg[i]);
}
ej[i][o] = j, eg[i][o] = g;
}
int dd[N], pp[N], qq[N], bb[N];
int dfs1(int p, int i, int d, int b) {
int o, s, k_, j_;
pp[i] = p, dd[i] = d, bb[i] = b;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], g = eg[i][o];
if (j != p) {
int k = dfs1(i, j, d + 1, g == -1 ? b : b | 1 << g);
s += k;
if (k_ < k)
k_ = k, j_ = j;
if (g != -1)
vv[g] = i;
}
}
qq[i] = j_;
return s;
}
void dfs2(int p, int i, int q) {
int o, j_;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(i, j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && j != j_)
dfs2(i, j, j);
}
}
int lca(int i, int j) {
while (qq[i] != qq[j])
if (dd[qq[i]] > dd[qq[j]])
i = pp[qq[i]];
else
j = pp[qq[j]];
return dd[i] < dd[j] ? i : j;
}
int dist(int i, int j) {
return dd[i] + dd[j] - dd[lca(i, j)] * 2;
}
int main() {
static int dds[M], ddt[M], ddd[M][M], bb_[M], dp[1 << M][M], gg[M];
int k, g, g_, h, i, j, u, b, s, t, d, ans;
scanf("%d%d%d", &n, &s, &t), s--, t--;
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
eg[i] = (int *) malloc(2 * sizeof *eg[i]);
}
m = 0;
for (h = 0; h < n - 1; h++) {
scanf("%d%d%d", &i, &j, &u), i--, j--, u--;
if (u == -1)
g = -1;
else
uu[g = m++] = u;
append(i, j, g), append(j, i, g);
}
dfs1(-1, s, 0, 0);
dfs2(-1, s, s);
if (bb[t] == 0) {
printf("%d\n", dd[t]);
return 0;
}
for (g_ = 0; g_ < m; g_++) {
d = dist(uu[g_], vv[g_]);
dds[g_] = dd[uu[g_]] + d;
for (g = 0; g < m; g++)
ddd[g][g_] = dist(vv[g], uu[g_]) + d;
}
for (g = 0; g < m; g++)
ddt[g] = dist(vv[g], t);
for (g = 0; g < m; g++)
bb_[g] = bb[uu[g]] | bb[vv[g]];
for (b = 1; b < 1 << m; b++)
memset(dp[b], 0x3f, m * sizeof *dp[b]);
for (g = 0; g < m; g++)
if (bb_[g] == 0)
dp[1 << g][g] = dds[g];
ans = INF;
for (b = 1; b < 1 << m; b++) {
k = 0;
for (g = 0; g < m; g++)
if ((b & 1 << g) == 0 && (bb_[g] & b) == bb_[g])
gg[k++] = g;
for (g = 0; g < m; g++) {
int x = dp[b][g];
if (x == INF)
continue;
if ((bb[t] & b) == bb[t])
ans = min(ans, x + ddt[g]);
for (h = 0; h < k; h++) {
g_ = gg[h];
dp[b | 1 << g_][g_] = min(dp[b | 1 << g_][g_], x + ddd[g][g_]);
}
}
}
if (ans == INF)
ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:81:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d%d%d", &n, &s, &t), s--, t--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:88:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d%d%d", &i, &j, &u), i--, j--, u--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
55 ms |
10824 KB |
Output is correct |
4 |
Correct |
43 ms |
10688 KB |
Output is correct |
5 |
Correct |
44 ms |
10820 KB |
Output is correct |
6 |
Correct |
39 ms |
10808 KB |
Output is correct |
7 |
Correct |
51 ms |
10808 KB |
Output is correct |
8 |
Correct |
68 ms |
10800 KB |
Output is correct |
9 |
Correct |
40 ms |
11052 KB |
Output is correct |
10 |
Correct |
54 ms |
10920 KB |
Output is correct |
11 |
Correct |
39 ms |
10792 KB |
Output is correct |
12 |
Correct |
43 ms |
12892 KB |
Output is correct |
13 |
Correct |
46 ms |
12436 KB |
Output is correct |
14 |
Correct |
47 ms |
12008 KB |
Output is correct |
15 |
Correct |
60 ms |
13152 KB |
Output is correct |
16 |
Correct |
40 ms |
13920 KB |
Output is correct |
17 |
Correct |
39 ms |
14048 KB |
Output is correct |
18 |
Correct |
45 ms |
14556 KB |
Output is correct |
19 |
Correct |
67 ms |
19544 KB |
Output is correct |
20 |
Correct |
45 ms |
19284 KB |
Output is correct |
21 |
Correct |
45 ms |
19008 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
67 ms |
42068 KB |
Output is correct |
6 |
Correct |
68 ms |
42188 KB |
Output is correct |
7 |
Correct |
68 ms |
42068 KB |
Output is correct |
8 |
Correct |
67 ms |
42196 KB |
Output is correct |
9 |
Correct |
67 ms |
42184 KB |
Output is correct |
10 |
Correct |
64 ms |
42196 KB |
Output is correct |
11 |
Correct |
64 ms |
42184 KB |
Output is correct |
12 |
Correct |
64 ms |
42200 KB |
Output is correct |
13 |
Correct |
64 ms |
42216 KB |
Output is correct |
14 |
Correct |
65 ms |
42208 KB |
Output is correct |
15 |
Correct |
66 ms |
42196 KB |
Output is correct |
16 |
Correct |
66 ms |
42164 KB |
Output is correct |
17 |
Correct |
66 ms |
42188 KB |
Output is correct |
18 |
Correct |
78 ms |
42372 KB |
Output is correct |
19 |
Correct |
75 ms |
42328 KB |
Output is correct |
20 |
Correct |
63 ms |
42376 KB |
Output is correct |
21 |
Correct |
74 ms |
42404 KB |
Output is correct |
22 |
Correct |
63 ms |
42776 KB |
Output is correct |
23 |
Correct |
74 ms |
42708 KB |
Output is correct |
24 |
Correct |
64 ms |
42712 KB |
Output is correct |
25 |
Correct |
330 ms |
82420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
55 ms |
10824 KB |
Output is correct |
4 |
Correct |
43 ms |
10688 KB |
Output is correct |
5 |
Correct |
44 ms |
10820 KB |
Output is correct |
6 |
Correct |
39 ms |
10808 KB |
Output is correct |
7 |
Correct |
51 ms |
10808 KB |
Output is correct |
8 |
Correct |
68 ms |
10800 KB |
Output is correct |
9 |
Correct |
40 ms |
11052 KB |
Output is correct |
10 |
Correct |
54 ms |
10920 KB |
Output is correct |
11 |
Correct |
39 ms |
10792 KB |
Output is correct |
12 |
Correct |
43 ms |
12892 KB |
Output is correct |
13 |
Correct |
46 ms |
12436 KB |
Output is correct |
14 |
Correct |
47 ms |
12008 KB |
Output is correct |
15 |
Correct |
60 ms |
13152 KB |
Output is correct |
16 |
Correct |
40 ms |
13920 KB |
Output is correct |
17 |
Correct |
39 ms |
14048 KB |
Output is correct |
18 |
Correct |
45 ms |
14556 KB |
Output is correct |
19 |
Correct |
67 ms |
19544 KB |
Output is correct |
20 |
Correct |
45 ms |
19284 KB |
Output is correct |
21 |
Correct |
45 ms |
19008 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
296 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
67 ms |
42068 KB |
Output is correct |
32 |
Correct |
68 ms |
42188 KB |
Output is correct |
33 |
Correct |
68 ms |
42068 KB |
Output is correct |
34 |
Correct |
67 ms |
42196 KB |
Output is correct |
35 |
Correct |
67 ms |
42184 KB |
Output is correct |
36 |
Correct |
64 ms |
42196 KB |
Output is correct |
37 |
Correct |
64 ms |
42184 KB |
Output is correct |
38 |
Correct |
64 ms |
42200 KB |
Output is correct |
39 |
Correct |
64 ms |
42216 KB |
Output is correct |
40 |
Correct |
65 ms |
42208 KB |
Output is correct |
41 |
Correct |
66 ms |
42196 KB |
Output is correct |
42 |
Correct |
66 ms |
42164 KB |
Output is correct |
43 |
Correct |
66 ms |
42188 KB |
Output is correct |
44 |
Correct |
78 ms |
42372 KB |
Output is correct |
45 |
Correct |
75 ms |
42328 KB |
Output is correct |
46 |
Correct |
63 ms |
42376 KB |
Output is correct |
47 |
Correct |
74 ms |
42404 KB |
Output is correct |
48 |
Correct |
63 ms |
42776 KB |
Output is correct |
49 |
Correct |
74 ms |
42708 KB |
Output is correct |
50 |
Correct |
64 ms |
42712 KB |
Output is correct |
51 |
Correct |
330 ms |
82420 KB |
Output is correct |
52 |
Correct |
931 ms |
108348 KB |
Output is correct |
53 |
Correct |
1025 ms |
123764 KB |
Output is correct |
54 |
Correct |
1006 ms |
120944 KB |
Output is correct |
55 |
Correct |
997 ms |
120364 KB |
Output is correct |
56 |
Correct |
976 ms |
121548 KB |
Output is correct |
57 |
Correct |
952 ms |
123528 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
963 ms |
123924 KB |
Output is correct |
62 |
Correct |
1012 ms |
130356 KB |
Output is correct |
63 |
Correct |
1010 ms |
125300 KB |
Output is correct |
64 |
Correct |
45 ms |
14284 KB |
Output is correct |
65 |
Correct |
43 ms |
14648 KB |
Output is correct |
66 |
Correct |
949 ms |
123820 KB |
Output is correct |
67 |
Correct |
970 ms |
123500 KB |
Output is correct |
68 |
Correct |
987 ms |
158940 KB |
Output is correct |
69 |
Correct |
977 ms |
159572 KB |
Output is correct |
70 |
Correct |
983 ms |
158656 KB |
Output is correct |
71 |
Correct |
1040 ms |
158616 KB |
Output is correct |
72 |
Correct |
1004 ms |
158656 KB |
Output is correct |
73 |
Correct |
1156 ms |
160620 KB |
Output is correct |
74 |
Correct |
1224 ms |
161464 KB |
Output is correct |
75 |
Correct |
1057 ms |
161172 KB |
Output is correct |
76 |
Correct |
1161 ms |
162376 KB |
Output is correct |
77 |
Correct |
1604 ms |
160796 KB |
Output is correct |
78 |
Correct |
1050 ms |
168400 KB |
Output is correct |
79 |
Correct |
1219 ms |
167864 KB |
Output is correct |
80 |
Correct |
1163 ms |
166160 KB |
Output is correct |
81 |
Correct |
1140 ms |
185788 KB |
Output is correct |
82 |
Correct |
1254 ms |
187756 KB |
Output is correct |
83 |
Correct |
1083 ms |
187592 KB |
Output is correct |
84 |
Correct |
1108 ms |
200916 KB |
Output is correct |
85 |
Correct |
1171 ms |
246508 KB |
Output is correct |
86 |
Correct |
1086 ms |
245160 KB |
Output is correct |
87 |
Correct |
1357 ms |
245296 KB |
Output is correct |