# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
624113 |
2022-08-07T09:32:49 Z |
Arinoor |
LOSTIKS (INOI20_lostiks) |
C++17 |
|
1764 ms |
425740 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define bug(x) cout << #x << " : " << x << '\n'
#define ios ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 10;
const int maxm = 30;
const int maxlg = 20;
const int mod = 1e9 + 7;
const ll inf = 1e17 + 10;
int e[maxn], h[maxn], par[maxn][maxlg], lck[maxn][maxlg], V[maxm], ind[maxn];
int dt[maxm], dl[maxm];
int d[maxm][maxm], l[maxm][maxm];
ll dp[1 << maxlg][maxm];
vector<pii> E;
vector<pii> edge;
vector<int> G[maxn], S;
void dfs(int v, int p = 0, int pe = -1){
par[v][0] = p;
if(~pe)
lck[v][0] = 1 << pe;
for(int i = 1; i < maxlg and par[v][i - 1]; i ++){
par[v][i] = par[par[v][i - 1]][i - 1];
lck[v][i] = lck[v][i - 1] | lck[par[v][i - 1]][i - 1];
}
for(int eg : G[v]){
int u = edge[eg].fi + edge[eg].se - v;
if(u != p){
h[u] = h[v] + 1;
dfs(u, v, e[eg]);
}
}
}
int get_par(int v, int h){
for(int i = 0; i < maxlg; i ++)
if(h >> i & 1)
v = par[v][i];
return v;
}
int get_lock(int v, int h){
int res = 0;
for(int i = 0; i < maxlg; i ++){
if(h >> i & 1){
res |= lck[v][i];
v = par[v][i];
}
}
return res;
}
int lca(int v, int u){
if(h[v] < h[u])
swap(u, v);
v = get_par(v, h[v] - h[u]);
if(v == u)
return v;
for(int i = maxlg - 1; ~i; i --){
if(par[v][i] ^ par[u][i]){
v = par[v][i];
u = par[u][i];
}
}
return par[v][0];
}
int dist(int u, int v){
int w = lca(u, v);
return h[v] + h[u] - 2 * h[w];
}
int lock_path(int u, int v){
int w = lca(u, v);
return get_lock(u, h[u] - h[w]) | get_lock(v, h[v] - h[w]);
}
int main(){
ios;
int n, s, t;
cin >> n >> s >> t;
for(int i = 0; i < n - 1; i ++){
int u, v, w;
cin >> u >> v >> w;
edge.pb(mp(u, v));
G[u].pb(i);
G[v].pb(i);
if(w){
e[i] = E.size();
E.pb(mp(i, w));
}
else{
e[i] = -1;
}
}
dfs(1);
int m = E.size();
for(int i = 0; i < m; i ++){
int u = edge[E[i].fi].fi;
int v = edge[E[i].fi].se;
if(dist(E[i].se, u) < dist(E[i].se, v))
V[i] = u;
else
V[i] = v;
S.pb(V[i]);
}
S.pb(s);
sort(all(S));
S.resize(unique(all(S)) - S.begin());
for(int v : S)
ind[v] = lower_bound(all(S), v) - S.begin();
n = S.size();
for(int i = 0; i < n; i ++){
int v = S[i];
dt[i] = dist(v, t);
dl[i] = lock_path(v, t);
for(int j = 0; j < m; j ++){
int u = V[j];
int w = E[j].se;
d[i][j] = dist(v, w) + dist(w, u);
l[i][j] = lock_path(v, w) | lock_path(w, u);
}
}
for(int msk = 0; msk < 1 << m; msk ++){
for(int i = 0; i < n; i ++){
if(!(dl[i] & msk)){
dp[msk][i] = dt[i];
continue;
}
dp[msk][i] = inf;
for(int msk2 = msk; ; msk2 -= msk2 & -msk2){
if(!msk2) break;
int bit = __builtin_ctz(msk2);
if(!(l[i][bit] & msk)){
dp[msk][i] = min(dp[msk][i], d[i][bit] + dp[msk ^ (1 << bit)][ind[V[bit]]]);
}
}
}
}
if(dp[(1 << m) - 1][ind[s]] >= inf)
cout << "-1\n";
else
cout << dp[(1 << m) - 1][ind[s]] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23852 KB |
Output is correct |
2 |
Correct |
15 ms |
23864 KB |
Output is correct |
3 |
Correct |
86 ms |
44464 KB |
Output is correct |
4 |
Correct |
83 ms |
44480 KB |
Output is correct |
5 |
Correct |
81 ms |
44480 KB |
Output is correct |
6 |
Correct |
83 ms |
44420 KB |
Output is correct |
7 |
Correct |
83 ms |
44536 KB |
Output is correct |
8 |
Correct |
84 ms |
44508 KB |
Output is correct |
9 |
Correct |
84 ms |
44616 KB |
Output is correct |
10 |
Correct |
82 ms |
44528 KB |
Output is correct |
11 |
Correct |
88 ms |
44404 KB |
Output is correct |
12 |
Correct |
87 ms |
45608 KB |
Output is correct |
13 |
Correct |
90 ms |
46712 KB |
Output is correct |
14 |
Correct |
89 ms |
45544 KB |
Output is correct |
15 |
Correct |
100 ms |
45928 KB |
Output is correct |
16 |
Correct |
107 ms |
48280 KB |
Output is correct |
17 |
Correct |
96 ms |
47916 KB |
Output is correct |
18 |
Correct |
89 ms |
47636 KB |
Output is correct |
19 |
Correct |
101 ms |
51504 KB |
Output is correct |
20 |
Correct |
93 ms |
49788 KB |
Output is correct |
21 |
Correct |
110 ms |
51852 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23892 KB |
Output is correct |
25 |
Correct |
13 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
16 ms |
24128 KB |
Output is correct |
4 |
Correct |
13 ms |
24140 KB |
Output is correct |
5 |
Correct |
316 ms |
148396 KB |
Output is correct |
6 |
Correct |
307 ms |
148344 KB |
Output is correct |
7 |
Correct |
318 ms |
148512 KB |
Output is correct |
8 |
Correct |
287 ms |
148328 KB |
Output is correct |
9 |
Correct |
305 ms |
148300 KB |
Output is correct |
10 |
Correct |
263 ms |
148352 KB |
Output is correct |
11 |
Correct |
251 ms |
148416 KB |
Output is correct |
12 |
Correct |
270 ms |
148324 KB |
Output is correct |
13 |
Correct |
254 ms |
148420 KB |
Output is correct |
14 |
Correct |
241 ms |
148324 KB |
Output is correct |
15 |
Correct |
318 ms |
148364 KB |
Output is correct |
16 |
Correct |
285 ms |
148560 KB |
Output is correct |
17 |
Correct |
319 ms |
148432 KB |
Output is correct |
18 |
Correct |
222 ms |
148428 KB |
Output is correct |
19 |
Correct |
239 ms |
148392 KB |
Output is correct |
20 |
Correct |
267 ms |
148496 KB |
Output is correct |
21 |
Correct |
262 ms |
148600 KB |
Output is correct |
22 |
Correct |
227 ms |
148860 KB |
Output is correct |
23 |
Correct |
239 ms |
148588 KB |
Output is correct |
24 |
Correct |
242 ms |
148672 KB |
Output is correct |
25 |
Correct |
159 ms |
270096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23852 KB |
Output is correct |
2 |
Correct |
15 ms |
23864 KB |
Output is correct |
3 |
Correct |
86 ms |
44464 KB |
Output is correct |
4 |
Correct |
83 ms |
44480 KB |
Output is correct |
5 |
Correct |
81 ms |
44480 KB |
Output is correct |
6 |
Correct |
83 ms |
44420 KB |
Output is correct |
7 |
Correct |
83 ms |
44536 KB |
Output is correct |
8 |
Correct |
84 ms |
44508 KB |
Output is correct |
9 |
Correct |
84 ms |
44616 KB |
Output is correct |
10 |
Correct |
82 ms |
44528 KB |
Output is correct |
11 |
Correct |
88 ms |
44404 KB |
Output is correct |
12 |
Correct |
87 ms |
45608 KB |
Output is correct |
13 |
Correct |
90 ms |
46712 KB |
Output is correct |
14 |
Correct |
89 ms |
45544 KB |
Output is correct |
15 |
Correct |
100 ms |
45928 KB |
Output is correct |
16 |
Correct |
107 ms |
48280 KB |
Output is correct |
17 |
Correct |
96 ms |
47916 KB |
Output is correct |
18 |
Correct |
89 ms |
47636 KB |
Output is correct |
19 |
Correct |
101 ms |
51504 KB |
Output is correct |
20 |
Correct |
93 ms |
49788 KB |
Output is correct |
21 |
Correct |
110 ms |
51852 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23892 KB |
Output is correct |
25 |
Correct |
13 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23920 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23788 KB |
Output is correct |
29 |
Correct |
16 ms |
24128 KB |
Output is correct |
30 |
Correct |
13 ms |
24140 KB |
Output is correct |
31 |
Correct |
316 ms |
148396 KB |
Output is correct |
32 |
Correct |
307 ms |
148344 KB |
Output is correct |
33 |
Correct |
318 ms |
148512 KB |
Output is correct |
34 |
Correct |
287 ms |
148328 KB |
Output is correct |
35 |
Correct |
305 ms |
148300 KB |
Output is correct |
36 |
Correct |
263 ms |
148352 KB |
Output is correct |
37 |
Correct |
251 ms |
148416 KB |
Output is correct |
38 |
Correct |
270 ms |
148324 KB |
Output is correct |
39 |
Correct |
254 ms |
148420 KB |
Output is correct |
40 |
Correct |
241 ms |
148324 KB |
Output is correct |
41 |
Correct |
318 ms |
148364 KB |
Output is correct |
42 |
Correct |
285 ms |
148560 KB |
Output is correct |
43 |
Correct |
319 ms |
148432 KB |
Output is correct |
44 |
Correct |
222 ms |
148428 KB |
Output is correct |
45 |
Correct |
239 ms |
148392 KB |
Output is correct |
46 |
Correct |
267 ms |
148496 KB |
Output is correct |
47 |
Correct |
262 ms |
148600 KB |
Output is correct |
48 |
Correct |
227 ms |
148860 KB |
Output is correct |
49 |
Correct |
239 ms |
148588 KB |
Output is correct |
50 |
Correct |
242 ms |
148672 KB |
Output is correct |
51 |
Correct |
159 ms |
270096 KB |
Output is correct |
52 |
Correct |
1265 ms |
247548 KB |
Output is correct |
53 |
Correct |
1279 ms |
247668 KB |
Output is correct |
54 |
Correct |
1264 ms |
247120 KB |
Output is correct |
55 |
Correct |
1314 ms |
247752 KB |
Output is correct |
56 |
Correct |
1286 ms |
247136 KB |
Output is correct |
57 |
Correct |
1256 ms |
247376 KB |
Output is correct |
58 |
Correct |
12 ms |
23892 KB |
Output is correct |
59 |
Correct |
12 ms |
23872 KB |
Output is correct |
60 |
Correct |
12 ms |
23892 KB |
Output is correct |
61 |
Correct |
1256 ms |
246888 KB |
Output is correct |
62 |
Correct |
1337 ms |
277620 KB |
Output is correct |
63 |
Correct |
1278 ms |
262084 KB |
Output is correct |
64 |
Correct |
106 ms |
48064 KB |
Output is correct |
65 |
Correct |
109 ms |
47912 KB |
Output is correct |
66 |
Correct |
1295 ms |
248584 KB |
Output is correct |
67 |
Correct |
1298 ms |
246436 KB |
Output is correct |
68 |
Correct |
1369 ms |
368128 KB |
Output is correct |
69 |
Correct |
1406 ms |
368256 KB |
Output is correct |
70 |
Correct |
1361 ms |
367612 KB |
Output is correct |
71 |
Correct |
1396 ms |
367700 KB |
Output is correct |
72 |
Correct |
1374 ms |
367804 KB |
Output is correct |
73 |
Correct |
1371 ms |
368672 KB |
Output is correct |
74 |
Correct |
1404 ms |
369388 KB |
Output is correct |
75 |
Correct |
1314 ms |
369124 KB |
Output is correct |
76 |
Correct |
1317 ms |
369616 KB |
Output is correct |
77 |
Correct |
1295 ms |
368268 KB |
Output is correct |
78 |
Correct |
1555 ms |
376088 KB |
Output is correct |
79 |
Correct |
1601 ms |
378216 KB |
Output is correct |
80 |
Correct |
1553 ms |
371516 KB |
Output is correct |
81 |
Correct |
1538 ms |
380812 KB |
Output is correct |
82 |
Correct |
1621 ms |
381980 KB |
Output is correct |
83 |
Correct |
1609 ms |
387400 KB |
Output is correct |
84 |
Correct |
1731 ms |
397020 KB |
Output is correct |
85 |
Correct |
1764 ms |
425740 KB |
Output is correct |
86 |
Correct |
1689 ms |
399592 KB |
Output is correct |
87 |
Correct |
1715 ms |
399864 KB |
Output is correct |