This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |