// In the name of God
//#pragma GCC optimize("O2", "unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 1e6 + 5, lg = 21;
int n, c, s, t;
vector<int> adj[N];
vector<pair<int, int> > E;
int f[N], tin[N], tout[N], T, h[N], up[N][lg];
int dis[lg][N];
ll dp[1 << lg][lg];
bool ok[1 << lg][lg];
void dfs(int v, int p) {
tin[v] = ++T;
up[v][0] = p;
for (int i = 1; i < lg; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (auto u : adj[v]) {
if (u != p) {
h[u] = h[v] + 1;
dfs(u, v);
}
}
tout[v] = ++T;
}
void dfs2(int v, int p, int k) {
for (auto u : adj[v]) {
if (u != p) {
dis[k][u] = dis[k][v] + 1;
dfs2(u, v, k);
}
}
}
bool anc(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int lca(int v, int u) {
if (tin[v] > tin[u])
swap(v, u);
if (anc(v, u))
return v;
for (int i = lg - 1; i >= 0; i--)
if (!anc(up[u][i], v))
u = up[u][i];
return up[u][0];
}
void solve() {
set<int> st;
cin >> n >> s >> t;
for (int i = 0; i < n - 1; i++) {
int v, u, w; cin >> v >> u >> w;
adj[v].pb(u), adj[u].pb(v);
if (w) {
f[(int)E.size()] = w, E.pb(mp(v, u));
st.insert(w);
}
}
st.insert(t);
dfs(s, 0);
tout[0] = ++T;
c = (int)E.size();
assert(c <= 20);
memset(dis, 63, sizeof dis);
for (int i = 0; i < c; i++) {
if (tin[E[i].fi] > tin[E[i].se])
swap(E[i].fi, E[i].se);
//dfs2(E[i].fi, 0, i);
for (auto v : st) {
//assert(dis[i][v] == h[E[i].fi] + h[v] - 2 * h[lca(E[i].fi, v)]);
dis[i][v] = h[E[i].fi] + h[v] - 2 * h[lca(E[i].fi, v)];
}
}
E.pb(mp(t, t));
f[c] = t;
for (int mask = 0; mask < (1 << c); mask++) {
for (int i = 0; i <= c; i++) {
int v = E[i].fi;
ok[mask][i] = true;
for (int j = 0; j < c; j++) {
ok[mask][i] &= (mask & (1 << j)) || (!anc(E[j].se, v) && !anc(E[j].se, f[i]));
}
}
}
memset(dp, 63, sizeof dp);
for (int i = 0; i < c; i++) {
if (ok[1 << i][i]) {
dp[1 << i][i] = h[f[i]] + dis[i][f[i]];
}
}
ll ans = 1e18;
if (ok[0][c]) {
ans = h[t];
}
for (int mask = 1; mask < (1 << c); mask++) {
for (int i = 0; i < c; i++) {
if (!(mask & (1 << i)))
continue;
int mask2 = mask ^ (1 << i);
for (int j = 0; j < c; j++) {
if (mask2 & (1 << j)) {
if (ok[mask2][i]) {
dp[mask][i] = min(dp[mask][i], dp[mask2][j] + dis[j][f[i]] + dis[i][f[i]]);
}
}
}
if (ok[mask][c]) {
ans = min(ans, dp[mask][i] + dis[i][t]);
}
}
}
if (ans < 1e18)
cout << ans << '\n';
else
cout << -1 << '\n';
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
450680 KB |
Output is correct |
2 |
Correct |
142 ms |
450636 KB |
Output is correct |
3 |
Correct |
191 ms |
463612 KB |
Output is correct |
4 |
Correct |
189 ms |
464840 KB |
Output is correct |
5 |
Correct |
192 ms |
464776 KB |
Output is correct |
6 |
Correct |
205 ms |
464776 KB |
Output is correct |
7 |
Correct |
195 ms |
464960 KB |
Output is correct |
8 |
Correct |
202 ms |
464928 KB |
Output is correct |
9 |
Correct |
203 ms |
465064 KB |
Output is correct |
10 |
Correct |
198 ms |
464924 KB |
Output is correct |
11 |
Correct |
194 ms |
465016 KB |
Output is correct |
12 |
Correct |
203 ms |
466400 KB |
Output is correct |
13 |
Correct |
192 ms |
466080 KB |
Output is correct |
14 |
Correct |
195 ms |
465732 KB |
Output is correct |
15 |
Correct |
199 ms |
466568 KB |
Output is correct |
16 |
Correct |
196 ms |
467072 KB |
Output is correct |
17 |
Correct |
202 ms |
467080 KB |
Output is correct |
18 |
Correct |
193 ms |
467552 KB |
Output is correct |
19 |
Correct |
207 ms |
470684 KB |
Output is correct |
20 |
Correct |
209 ms |
470596 KB |
Output is correct |
21 |
Correct |
215 ms |
470404 KB |
Output is correct |
22 |
Correct |
141 ms |
450684 KB |
Output is correct |
23 |
Correct |
140 ms |
450688 KB |
Output is correct |
24 |
Correct |
156 ms |
450748 KB |
Output is correct |
25 |
Correct |
142 ms |
450764 KB |
Output is correct |
26 |
Correct |
144 ms |
450728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
450656 KB |
Output is correct |
2 |
Correct |
144 ms |
450652 KB |
Output is correct |
3 |
Correct |
143 ms |
450708 KB |
Output is correct |
4 |
Correct |
141 ms |
450852 KB |
Output is correct |
5 |
Correct |
1380 ms |
462528 KB |
Output is correct |
6 |
Correct |
1366 ms |
462520 KB |
Output is correct |
7 |
Correct |
1330 ms |
462508 KB |
Output is correct |
8 |
Correct |
1348 ms |
462512 KB |
Output is correct |
9 |
Correct |
1357 ms |
462436 KB |
Output is correct |
10 |
Correct |
1219 ms |
462440 KB |
Output is correct |
11 |
Correct |
1197 ms |
462524 KB |
Output is correct |
12 |
Correct |
1218 ms |
462520 KB |
Output is correct |
13 |
Correct |
1181 ms |
462540 KB |
Output is correct |
14 |
Correct |
1155 ms |
462440 KB |
Output is correct |
15 |
Correct |
1296 ms |
462540 KB |
Output is correct |
16 |
Correct |
1308 ms |
462548 KB |
Output is correct |
17 |
Correct |
1303 ms |
462556 KB |
Output is correct |
18 |
Correct |
1179 ms |
462660 KB |
Output is correct |
19 |
Correct |
1138 ms |
462776 KB |
Output is correct |
20 |
Correct |
1172 ms |
462748 KB |
Output is correct |
21 |
Correct |
1144 ms |
462660 KB |
Output is correct |
22 |
Correct |
1104 ms |
462948 KB |
Output is correct |
23 |
Correct |
1110 ms |
462912 KB |
Output is correct |
24 |
Correct |
1114 ms |
462836 KB |
Output is correct |
25 |
Correct |
1862 ms |
472276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
450680 KB |
Output is correct |
2 |
Correct |
142 ms |
450636 KB |
Output is correct |
3 |
Correct |
191 ms |
463612 KB |
Output is correct |
4 |
Correct |
189 ms |
464840 KB |
Output is correct |
5 |
Correct |
192 ms |
464776 KB |
Output is correct |
6 |
Correct |
205 ms |
464776 KB |
Output is correct |
7 |
Correct |
195 ms |
464960 KB |
Output is correct |
8 |
Correct |
202 ms |
464928 KB |
Output is correct |
9 |
Correct |
203 ms |
465064 KB |
Output is correct |
10 |
Correct |
198 ms |
464924 KB |
Output is correct |
11 |
Correct |
194 ms |
465016 KB |
Output is correct |
12 |
Correct |
203 ms |
466400 KB |
Output is correct |
13 |
Correct |
192 ms |
466080 KB |
Output is correct |
14 |
Correct |
195 ms |
465732 KB |
Output is correct |
15 |
Correct |
199 ms |
466568 KB |
Output is correct |
16 |
Correct |
196 ms |
467072 KB |
Output is correct |
17 |
Correct |
202 ms |
467080 KB |
Output is correct |
18 |
Correct |
193 ms |
467552 KB |
Output is correct |
19 |
Correct |
207 ms |
470684 KB |
Output is correct |
20 |
Correct |
209 ms |
470596 KB |
Output is correct |
21 |
Correct |
215 ms |
470404 KB |
Output is correct |
22 |
Correct |
141 ms |
450684 KB |
Output is correct |
23 |
Correct |
140 ms |
450688 KB |
Output is correct |
24 |
Correct |
156 ms |
450748 KB |
Output is correct |
25 |
Correct |
142 ms |
450764 KB |
Output is correct |
26 |
Correct |
144 ms |
450728 KB |
Output is correct |
27 |
Correct |
142 ms |
450656 KB |
Output is correct |
28 |
Correct |
144 ms |
450652 KB |
Output is correct |
29 |
Correct |
143 ms |
450708 KB |
Output is correct |
30 |
Correct |
141 ms |
450852 KB |
Output is correct |
31 |
Correct |
1380 ms |
462528 KB |
Output is correct |
32 |
Correct |
1366 ms |
462520 KB |
Output is correct |
33 |
Correct |
1330 ms |
462508 KB |
Output is correct |
34 |
Correct |
1348 ms |
462512 KB |
Output is correct |
35 |
Correct |
1357 ms |
462436 KB |
Output is correct |
36 |
Correct |
1219 ms |
462440 KB |
Output is correct |
37 |
Correct |
1197 ms |
462524 KB |
Output is correct |
38 |
Correct |
1218 ms |
462520 KB |
Output is correct |
39 |
Correct |
1181 ms |
462540 KB |
Output is correct |
40 |
Correct |
1155 ms |
462440 KB |
Output is correct |
41 |
Correct |
1296 ms |
462540 KB |
Output is correct |
42 |
Correct |
1308 ms |
462548 KB |
Output is correct |
43 |
Correct |
1303 ms |
462556 KB |
Output is correct |
44 |
Correct |
1179 ms |
462660 KB |
Output is correct |
45 |
Correct |
1138 ms |
462776 KB |
Output is correct |
46 |
Correct |
1172 ms |
462748 KB |
Output is correct |
47 |
Correct |
1144 ms |
462660 KB |
Output is correct |
48 |
Correct |
1104 ms |
462948 KB |
Output is correct |
49 |
Correct |
1110 ms |
462912 KB |
Output is correct |
50 |
Correct |
1114 ms |
462836 KB |
Output is correct |
51 |
Correct |
1862 ms |
472276 KB |
Output is correct |
52 |
Correct |
1480 ms |
585548 KB |
Output is correct |
53 |
Correct |
1408 ms |
585596 KB |
Output is correct |
54 |
Correct |
1289 ms |
588216 KB |
Output is correct |
55 |
Correct |
1245 ms |
587920 KB |
Output is correct |
56 |
Correct |
1281 ms |
588772 KB |
Output is correct |
57 |
Correct |
1257 ms |
589992 KB |
Output is correct |
58 |
Correct |
148 ms |
450708 KB |
Output is correct |
59 |
Correct |
147 ms |
450676 KB |
Output is correct |
60 |
Correct |
143 ms |
450692 KB |
Output is correct |
61 |
Correct |
1243 ms |
590296 KB |
Output is correct |
62 |
Correct |
1478 ms |
590444 KB |
Output is correct |
63 |
Correct |
1307 ms |
596044 KB |
Output is correct |
64 |
Correct |
197 ms |
466396 KB |
Output is correct |
65 |
Correct |
199 ms |
466636 KB |
Output is correct |
66 |
Correct |
1234 ms |
597268 KB |
Output is correct |
67 |
Correct |
1290 ms |
596880 KB |
Output is correct |
68 |
Execution timed out |
2105 ms |
604064 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |