#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 bb[N];
void dfs1(int p, int i, int b) {
int o;
bb[i] = b;
for (o = eo[i]; o--; ) {
int j = ej[i][o], g = eg[i][o];
if (j != p) {
if (g == -1)
dfs1(i, j, b);
else
vv[g] = i, dfs1(i, j, b | 1 << g);
}
}
}
int dd[N];
void dfs2(int p, int i, int d) {
int o;
dd[i] = d;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j, d + 1);
}
}
int main() {
static int dds[M], ddt[M], ddd[M][M], bb_[M], dp[1 << M][M];
int g, g_, h, i, j, u, b, s, t, 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);
if (bb[t] == 0) {
dfs2(-1, s, 0);
printf("%d\n", dd[t]);
return 0;
}
for (g_ = 0; g_ < m; g_++) {
dfs2(-1, uu[g_], 0);
dds[g_] = dd[s] + dd[vv[g_]];
for (g = 0; g < m; g++)
ddd[g][g_] = dd[vv[g]] + dd[vv[g_]];
}
dfs2(-1, t, 0);
for (g = 0; g < m; g++)
ddt[g] = dd[vv[g]];
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[uu[g]] == 0 && bb[vv[g]] == 0)
dp[1 << g][g] = dds[g];
ans = INF;
for (b = 1; b < 1 << m; b++)
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 (g_ = 0; g_ < m; g_++)
if ((b & 1 << g_) == 0 && (bb_[g_] & b) == bb_[g_])
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:62:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d%d", &n, &s, &t), s--, t--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:69:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d%d", &i, &j, &u), i--, j--, u--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
70 ms |
9884 KB |
Output is correct |
4 |
Correct |
81 ms |
9824 KB |
Output is correct |
5 |
Correct |
60 ms |
9916 KB |
Output is correct |
6 |
Correct |
89 ms |
9784 KB |
Output is correct |
7 |
Correct |
103 ms |
9920 KB |
Output is correct |
8 |
Correct |
70 ms |
9916 KB |
Output is correct |
9 |
Correct |
59 ms |
10108 KB |
Output is correct |
10 |
Correct |
75 ms |
9936 KB |
Output is correct |
11 |
Correct |
58 ms |
9916 KB |
Output is correct |
12 |
Correct |
94 ms |
11836 KB |
Output is correct |
13 |
Correct |
105 ms |
12616 KB |
Output is correct |
14 |
Correct |
66 ms |
11484 KB |
Output is correct |
15 |
Correct |
68 ms |
11732 KB |
Output is correct |
16 |
Correct |
74 ms |
12756 KB |
Output is correct |
17 |
Correct |
80 ms |
12732 KB |
Output is correct |
18 |
Correct |
107 ms |
13164 KB |
Output is correct |
19 |
Correct |
132 ms |
17000 KB |
Output is correct |
20 |
Correct |
99 ms |
16972 KB |
Output is correct |
21 |
Correct |
81 ms |
17012 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
26 ms |
41920 KB |
Output is correct |
6 |
Correct |
29 ms |
41940 KB |
Output is correct |
7 |
Correct |
28 ms |
41940 KB |
Output is correct |
8 |
Correct |
28 ms |
42052 KB |
Output is correct |
9 |
Correct |
28 ms |
41964 KB |
Output is correct |
10 |
Correct |
26 ms |
41940 KB |
Output is correct |
11 |
Correct |
27 ms |
42072 KB |
Output is correct |
12 |
Correct |
26 ms |
42060 KB |
Output is correct |
13 |
Correct |
26 ms |
42060 KB |
Output is correct |
14 |
Correct |
31 ms |
41948 KB |
Output is correct |
15 |
Correct |
26 ms |
42100 KB |
Output is correct |
16 |
Correct |
27 ms |
42028 KB |
Output is correct |
17 |
Correct |
35 ms |
42060 KB |
Output is correct |
18 |
Correct |
27 ms |
42184 KB |
Output is correct |
19 |
Correct |
30 ms |
42136 KB |
Output is correct |
20 |
Correct |
36 ms |
42200 KB |
Output is correct |
21 |
Correct |
26 ms |
42220 KB |
Output is correct |
22 |
Correct |
28 ms |
42516 KB |
Output is correct |
23 |
Correct |
27 ms |
42452 KB |
Output is correct |
24 |
Correct |
27 ms |
42524 KB |
Output is correct |
25 |
Correct |
595 ms |
82392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
70 ms |
9884 KB |
Output is correct |
4 |
Correct |
81 ms |
9824 KB |
Output is correct |
5 |
Correct |
60 ms |
9916 KB |
Output is correct |
6 |
Correct |
89 ms |
9784 KB |
Output is correct |
7 |
Correct |
103 ms |
9920 KB |
Output is correct |
8 |
Correct |
70 ms |
9916 KB |
Output is correct |
9 |
Correct |
59 ms |
10108 KB |
Output is correct |
10 |
Correct |
75 ms |
9936 KB |
Output is correct |
11 |
Correct |
58 ms |
9916 KB |
Output is correct |
12 |
Correct |
94 ms |
11836 KB |
Output is correct |
13 |
Correct |
105 ms |
12616 KB |
Output is correct |
14 |
Correct |
66 ms |
11484 KB |
Output is correct |
15 |
Correct |
68 ms |
11732 KB |
Output is correct |
16 |
Correct |
74 ms |
12756 KB |
Output is correct |
17 |
Correct |
80 ms |
12732 KB |
Output is correct |
18 |
Correct |
107 ms |
13164 KB |
Output is correct |
19 |
Correct |
132 ms |
17000 KB |
Output is correct |
20 |
Correct |
99 ms |
16972 KB |
Output is correct |
21 |
Correct |
81 ms |
17012 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 |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
26 ms |
41920 KB |
Output is correct |
32 |
Correct |
29 ms |
41940 KB |
Output is correct |
33 |
Correct |
28 ms |
41940 KB |
Output is correct |
34 |
Correct |
28 ms |
42052 KB |
Output is correct |
35 |
Correct |
28 ms |
41964 KB |
Output is correct |
36 |
Correct |
26 ms |
41940 KB |
Output is correct |
37 |
Correct |
27 ms |
42072 KB |
Output is correct |
38 |
Correct |
26 ms |
42060 KB |
Output is correct |
39 |
Correct |
26 ms |
42060 KB |
Output is correct |
40 |
Correct |
31 ms |
41948 KB |
Output is correct |
41 |
Correct |
26 ms |
42100 KB |
Output is correct |
42 |
Correct |
27 ms |
42028 KB |
Output is correct |
43 |
Correct |
35 ms |
42060 KB |
Output is correct |
44 |
Correct |
27 ms |
42184 KB |
Output is correct |
45 |
Correct |
30 ms |
42136 KB |
Output is correct |
46 |
Correct |
36 ms |
42200 KB |
Output is correct |
47 |
Correct |
26 ms |
42220 KB |
Output is correct |
48 |
Correct |
28 ms |
42516 KB |
Output is correct |
49 |
Correct |
27 ms |
42452 KB |
Output is correct |
50 |
Correct |
27 ms |
42524 KB |
Output is correct |
51 |
Correct |
595 ms |
82392 KB |
Output is correct |
52 |
Execution timed out |
2080 ms |
99224 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |