#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __uint128_t L;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
const int maxn = 1e6 + 10;
const int maxm = 20;
const int mask = (1 << 20);
const int inf = 1e9;
int n, m, S, T, edge[maxm][3], comp[maxn], cnt, sz, par[mask][maxm+1], h[maxm+1][maxn], dp[mask][maxm+1];
vector<int> t[maxn], g[maxn], G[maxm+1];
bool vis[maxn];
void dfs(int v, int *h, int p = -1){
for (auto u: t[v]){
if (u != p){
h[u] = h[v] + 1;
dfs(u, h, v);
}
}
}
void getcomp(int v, int p = -1){
vis[v] = true;
comp[v] = cnt;
for (auto u: g[v]) if (u != p) getcomp(u, v);
}
void getpar(int v, int *par, int p = -1){
vis[v] = true;
par[v] = sz;
for (auto u : G[v]) if (u != p) getpar(u, par, v);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> S >> T;
for (int i = 1; i < n; i++){
int u, v, w; cin >> u >> v >> w;
t[u].push_back(v);
t[v].push_back(u);
if (w == 0){
g[u].push_back(v);
g[v].push_back(u);
}
else{
edge[m][0] = u;
edge[m][1] = v;
edge[m][2] = w;
m++;
}
}
dfs(S, h[m]);
for (int i = 0; i < m; i++){
if (h[m][edge[i][0]] > h[m][edge[i][1]]) swap(edge[i][0], edge[i][1]);
dfs(edge[i][0], h[i]);
}
assert(m <= 20);
for (int i = 1; i <= n; i++){
if (!vis[i]){
getcomp(i);
cnt++;
}
}
for (int i = 0; i < (1 << m); i++){
for (int j = 0; j < m+1; j++){
G[j].clear();
vis[j] = false;
}
for (int j = 0; j < m; j++){
if (!((i >> j) & 1)){
G[comp[edge[j][0]]].push_back(comp[edge[j][1]]);
G[comp[edge[j][1]]].push_back(comp[edge[j][0]]);
}
}
sz = 0;
for (int j = 0; j < m+1; j++){
if (!vis[j]){
sz++;
getpar(j, par[i]);
}
}
}
for (int i = 0; i <= m; i++){
dp[0][i] = h[i][T];
}
for (int msk = 1; msk < (1 << m); msk++){
for (int i = 0; i <= m; i++){
int v = (i == m? S: edge[i][0]);
dp[msk][i] = inf;
if (par[msk][comp[T]] == par[msk][comp[v]]){
dp[msk][i] = h[i][T];
continue;
}
for (int tmp = msk; tmp; tmp ^= tmp & -tmp){
int j = __builtin_ctz(tmp);
if (par[msk][comp[edge[j][2]]] == par[msk][comp[v]] && par[msk][comp[edge[j][0]]] == par[msk][comp[v]]){
dp[msk][i] = min(dp[msk][i], dp[msk^(1 << j)][j] + h[i][edge[j][2]] + h[j][edge[j][2]]);
}
}
}
}
cout << (dp[(1 << m) - 1][m] == inf? -1: dp[(1 << m) - 1][m]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47248 KB |
Output is correct |
3 |
Correct |
135 ms |
57720 KB |
Output is correct |
4 |
Correct |
156 ms |
57488 KB |
Output is correct |
5 |
Correct |
135 ms |
57576 KB |
Output is correct |
6 |
Correct |
127 ms |
57612 KB |
Output is correct |
7 |
Correct |
149 ms |
57652 KB |
Output is correct |
8 |
Correct |
134 ms |
57648 KB |
Output is correct |
9 |
Correct |
135 ms |
57904 KB |
Output is correct |
10 |
Correct |
125 ms |
57624 KB |
Output is correct |
11 |
Correct |
128 ms |
57676 KB |
Output is correct |
12 |
Correct |
129 ms |
58984 KB |
Output is correct |
13 |
Correct |
111 ms |
59404 KB |
Output is correct |
14 |
Correct |
104 ms |
59004 KB |
Output is correct |
15 |
Correct |
108 ms |
59184 KB |
Output is correct |
16 |
Correct |
114 ms |
59720 KB |
Output is correct |
17 |
Correct |
103 ms |
59780 KB |
Output is correct |
18 |
Correct |
103 ms |
60108 KB |
Output is correct |
19 |
Correct |
116 ms |
63320 KB |
Output is correct |
20 |
Correct |
126 ms |
63232 KB |
Output is correct |
21 |
Correct |
124 ms |
63032 KB |
Output is correct |
22 |
Correct |
25 ms |
47360 KB |
Output is correct |
23 |
Correct |
26 ms |
47308 KB |
Output is correct |
24 |
Correct |
27 ms |
47420 KB |
Output is correct |
25 |
Correct |
25 ms |
47396 KB |
Output is correct |
26 |
Correct |
25 ms |
47408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
47248 KB |
Output is correct |
2 |
Correct |
25 ms |
47244 KB |
Output is correct |
3 |
Correct |
29 ms |
47572 KB |
Output is correct |
4 |
Correct |
25 ms |
47496 KB |
Output is correct |
5 |
Correct |
731 ms |
134664 KB |
Output is correct |
6 |
Correct |
684 ms |
134664 KB |
Output is correct |
7 |
Correct |
692 ms |
134664 KB |
Output is correct |
8 |
Correct |
683 ms |
134656 KB |
Output is correct |
9 |
Correct |
720 ms |
134712 KB |
Output is correct |
10 |
Correct |
538 ms |
134584 KB |
Output is correct |
11 |
Correct |
547 ms |
134760 KB |
Output is correct |
12 |
Correct |
552 ms |
134668 KB |
Output is correct |
13 |
Correct |
536 ms |
134720 KB |
Output is correct |
14 |
Correct |
523 ms |
134620 KB |
Output is correct |
15 |
Correct |
732 ms |
134668 KB |
Output is correct |
16 |
Correct |
648 ms |
134636 KB |
Output is correct |
17 |
Correct |
734 ms |
134624 KB |
Output is correct |
18 |
Correct |
565 ms |
134928 KB |
Output is correct |
19 |
Correct |
542 ms |
134884 KB |
Output is correct |
20 |
Correct |
559 ms |
134752 KB |
Output is correct |
21 |
Correct |
563 ms |
134960 KB |
Output is correct |
22 |
Correct |
532 ms |
134988 KB |
Output is correct |
23 |
Correct |
540 ms |
135116 KB |
Output is correct |
24 |
Correct |
552 ms |
135192 KB |
Output is correct |
25 |
Correct |
1018 ms |
219896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47248 KB |
Output is correct |
3 |
Correct |
135 ms |
57720 KB |
Output is correct |
4 |
Correct |
156 ms |
57488 KB |
Output is correct |
5 |
Correct |
135 ms |
57576 KB |
Output is correct |
6 |
Correct |
127 ms |
57612 KB |
Output is correct |
7 |
Correct |
149 ms |
57652 KB |
Output is correct |
8 |
Correct |
134 ms |
57648 KB |
Output is correct |
9 |
Correct |
135 ms |
57904 KB |
Output is correct |
10 |
Correct |
125 ms |
57624 KB |
Output is correct |
11 |
Correct |
128 ms |
57676 KB |
Output is correct |
12 |
Correct |
129 ms |
58984 KB |
Output is correct |
13 |
Correct |
111 ms |
59404 KB |
Output is correct |
14 |
Correct |
104 ms |
59004 KB |
Output is correct |
15 |
Correct |
108 ms |
59184 KB |
Output is correct |
16 |
Correct |
114 ms |
59720 KB |
Output is correct |
17 |
Correct |
103 ms |
59780 KB |
Output is correct |
18 |
Correct |
103 ms |
60108 KB |
Output is correct |
19 |
Correct |
116 ms |
63320 KB |
Output is correct |
20 |
Correct |
126 ms |
63232 KB |
Output is correct |
21 |
Correct |
124 ms |
63032 KB |
Output is correct |
22 |
Correct |
25 ms |
47360 KB |
Output is correct |
23 |
Correct |
26 ms |
47308 KB |
Output is correct |
24 |
Correct |
27 ms |
47420 KB |
Output is correct |
25 |
Correct |
25 ms |
47396 KB |
Output is correct |
26 |
Correct |
25 ms |
47408 KB |
Output is correct |
27 |
Correct |
25 ms |
47248 KB |
Output is correct |
28 |
Correct |
25 ms |
47244 KB |
Output is correct |
29 |
Correct |
29 ms |
47572 KB |
Output is correct |
30 |
Correct |
25 ms |
47496 KB |
Output is correct |
31 |
Correct |
731 ms |
134664 KB |
Output is correct |
32 |
Correct |
684 ms |
134664 KB |
Output is correct |
33 |
Correct |
692 ms |
134664 KB |
Output is correct |
34 |
Correct |
683 ms |
134656 KB |
Output is correct |
35 |
Correct |
720 ms |
134712 KB |
Output is correct |
36 |
Correct |
538 ms |
134584 KB |
Output is correct |
37 |
Correct |
547 ms |
134760 KB |
Output is correct |
38 |
Correct |
552 ms |
134668 KB |
Output is correct |
39 |
Correct |
536 ms |
134720 KB |
Output is correct |
40 |
Correct |
523 ms |
134620 KB |
Output is correct |
41 |
Correct |
732 ms |
134668 KB |
Output is correct |
42 |
Correct |
648 ms |
134636 KB |
Output is correct |
43 |
Correct |
734 ms |
134624 KB |
Output is correct |
44 |
Correct |
565 ms |
134928 KB |
Output is correct |
45 |
Correct |
542 ms |
134884 KB |
Output is correct |
46 |
Correct |
559 ms |
134752 KB |
Output is correct |
47 |
Correct |
563 ms |
134960 KB |
Output is correct |
48 |
Correct |
532 ms |
134988 KB |
Output is correct |
49 |
Correct |
540 ms |
135116 KB |
Output is correct |
50 |
Correct |
552 ms |
135192 KB |
Output is correct |
51 |
Correct |
1018 ms |
219896 KB |
Output is correct |
52 |
Execution timed out |
2070 ms |
151828 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |