#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC optimize("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define int long long
#define S second
#define F first
#define mp make_pair
#define smax(xyxy, yxy) (xyxy) = max((xyxy), (yxy))
#define smin(xyxy, yxy) (xyxy) = min((xyxy), (yxy))
#define all(xyxy) (xyxy).begin(), (xyxy).end()
#define len(xyxy) ((int)(xyxy).size())
const int maxn = 2e6 + 5, lg = 22;
const ll inf = 1e15 + 7;
ll n, s, t, w[lg], dp[(1ll << lg)][lg + 1];
pll d[lg + 1][maxn];
vector<pll> adj[maxn];
pll a[lg];
void dfs(int v, int p, int id){
for(auto [u, e]: adj[v]){
if(u == p) continue;
d[id][u].F = d[id][v].F + 1;
d[id][u].S = d[id][v].S;
if(e != -1) d[id][u].S += 1ll << e;
dfs(u, v, id);
}
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s >> t;
int m = 0;
for(int i = 0; i < n - 1; i++){
int u, v, we; cin >> u >> v >> we;
if(we == 0){
adj[u].pb(mp(v, -1));
adj[v].pb(mp(u, -1));
}
else{
a[m] = mp(u, v);
adj[u].pb(mp(v, m));
adj[v].pb(mp(u, m));
w[m] = we;
m++;
}
}
dfs(s, 0, m);
for(int i = 0; i < m; i++){
int v = a[i].F;
if(d[m][a[i].S].F < d[m][a[i].F].F) v = a[i].S;
dfs(v, 0, i);
}
for(int mask = (1ll << m) - 1; mask >= 0; mask--){
for(int i = 0; i <= m; i++){
dp[mask][i] = inf;
int m1 = d[i][t].S, h = d[i][t].F;
if((m1 & mask) == m1){
dp[mask][i] = d[i][t].F;
continue;
}
for(int j = 0; j < m; j++){
if(mask & (1ll << j)) continue;
int res = d[i][w[j]].F;
int m2 = d[i][w[j]].S;
if((m2 & mask) == m2){
int m3 = d[j][w[j]].S;
res += d[j][w[j]].F + dp[mask + (1ll << j)][j];
if((m3 & (mask + (1ll << j))) == m3) smin(dp[mask][i], res);
}
}
}
}
if(dp[0][m] >= inf) cout << -1 << '\n';
else cout << dp[0][m] << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:67:33: warning: unused variable 'h' [-Wunused-variable]
67 | int m1 = d[i][t].S, h = d[i][t].F;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
70 ms |
75556 KB |
Output is correct |
4 |
Correct |
59 ms |
76344 KB |
Output is correct |
5 |
Correct |
83 ms |
76636 KB |
Output is correct |
6 |
Correct |
62 ms |
76372 KB |
Output is correct |
7 |
Correct |
59 ms |
76368 KB |
Output is correct |
8 |
Correct |
62 ms |
76624 KB |
Output is correct |
9 |
Correct |
59 ms |
76372 KB |
Output is correct |
10 |
Correct |
63 ms |
76368 KB |
Output is correct |
11 |
Correct |
61 ms |
76368 KB |
Output is correct |
12 |
Correct |
78 ms |
75860 KB |
Output is correct |
13 |
Correct |
56 ms |
77140 KB |
Output is correct |
14 |
Correct |
56 ms |
77904 KB |
Output is correct |
15 |
Correct |
56 ms |
76880 KB |
Output is correct |
16 |
Correct |
55 ms |
77388 KB |
Output is correct |
17 |
Correct |
58 ms |
78300 KB |
Output is correct |
18 |
Correct |
57 ms |
77648 KB |
Output is correct |
19 |
Correct |
59 ms |
79496 KB |
Output is correct |
20 |
Correct |
58 ms |
78672 KB |
Output is correct |
21 |
Correct |
61 ms |
80232 KB |
Output is correct |
22 |
Correct |
9 ms |
49752 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
8 ms |
49832 KB |
Output is correct |
25 |
Correct |
9 ms |
49756 KB |
Output is correct |
26 |
Correct |
8 ms |
49756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49844 KB |
Output is correct |
3 |
Correct |
9 ms |
50008 KB |
Output is correct |
4 |
Correct |
10 ms |
50012 KB |
Output is correct |
5 |
Correct |
480 ms |
146776 KB |
Output is correct |
6 |
Correct |
512 ms |
146892 KB |
Output is correct |
7 |
Correct |
466 ms |
146768 KB |
Output is correct |
8 |
Correct |
496 ms |
146812 KB |
Output is correct |
9 |
Correct |
433 ms |
146772 KB |
Output is correct |
10 |
Correct |
476 ms |
146768 KB |
Output is correct |
11 |
Correct |
490 ms |
146772 KB |
Output is correct |
12 |
Correct |
435 ms |
146776 KB |
Output is correct |
13 |
Correct |
467 ms |
147076 KB |
Output is correct |
14 |
Correct |
426 ms |
146768 KB |
Output is correct |
15 |
Correct |
441 ms |
146768 KB |
Output is correct |
16 |
Correct |
386 ms |
146776 KB |
Output is correct |
17 |
Correct |
419 ms |
146768 KB |
Output is correct |
18 |
Correct |
406 ms |
147024 KB |
Output is correct |
19 |
Correct |
414 ms |
146768 KB |
Output is correct |
20 |
Correct |
420 ms |
146768 KB |
Output is correct |
21 |
Correct |
457 ms |
146772 KB |
Output is correct |
22 |
Correct |
429 ms |
146984 KB |
Output is correct |
23 |
Correct |
428 ms |
147028 KB |
Output is correct |
24 |
Correct |
469 ms |
147084 KB |
Output is correct |
25 |
Correct |
518 ms |
237140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
70 ms |
75556 KB |
Output is correct |
4 |
Correct |
59 ms |
76344 KB |
Output is correct |
5 |
Correct |
83 ms |
76636 KB |
Output is correct |
6 |
Correct |
62 ms |
76372 KB |
Output is correct |
7 |
Correct |
59 ms |
76368 KB |
Output is correct |
8 |
Correct |
62 ms |
76624 KB |
Output is correct |
9 |
Correct |
59 ms |
76372 KB |
Output is correct |
10 |
Correct |
63 ms |
76368 KB |
Output is correct |
11 |
Correct |
61 ms |
76368 KB |
Output is correct |
12 |
Correct |
78 ms |
75860 KB |
Output is correct |
13 |
Correct |
56 ms |
77140 KB |
Output is correct |
14 |
Correct |
56 ms |
77904 KB |
Output is correct |
15 |
Correct |
56 ms |
76880 KB |
Output is correct |
16 |
Correct |
55 ms |
77388 KB |
Output is correct |
17 |
Correct |
58 ms |
78300 KB |
Output is correct |
18 |
Correct |
57 ms |
77648 KB |
Output is correct |
19 |
Correct |
59 ms |
79496 KB |
Output is correct |
20 |
Correct |
58 ms |
78672 KB |
Output is correct |
21 |
Correct |
61 ms |
80232 KB |
Output is correct |
22 |
Correct |
9 ms |
49752 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
8 ms |
49832 KB |
Output is correct |
25 |
Correct |
9 ms |
49756 KB |
Output is correct |
26 |
Correct |
8 ms |
49756 KB |
Output is correct |
27 |
Correct |
8 ms |
49756 KB |
Output is correct |
28 |
Correct |
8 ms |
49844 KB |
Output is correct |
29 |
Correct |
9 ms |
50008 KB |
Output is correct |
30 |
Correct |
10 ms |
50012 KB |
Output is correct |
31 |
Correct |
480 ms |
146776 KB |
Output is correct |
32 |
Correct |
512 ms |
146892 KB |
Output is correct |
33 |
Correct |
466 ms |
146768 KB |
Output is correct |
34 |
Correct |
496 ms |
146812 KB |
Output is correct |
35 |
Correct |
433 ms |
146772 KB |
Output is correct |
36 |
Correct |
476 ms |
146768 KB |
Output is correct |
37 |
Correct |
490 ms |
146772 KB |
Output is correct |
38 |
Correct |
435 ms |
146776 KB |
Output is correct |
39 |
Correct |
467 ms |
147076 KB |
Output is correct |
40 |
Correct |
426 ms |
146768 KB |
Output is correct |
41 |
Correct |
441 ms |
146768 KB |
Output is correct |
42 |
Correct |
386 ms |
146776 KB |
Output is correct |
43 |
Correct |
419 ms |
146768 KB |
Output is correct |
44 |
Correct |
406 ms |
147024 KB |
Output is correct |
45 |
Correct |
414 ms |
146768 KB |
Output is correct |
46 |
Correct |
420 ms |
146768 KB |
Output is correct |
47 |
Correct |
457 ms |
146772 KB |
Output is correct |
48 |
Correct |
429 ms |
146984 KB |
Output is correct |
49 |
Correct |
428 ms |
147028 KB |
Output is correct |
50 |
Correct |
469 ms |
147084 KB |
Output is correct |
51 |
Correct |
518 ms |
237140 KB |
Output is correct |
52 |
Correct |
1684 ms |
274660 KB |
Output is correct |
53 |
Execution timed out |
2045 ms |
323664 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |