#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6 + 10 , maxm = 21 , inf = 1e9;
int n , key[maxm] , s , t , t_path[maxm] , door[maxm] , par[maxm][maxn] , dis[maxn] , path[maxm][maxm];
int mask[maxn] , dp[maxm][(1 << maxm)] , ps;
struct edge{
int v , lock;
};
vector <edge> adj[maxn];
void dfs(int v)
{
for(auto u : adj[v]) if(u.v != par[0][v])
{
par[0][u.v] = v;
dis[u.v] = dis[v] + 1;
mask[u.v] = mask[v];
if(u.lock != -1)
{
door[u.lock] = v;
mask[u.v] |= (1 << u.lock);
}
dfs(u.v);
}
}
int lca(int a , int b)
{
if(dis[a] < dis[b]) swap(a , b);
for(int i = 0 ; i < maxm ; i++) if(((dis[a] - dis[b]) >> i) & 1) a = par[i][a];
if(a == b) return a;
for(int i = maxm - 1 ; i > -1 ; i--) if(par[i][a] != par[i][b])
{
a = par[i][a]; b = par[i][b];
}
return par[0][a];
}
void solve(int v , int msk)
{
if(dp[v][msk] != -1) return;
if(!(mask[t] & msk))
{
dp[v][msk] = t_path[v];
return;
}
dp[v][msk] = inf;
for(int i = 0 ; i < ps ; i++) if(((msk >> i) & 1))
{
int tmask = (mask[key[i]] | mask[door[i]]);
if(!(tmask & msk))
{
solve(i , (msk ^ (1 << i)));
dp[v][msk] = min(dp[v][msk] , dp[i][(msk ^ (1 << i))] + path[v][i]);
}
}
}
int32_t main()
{
fast;
cin >> n;
cin >> s >> t;
for(int i = 0 ; i < n - 1 ; i++)
{
int v , u , w; cin >> v >> u >> w;
int tmp = -1;
if(w != 0)
{
tmp = ps;
key[ps] = w;
ps++;
}
adj[v].pb({u , tmp}); adj[u].pb({v , tmp});
}
door[ps] = s;
dfs(s);
for(int i = 1 ; i < maxm ; i++)
for(int j = 1 ; j <= n ; j++)
par[i][j] = par[i - 1][par[i - 1][j]];
for(int i = 0 ; i <= ps ; i++) for(int j = 0 ; j < ps ; j++) if(i != j)
{
path[i][j] = dis[door[i]] + dis[key[j]] + dis[key[j]] + dis[door[j]];
int l1 = lca(door[i] , key[j]) , l2 = lca(door[j] , key[j]);
path[i][j] -= (dis[l1] + dis[l2] + dis[l2] + dis[l1]);
}
for(int i = 0 ; i <= ps ; i++)
{
int l = lca(t , door[i]); t_path[i] = dis[door[i]] + dis[t] - dis[l] - dis[l];
}
memset(dp , -1 , sizeof dp);
solve(ps , (1 << ps) - 1);
cout << (dp[ps][(1 << ps) - 1] == inf ? -1 : dp[ps][(1 << ps) - 1]) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196320 KB |
Output is correct |
3 |
Correct |
117 ms |
209164 KB |
Output is correct |
4 |
Correct |
99 ms |
209244 KB |
Output is correct |
5 |
Correct |
100 ms |
209236 KB |
Output is correct |
6 |
Correct |
98 ms |
209248 KB |
Output is correct |
7 |
Correct |
98 ms |
209356 KB |
Output is correct |
8 |
Correct |
101 ms |
209340 KB |
Output is correct |
9 |
Correct |
102 ms |
209528 KB |
Output is correct |
10 |
Correct |
98 ms |
209312 KB |
Output is correct |
11 |
Correct |
98 ms |
209292 KB |
Output is correct |
12 |
Correct |
121 ms |
210704 KB |
Output is correct |
13 |
Correct |
110 ms |
210356 KB |
Output is correct |
14 |
Correct |
108 ms |
209900 KB |
Output is correct |
15 |
Correct |
98 ms |
210896 KB |
Output is correct |
16 |
Correct |
98 ms |
212972 KB |
Output is correct |
17 |
Correct |
101 ms |
212968 KB |
Output is correct |
18 |
Correct |
109 ms |
213376 KB |
Output is correct |
19 |
Correct |
101 ms |
217448 KB |
Output is correct |
20 |
Correct |
121 ms |
217292 KB |
Output is correct |
21 |
Correct |
101 ms |
217124 KB |
Output is correct |
22 |
Correct |
67 ms |
196300 KB |
Output is correct |
23 |
Correct |
66 ms |
196316 KB |
Output is correct |
24 |
Correct |
65 ms |
196348 KB |
Output is correct |
25 |
Correct |
66 ms |
196332 KB |
Output is correct |
26 |
Correct |
64 ms |
196280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
196244 KB |
Output is correct |
2 |
Correct |
75 ms |
196260 KB |
Output is correct |
3 |
Correct |
67 ms |
196264 KB |
Output is correct |
4 |
Correct |
69 ms |
196300 KB |
Output is correct |
5 |
Correct |
68 ms |
197220 KB |
Output is correct |
6 |
Correct |
69 ms |
197212 KB |
Output is correct |
7 |
Correct |
72 ms |
197128 KB |
Output is correct |
8 |
Correct |
71 ms |
197188 KB |
Output is correct |
9 |
Correct |
68 ms |
197244 KB |
Output is correct |
10 |
Correct |
73 ms |
197176 KB |
Output is correct |
11 |
Correct |
75 ms |
197196 KB |
Output is correct |
12 |
Correct |
74 ms |
197192 KB |
Output is correct |
13 |
Correct |
78 ms |
197172 KB |
Output is correct |
14 |
Correct |
67 ms |
197224 KB |
Output is correct |
15 |
Correct |
65 ms |
197196 KB |
Output is correct |
16 |
Correct |
69 ms |
197328 KB |
Output is correct |
17 |
Correct |
66 ms |
197188 KB |
Output is correct |
18 |
Correct |
75 ms |
197316 KB |
Output is correct |
19 |
Correct |
76 ms |
197344 KB |
Output is correct |
20 |
Correct |
67 ms |
197384 KB |
Output is correct |
21 |
Correct |
67 ms |
197424 KB |
Output is correct |
22 |
Correct |
73 ms |
197708 KB |
Output is correct |
23 |
Correct |
66 ms |
197640 KB |
Output is correct |
24 |
Correct |
68 ms |
197708 KB |
Output is correct |
25 |
Correct |
1238 ms |
196344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
196300 KB |
Output is correct |
2 |
Correct |
71 ms |
196320 KB |
Output is correct |
3 |
Correct |
117 ms |
209164 KB |
Output is correct |
4 |
Correct |
99 ms |
209244 KB |
Output is correct |
5 |
Correct |
100 ms |
209236 KB |
Output is correct |
6 |
Correct |
98 ms |
209248 KB |
Output is correct |
7 |
Correct |
98 ms |
209356 KB |
Output is correct |
8 |
Correct |
101 ms |
209340 KB |
Output is correct |
9 |
Correct |
102 ms |
209528 KB |
Output is correct |
10 |
Correct |
98 ms |
209312 KB |
Output is correct |
11 |
Correct |
98 ms |
209292 KB |
Output is correct |
12 |
Correct |
121 ms |
210704 KB |
Output is correct |
13 |
Correct |
110 ms |
210356 KB |
Output is correct |
14 |
Correct |
108 ms |
209900 KB |
Output is correct |
15 |
Correct |
98 ms |
210896 KB |
Output is correct |
16 |
Correct |
98 ms |
212972 KB |
Output is correct |
17 |
Correct |
101 ms |
212968 KB |
Output is correct |
18 |
Correct |
109 ms |
213376 KB |
Output is correct |
19 |
Correct |
101 ms |
217448 KB |
Output is correct |
20 |
Correct |
121 ms |
217292 KB |
Output is correct |
21 |
Correct |
101 ms |
217124 KB |
Output is correct |
22 |
Correct |
67 ms |
196300 KB |
Output is correct |
23 |
Correct |
66 ms |
196316 KB |
Output is correct |
24 |
Correct |
65 ms |
196348 KB |
Output is correct |
25 |
Correct |
66 ms |
196332 KB |
Output is correct |
26 |
Correct |
64 ms |
196280 KB |
Output is correct |
27 |
Correct |
66 ms |
196244 KB |
Output is correct |
28 |
Correct |
75 ms |
196260 KB |
Output is correct |
29 |
Correct |
67 ms |
196264 KB |
Output is correct |
30 |
Correct |
69 ms |
196300 KB |
Output is correct |
31 |
Correct |
68 ms |
197220 KB |
Output is correct |
32 |
Correct |
69 ms |
197212 KB |
Output is correct |
33 |
Correct |
72 ms |
197128 KB |
Output is correct |
34 |
Correct |
71 ms |
197188 KB |
Output is correct |
35 |
Correct |
68 ms |
197244 KB |
Output is correct |
36 |
Correct |
73 ms |
197176 KB |
Output is correct |
37 |
Correct |
75 ms |
197196 KB |
Output is correct |
38 |
Correct |
74 ms |
197192 KB |
Output is correct |
39 |
Correct |
78 ms |
197172 KB |
Output is correct |
40 |
Correct |
67 ms |
197224 KB |
Output is correct |
41 |
Correct |
65 ms |
197196 KB |
Output is correct |
42 |
Correct |
69 ms |
197328 KB |
Output is correct |
43 |
Correct |
66 ms |
197188 KB |
Output is correct |
44 |
Correct |
75 ms |
197316 KB |
Output is correct |
45 |
Correct |
76 ms |
197344 KB |
Output is correct |
46 |
Correct |
67 ms |
197384 KB |
Output is correct |
47 |
Correct |
67 ms |
197424 KB |
Output is correct |
48 |
Correct |
73 ms |
197708 KB |
Output is correct |
49 |
Correct |
66 ms |
197640 KB |
Output is correct |
50 |
Correct |
68 ms |
197708 KB |
Output is correct |
51 |
Correct |
1238 ms |
196344 KB |
Output is correct |
52 |
Correct |
870 ms |
344792 KB |
Output is correct |
53 |
Correct |
867 ms |
344768 KB |
Output is correct |
54 |
Correct |
906 ms |
342224 KB |
Output is correct |
55 |
Correct |
937 ms |
342048 KB |
Output is correct |
56 |
Correct |
899 ms |
343028 KB |
Output is correct |
57 |
Correct |
868 ms |
344600 KB |
Output is correct |
58 |
Correct |
63 ms |
196568 KB |
Output is correct |
59 |
Correct |
62 ms |
196396 KB |
Output is correct |
60 |
Correct |
62 ms |
196280 KB |
Output is correct |
61 |
Correct |
889 ms |
344984 KB |
Output is correct |
62 |
Correct |
887 ms |
341748 KB |
Output is correct |
63 |
Correct |
850 ms |
341824 KB |
Output is correct |
64 |
Correct |
98 ms |
212424 KB |
Output is correct |
65 |
Correct |
100 ms |
212788 KB |
Output is correct |
66 |
Correct |
878 ms |
344920 KB |
Output is correct |
67 |
Correct |
878 ms |
344652 KB |
Output is correct |
68 |
Correct |
657 ms |
340024 KB |
Output is correct |
69 |
Correct |
642 ms |
340592 KB |
Output is correct |
70 |
Correct |
671 ms |
339928 KB |
Output is correct |
71 |
Correct |
691 ms |
339780 KB |
Output is correct |
72 |
Correct |
699 ms |
339856 KB |
Output is correct |
73 |
Correct |
689 ms |
341392 KB |
Output is correct |
74 |
Correct |
680 ms |
342148 KB |
Output is correct |
75 |
Correct |
683 ms |
341948 KB |
Output is correct |
76 |
Correct |
663 ms |
342876 KB |
Output is correct |
77 |
Correct |
649 ms |
341568 KB |
Output is correct |
78 |
Correct |
678 ms |
344488 KB |
Output is correct |
79 |
Correct |
648 ms |
343944 KB |
Output is correct |
80 |
Correct |
682 ms |
342612 KB |
Output is correct |
81 |
Correct |
642 ms |
358992 KB |
Output is correct |
82 |
Correct |
673 ms |
360564 KB |
Output is correct |
83 |
Correct |
682 ms |
360488 KB |
Output is correct |
84 |
Correct |
704 ms |
371584 KB |
Output is correct |
85 |
Correct |
764 ms |
409264 KB |
Output is correct |
86 |
Correct |
709 ms |
408344 KB |
Output is correct |
87 |
Correct |
779 ms |
408408 KB |
Output is correct |