// In the Name of Allah
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,avx,avx2,sse4.2,popcnt,tune=native")
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
const int N = 1e6 + 12, M = 20, inf = 2e9 + 7;
vector<pii> g[N];
int n, s, t, par[N], dp[(1 << M) + 4][M], p[N], kd[N], bt[M], sz, pd[N], e[N], l[N];
vector<int> q[(1 << M)];
pii f[2 * M + 2][2 * M + 2];
bool mrk[N];
vector<int> v, d;
struct node {
int u, h, mask, par;
};
void ip() {
cin >> n >> s >> t;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
}
void dfs(int u) {
for (auto i: g[u])
if (i.F != par[u]) {
par[i.F] = u;
e[i.F] = e[u] + 1;
if (i.S) {
pd[i.F] = d.size(), d.pb(i.F), kd[i.F] = i.S;
v.pb(u), v.pb(i.S);
}
dfs(i.F);
}
}
void bfs(int u, int rt) {
node st;
st.par = st.u = u, st.h = st.mask = 0;
vector<node> v;
v.pb(st);
while (!v.empty()) {
node tmp = v.back();
v.pp();
if (mrk[tmp.u])
f[rt][p[tmp.u]] = {tmp.h, tmp.mask};
for (auto i: g[tmp.u])
if (i.F != tmp.par) {
node tmp2;
tmp2.u = i.F, tmp2.h = tmp.h + 1, tmp2.par = tmp.u;
tmp2.mask = (i.S? (tmp.mask | (1 << pd[(e[tmp.u] < e[i.F])? i.F : tmp.u])) : tmp.mask);
v.pb(tmp2);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ip();
par[s] = s;
dfs(s);
v.pb(s), v.pb(t);
sort(all(v));
v.resize(unique(all(v)) - v.begin());
for (int i = 0; i < v.size(); i++)
p[v[i]] = i, mrk[v[i]] = true;
for (int i = 0; i < v.size(); i++)
bfs(v[i], i);
memset(dp, 127, sizeof dp);
int c = d.size();
for (int i = 0; i < c; i++)
l[i] = p[par[d[i]]];
int ans = inf;
for (int mask = 1; mask < (1 << c); ++mask) {
sz = 0;
for (char i = 0; i < c; i++)
if ((mask >> i) & 1)
bt[sz++] = i;
for (char id1 = 0; id1 < sz; ++id1) {
int i = bt[id1];
int u = d[i], ku = kd[d[i]], x = mask ^ (1 << i);
int v1 = f[p[ku]][p[par[u]]].F;
auto &dd = dp[mask][i];
auto &bb = f[p[ku]];
if (__builtin_popcount(mask) == 1) {
if (f[p[s]][p[ku]].S == x && f[p[ku]][l[i]].S == x)
dp[mask][i] = f[p[s]][p[ku]].F + v1;
}
else if ((f[p[ku]][l[i]].S | x) == x) {
for (auto j: q[x])
if ((bb[l[j]].S | x) == x)
dd = min(dd, dp[x][j] + bb[l[j]].F);
if (dd < inf)
dd += v1;
}
if ((f[p[par[u]]][p[t]].S | mask) == mask)
ans = min(ans, dp[mask][i] + f[p[par[u]]][p[t]].F);
if (dp[mask][i] < inf)
q[mask].pb(i);
}
}
if (f[p[s]][p[t]].S == 0)
ans = f[p[s]][p[t]].F;
cout << ((ans == inf)? -1 : ans) << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
Main.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
Main.cpp:96:15: warning: array subscript has type 'char' [-Wchar-subscripts]
96 | int i = bt[id1];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
142424 KB |
Output is correct |
2 |
Correct |
16 ms |
142428 KB |
Output is correct |
3 |
Correct |
85 ms |
148900 KB |
Output is correct |
4 |
Correct |
79 ms |
148940 KB |
Output is correct |
5 |
Correct |
83 ms |
148916 KB |
Output is correct |
6 |
Correct |
76 ms |
148888 KB |
Output is correct |
7 |
Correct |
82 ms |
149032 KB |
Output is correct |
8 |
Correct |
78 ms |
149072 KB |
Output is correct |
9 |
Correct |
77 ms |
149336 KB |
Output is correct |
10 |
Correct |
74 ms |
149076 KB |
Output is correct |
11 |
Correct |
76 ms |
149332 KB |
Output is correct |
12 |
Correct |
80 ms |
150864 KB |
Output is correct |
13 |
Correct |
79 ms |
150352 KB |
Output is correct |
14 |
Correct |
82 ms |
149852 KB |
Output is correct |
15 |
Correct |
90 ms |
151080 KB |
Output is correct |
16 |
Correct |
81 ms |
151892 KB |
Output is correct |
17 |
Correct |
81 ms |
151888 KB |
Output is correct |
18 |
Correct |
83 ms |
152460 KB |
Output is correct |
19 |
Correct |
94 ms |
157344 KB |
Output is correct |
20 |
Correct |
96 ms |
157268 KB |
Output is correct |
21 |
Correct |
92 ms |
156840 KB |
Output is correct |
22 |
Correct |
18 ms |
142560 KB |
Output is correct |
23 |
Correct |
17 ms |
142428 KB |
Output is correct |
24 |
Correct |
19 ms |
142504 KB |
Output is correct |
25 |
Correct |
17 ms |
142620 KB |
Output is correct |
26 |
Correct |
18 ms |
142428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
142428 KB |
Output is correct |
2 |
Correct |
19 ms |
142548 KB |
Output is correct |
3 |
Correct |
17 ms |
142428 KB |
Output is correct |
4 |
Correct |
19 ms |
142452 KB |
Output is correct |
5 |
Correct |
81 ms |
142940 KB |
Output is correct |
6 |
Correct |
86 ms |
142952 KB |
Output is correct |
7 |
Correct |
84 ms |
142940 KB |
Output is correct |
8 |
Correct |
80 ms |
142940 KB |
Output is correct |
9 |
Correct |
84 ms |
142928 KB |
Output is correct |
10 |
Correct |
81 ms |
142940 KB |
Output is correct |
11 |
Correct |
80 ms |
142940 KB |
Output is correct |
12 |
Correct |
77 ms |
142940 KB |
Output is correct |
13 |
Correct |
79 ms |
142940 KB |
Output is correct |
14 |
Correct |
78 ms |
142836 KB |
Output is correct |
15 |
Correct |
86 ms |
142948 KB |
Output is correct |
16 |
Correct |
79 ms |
142936 KB |
Output is correct |
17 |
Correct |
82 ms |
142932 KB |
Output is correct |
18 |
Correct |
73 ms |
143124 KB |
Output is correct |
19 |
Correct |
81 ms |
142932 KB |
Output is correct |
20 |
Correct |
81 ms |
142932 KB |
Output is correct |
21 |
Correct |
79 ms |
143192 KB |
Output is correct |
22 |
Correct |
78 ms |
143444 KB |
Output is correct |
23 |
Correct |
77 ms |
143520 KB |
Output is correct |
24 |
Correct |
79 ms |
143536 KB |
Output is correct |
25 |
Correct |
402 ms |
216200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
142424 KB |
Output is correct |
2 |
Correct |
16 ms |
142428 KB |
Output is correct |
3 |
Correct |
85 ms |
148900 KB |
Output is correct |
4 |
Correct |
79 ms |
148940 KB |
Output is correct |
5 |
Correct |
83 ms |
148916 KB |
Output is correct |
6 |
Correct |
76 ms |
148888 KB |
Output is correct |
7 |
Correct |
82 ms |
149032 KB |
Output is correct |
8 |
Correct |
78 ms |
149072 KB |
Output is correct |
9 |
Correct |
77 ms |
149336 KB |
Output is correct |
10 |
Correct |
74 ms |
149076 KB |
Output is correct |
11 |
Correct |
76 ms |
149332 KB |
Output is correct |
12 |
Correct |
80 ms |
150864 KB |
Output is correct |
13 |
Correct |
79 ms |
150352 KB |
Output is correct |
14 |
Correct |
82 ms |
149852 KB |
Output is correct |
15 |
Correct |
90 ms |
151080 KB |
Output is correct |
16 |
Correct |
81 ms |
151892 KB |
Output is correct |
17 |
Correct |
81 ms |
151888 KB |
Output is correct |
18 |
Correct |
83 ms |
152460 KB |
Output is correct |
19 |
Correct |
94 ms |
157344 KB |
Output is correct |
20 |
Correct |
96 ms |
157268 KB |
Output is correct |
21 |
Correct |
92 ms |
156840 KB |
Output is correct |
22 |
Correct |
18 ms |
142560 KB |
Output is correct |
23 |
Correct |
17 ms |
142428 KB |
Output is correct |
24 |
Correct |
19 ms |
142504 KB |
Output is correct |
25 |
Correct |
17 ms |
142620 KB |
Output is correct |
26 |
Correct |
18 ms |
142428 KB |
Output is correct |
27 |
Correct |
18 ms |
142428 KB |
Output is correct |
28 |
Correct |
19 ms |
142548 KB |
Output is correct |
29 |
Correct |
17 ms |
142428 KB |
Output is correct |
30 |
Correct |
19 ms |
142452 KB |
Output is correct |
31 |
Correct |
81 ms |
142940 KB |
Output is correct |
32 |
Correct |
86 ms |
142952 KB |
Output is correct |
33 |
Correct |
84 ms |
142940 KB |
Output is correct |
34 |
Correct |
80 ms |
142940 KB |
Output is correct |
35 |
Correct |
84 ms |
142928 KB |
Output is correct |
36 |
Correct |
81 ms |
142940 KB |
Output is correct |
37 |
Correct |
80 ms |
142940 KB |
Output is correct |
38 |
Correct |
77 ms |
142940 KB |
Output is correct |
39 |
Correct |
79 ms |
142940 KB |
Output is correct |
40 |
Correct |
78 ms |
142836 KB |
Output is correct |
41 |
Correct |
86 ms |
142948 KB |
Output is correct |
42 |
Correct |
79 ms |
142936 KB |
Output is correct |
43 |
Correct |
82 ms |
142932 KB |
Output is correct |
44 |
Correct |
73 ms |
143124 KB |
Output is correct |
45 |
Correct |
81 ms |
142932 KB |
Output is correct |
46 |
Correct |
81 ms |
142932 KB |
Output is correct |
47 |
Correct |
79 ms |
143192 KB |
Output is correct |
48 |
Correct |
78 ms |
143444 KB |
Output is correct |
49 |
Correct |
77 ms |
143520 KB |
Output is correct |
50 |
Correct |
79 ms |
143536 KB |
Output is correct |
51 |
Correct |
402 ms |
216200 KB |
Output is correct |
52 |
Execution timed out |
2036 ms |
120752 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |