#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+10], h[2*maxm+10][maxn], dp[mask][2*maxm+10];
vector<int> t[maxn], g[maxn], G[maxm+10];
vector<pii> ver;
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;
ver.push_back({S, -1});
ver.push_back({T, -1});
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[0]);
for (int i = 0; i < m; i++){
if (h[0][edge[i][0]] > h[0][edge[i][1]]) swap(edge[i][0], edge[i][1]);
ver.push_back({edge[i][0], 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]);
}
}
}
int vers = ver.size();
for (int i = 1; i < vers; i++){
dfs(ver[i].F, h[i]);
}
for (int i = 0; i < vers; i++){
dp[0][i] = h[i][T];
}
for (int msk = 1; msk < (1 << m); msk++){
for (int i = 0; i < vers; i++){
int v = ver[i].F;
dp[msk][i] = inf;
if (ver[i].F == T){
dp[msk][i] = 0;
continue;
}
for (int j = 0; j < vers; j++){
if (ver[j].S == -1 && ver[j].F == T && par[msk][comp[T]] == par[msk][comp[v]]){
dp[msk][i] = min(dp[msk][i], h[i][T]);
}
else if (ver[j].S != -1 && ((msk >> ver[j].S) & 1) &&
par[msk][comp[edge[ver[j].S][2]]] == par[msk][comp[v]] && par[msk][comp[ver[j].F]] == par[msk][comp[v]]){
dp[msk][i] = min(dp[msk][i], dp[msk^(1 << ver[j].S)][j] + h[i][edge[ver[j].S][2]] + h[j][edge[ver[j].S][2]]);
}
}
}
}
for (int i = 0; i < vers; i++){
if (ver[i].F == S) return cout << (dp[(1 << m) - 1][i] == inf? -1: dp[(1 << m) - 1][i]) << '\n', 0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
26 ms |
47312 KB |
Output is correct |
3 |
Correct |
136 ms |
57912 KB |
Output is correct |
4 |
Correct |
157 ms |
58028 KB |
Output is correct |
5 |
Correct |
133 ms |
58048 KB |
Output is correct |
6 |
Correct |
132 ms |
58008 KB |
Output is correct |
7 |
Correct |
141 ms |
58144 KB |
Output is correct |
8 |
Correct |
142 ms |
58232 KB |
Output is correct |
9 |
Correct |
137 ms |
58240 KB |
Output is correct |
10 |
Correct |
137 ms |
58088 KB |
Output is correct |
11 |
Correct |
138 ms |
58204 KB |
Output is correct |
12 |
Correct |
122 ms |
59672 KB |
Output is correct |
13 |
Correct |
110 ms |
60192 KB |
Output is correct |
14 |
Correct |
105 ms |
59380 KB |
Output is correct |
15 |
Correct |
108 ms |
59592 KB |
Output is correct |
16 |
Correct |
110 ms |
60116 KB |
Output is correct |
17 |
Correct |
111 ms |
60368 KB |
Output is correct |
18 |
Correct |
123 ms |
60656 KB |
Output is correct |
19 |
Correct |
122 ms |
63828 KB |
Output is correct |
20 |
Correct |
125 ms |
63864 KB |
Output is correct |
21 |
Correct |
125 ms |
63784 KB |
Output is correct |
22 |
Correct |
25 ms |
47308 KB |
Output is correct |
23 |
Correct |
26 ms |
47316 KB |
Output is correct |
24 |
Correct |
25 ms |
47372 KB |
Output is correct |
25 |
Correct |
25 ms |
47500 KB |
Output is correct |
26 |
Correct |
26 ms |
47436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47276 KB |
Output is correct |
2 |
Correct |
25 ms |
47256 KB |
Output is correct |
3 |
Correct |
25 ms |
47700 KB |
Output is correct |
4 |
Correct |
25 ms |
47736 KB |
Output is correct |
5 |
Correct |
1010 ms |
212712 KB |
Output is correct |
6 |
Correct |
971 ms |
212684 KB |
Output is correct |
7 |
Correct |
1030 ms |
213016 KB |
Output is correct |
8 |
Correct |
1037 ms |
212732 KB |
Output is correct |
9 |
Correct |
1030 ms |
212776 KB |
Output is correct |
10 |
Correct |
894 ms |
212768 KB |
Output is correct |
11 |
Correct |
879 ms |
212780 KB |
Output is correct |
12 |
Correct |
858 ms |
212748 KB |
Output is correct |
13 |
Correct |
900 ms |
212664 KB |
Output is correct |
14 |
Correct |
881 ms |
212776 KB |
Output is correct |
15 |
Correct |
1028 ms |
212808 KB |
Output is correct |
16 |
Correct |
1095 ms |
212756 KB |
Output is correct |
17 |
Correct |
1012 ms |
212796 KB |
Output is correct |
18 |
Correct |
905 ms |
212896 KB |
Output is correct |
19 |
Correct |
864 ms |
212944 KB |
Output is correct |
20 |
Correct |
893 ms |
212960 KB |
Output is correct |
21 |
Correct |
885 ms |
212852 KB |
Output is correct |
22 |
Correct |
864 ms |
213192 KB |
Output is correct |
23 |
Correct |
863 ms |
213388 KB |
Output is correct |
24 |
Correct |
870 ms |
213288 KB |
Output is correct |
25 |
Correct |
1961 ms |
375832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
26 ms |
47312 KB |
Output is correct |
3 |
Correct |
136 ms |
57912 KB |
Output is correct |
4 |
Correct |
157 ms |
58028 KB |
Output is correct |
5 |
Correct |
133 ms |
58048 KB |
Output is correct |
6 |
Correct |
132 ms |
58008 KB |
Output is correct |
7 |
Correct |
141 ms |
58144 KB |
Output is correct |
8 |
Correct |
142 ms |
58232 KB |
Output is correct |
9 |
Correct |
137 ms |
58240 KB |
Output is correct |
10 |
Correct |
137 ms |
58088 KB |
Output is correct |
11 |
Correct |
138 ms |
58204 KB |
Output is correct |
12 |
Correct |
122 ms |
59672 KB |
Output is correct |
13 |
Correct |
110 ms |
60192 KB |
Output is correct |
14 |
Correct |
105 ms |
59380 KB |
Output is correct |
15 |
Correct |
108 ms |
59592 KB |
Output is correct |
16 |
Correct |
110 ms |
60116 KB |
Output is correct |
17 |
Correct |
111 ms |
60368 KB |
Output is correct |
18 |
Correct |
123 ms |
60656 KB |
Output is correct |
19 |
Correct |
122 ms |
63828 KB |
Output is correct |
20 |
Correct |
125 ms |
63864 KB |
Output is correct |
21 |
Correct |
125 ms |
63784 KB |
Output is correct |
22 |
Correct |
25 ms |
47308 KB |
Output is correct |
23 |
Correct |
26 ms |
47316 KB |
Output is correct |
24 |
Correct |
25 ms |
47372 KB |
Output is correct |
25 |
Correct |
25 ms |
47500 KB |
Output is correct |
26 |
Correct |
26 ms |
47436 KB |
Output is correct |
27 |
Correct |
24 ms |
47276 KB |
Output is correct |
28 |
Correct |
25 ms |
47256 KB |
Output is correct |
29 |
Correct |
25 ms |
47700 KB |
Output is correct |
30 |
Correct |
25 ms |
47736 KB |
Output is correct |
31 |
Correct |
1010 ms |
212712 KB |
Output is correct |
32 |
Correct |
971 ms |
212684 KB |
Output is correct |
33 |
Correct |
1030 ms |
213016 KB |
Output is correct |
34 |
Correct |
1037 ms |
212732 KB |
Output is correct |
35 |
Correct |
1030 ms |
212776 KB |
Output is correct |
36 |
Correct |
894 ms |
212768 KB |
Output is correct |
37 |
Correct |
879 ms |
212780 KB |
Output is correct |
38 |
Correct |
858 ms |
212748 KB |
Output is correct |
39 |
Correct |
900 ms |
212664 KB |
Output is correct |
40 |
Correct |
881 ms |
212776 KB |
Output is correct |
41 |
Correct |
1028 ms |
212808 KB |
Output is correct |
42 |
Correct |
1095 ms |
212756 KB |
Output is correct |
43 |
Correct |
1012 ms |
212796 KB |
Output is correct |
44 |
Correct |
905 ms |
212896 KB |
Output is correct |
45 |
Correct |
864 ms |
212944 KB |
Output is correct |
46 |
Correct |
893 ms |
212960 KB |
Output is correct |
47 |
Correct |
885 ms |
212852 KB |
Output is correct |
48 |
Correct |
864 ms |
213192 KB |
Output is correct |
49 |
Correct |
863 ms |
213388 KB |
Output is correct |
50 |
Correct |
870 ms |
213288 KB |
Output is correct |
51 |
Correct |
1961 ms |
375832 KB |
Output is correct |
52 |
Execution timed out |
2083 ms |
168360 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |