// 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];
pii f[2 * M + 2][2 * M + 2];
bool mrk[N];
vector<int> v, d;
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 dfs2(int u, int rt, int h = 0, int mask = 0, int par = -1) {
if (mrk[u])
f[rt][p[u]] = {h, mask};
for (auto i: g[u])
if (i.F != par)
dfs2(i.F, rt, h + 1, (i.S? (mask | (1 << pd[(e[u] < e[i.F])? i.F : u])) : mask), u);
}
void solve() {
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++)
dfs2(v[i], i);
memset(dp, 127, sizeof dp);
int c = d.size();
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);
if (__builtin_popcount(mask) == 1) {
if (f[p[s]][p[ku]].S == x && f[p[ku]][p[par[u]]].S == x)
dp[mask][i] = f[p[s]][p[ku]].F + f[p[ku]][p[par[u]]].F;
}
else if ((f[p[ku]][p[par[u]]].S | x) == x) {
for (char id2 = 0; id2 < c; ++id2)
if (id1 != id2) {
char j = bt[id2];
int v = d[bt[id2]];
if ((f[p[par[v]]][p[ku]].S | x) == x)
dp[mask][i] = min(dp[mask][i], dp[x][j] + f[p[par[v]]][p[ku]].F + f[p[ku]][p[par[u]]].F);
}
}
if ((f[p[par[u]]][p[t]].S | mask) == mask)
ans = min(ans, dp[mask][i] + f[p[par[u]]][p[t]].F);
}
}
if (f[p[s]][p[t]].S == 0)
ans = f[p[s]][p[t]].F;
cout << ((ans == inf)? -1 : ans) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ip(), solve();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
Main.cpp:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
Main.cpp:75:15: warning: array subscript has type 'char' [-Wchar-subscripts]
75 | int i = bt[id1];
| ^~~
Main.cpp:84:19: warning: array subscript has type 'char' [-Wchar-subscripts]
84 | char j = bt[id2];
| ^~~
Main.cpp:85:20: warning: array subscript has type 'char' [-Wchar-subscripts]
85 | int v = d[bt[id2]];
| ^~~
Main.cpp:87:45: warning: array subscript has type 'char' [-Wchar-subscripts]
87 | dp[mask][i] = min(dp[mask][i], dp[x][j] + f[p[par[v]]][p[ku]].F + f[p[ku]][p[par[u]]].F);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
105808 KB |
Output is correct |
2 |
Correct |
43 ms |
105812 KB |
Output is correct |
3 |
Correct |
124 ms |
114260 KB |
Output is correct |
4 |
Correct |
93 ms |
114256 KB |
Output is correct |
5 |
Correct |
99 ms |
114256 KB |
Output is correct |
6 |
Correct |
89 ms |
114100 KB |
Output is correct |
7 |
Correct |
98 ms |
112252 KB |
Output is correct |
8 |
Correct |
98 ms |
114236 KB |
Output is correct |
9 |
Correct |
98 ms |
114396 KB |
Output is correct |
10 |
Correct |
112 ms |
117860 KB |
Output is correct |
11 |
Correct |
101 ms |
114276 KB |
Output is correct |
12 |
Correct |
124 ms |
114516 KB |
Output is correct |
13 |
Correct |
97 ms |
117068 KB |
Output is correct |
14 |
Correct |
105 ms |
114176 KB |
Output is correct |
15 |
Correct |
93 ms |
116048 KB |
Output is correct |
16 |
Correct |
114 ms |
117388 KB |
Output is correct |
17 |
Correct |
112 ms |
122960 KB |
Output is correct |
18 |
Correct |
107 ms |
118100 KB |
Output is correct |
19 |
Correct |
116 ms |
120660 KB |
Output is correct |
20 |
Correct |
140 ms |
122440 KB |
Output is correct |
21 |
Correct |
99 ms |
122856 KB |
Output is correct |
22 |
Correct |
49 ms |
114044 KB |
Output is correct |
23 |
Correct |
45 ms |
114040 KB |
Output is correct |
24 |
Correct |
48 ms |
106068 KB |
Output is correct |
25 |
Correct |
38 ms |
107856 KB |
Output is correct |
26 |
Correct |
39 ms |
106004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
107784 KB |
Output is correct |
2 |
Correct |
38 ms |
107808 KB |
Output is correct |
3 |
Correct |
39 ms |
107872 KB |
Output is correct |
4 |
Correct |
41 ms |
106076 KB |
Output is correct |
5 |
Correct |
213 ms |
108452 KB |
Output is correct |
6 |
Correct |
183 ms |
108280 KB |
Output is correct |
7 |
Correct |
280 ms |
106380 KB |
Output is correct |
8 |
Correct |
203 ms |
106420 KB |
Output is correct |
9 |
Correct |
207 ms |
106384 KB |
Output is correct |
10 |
Correct |
203 ms |
106564 KB |
Output is correct |
11 |
Correct |
193 ms |
108488 KB |
Output is correct |
12 |
Correct |
147 ms |
108316 KB |
Output is correct |
13 |
Correct |
162 ms |
108268 KB |
Output is correct |
14 |
Correct |
163 ms |
108488 KB |
Output is correct |
15 |
Correct |
202 ms |
108368 KB |
Output is correct |
16 |
Correct |
198 ms |
108368 KB |
Output is correct |
17 |
Correct |
216 ms |
108368 KB |
Output is correct |
18 |
Correct |
137 ms |
108628 KB |
Output is correct |
19 |
Correct |
183 ms |
108620 KB |
Output is correct |
20 |
Correct |
181 ms |
108660 KB |
Output is correct |
21 |
Correct |
161 ms |
108624 KB |
Output is correct |
22 |
Correct |
139 ms |
109056 KB |
Output is correct |
23 |
Correct |
146 ms |
108880 KB |
Output is correct |
24 |
Correct |
132 ms |
109064 KB |
Output is correct |
25 |
Correct |
625 ms |
107964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
105808 KB |
Output is correct |
2 |
Correct |
43 ms |
105812 KB |
Output is correct |
3 |
Correct |
124 ms |
114260 KB |
Output is correct |
4 |
Correct |
93 ms |
114256 KB |
Output is correct |
5 |
Correct |
99 ms |
114256 KB |
Output is correct |
6 |
Correct |
89 ms |
114100 KB |
Output is correct |
7 |
Correct |
98 ms |
112252 KB |
Output is correct |
8 |
Correct |
98 ms |
114236 KB |
Output is correct |
9 |
Correct |
98 ms |
114396 KB |
Output is correct |
10 |
Correct |
112 ms |
117860 KB |
Output is correct |
11 |
Correct |
101 ms |
114276 KB |
Output is correct |
12 |
Correct |
124 ms |
114516 KB |
Output is correct |
13 |
Correct |
97 ms |
117068 KB |
Output is correct |
14 |
Correct |
105 ms |
114176 KB |
Output is correct |
15 |
Correct |
93 ms |
116048 KB |
Output is correct |
16 |
Correct |
114 ms |
117388 KB |
Output is correct |
17 |
Correct |
112 ms |
122960 KB |
Output is correct |
18 |
Correct |
107 ms |
118100 KB |
Output is correct |
19 |
Correct |
116 ms |
120660 KB |
Output is correct |
20 |
Correct |
140 ms |
122440 KB |
Output is correct |
21 |
Correct |
99 ms |
122856 KB |
Output is correct |
22 |
Correct |
49 ms |
114044 KB |
Output is correct |
23 |
Correct |
45 ms |
114040 KB |
Output is correct |
24 |
Correct |
48 ms |
106068 KB |
Output is correct |
25 |
Correct |
38 ms |
107856 KB |
Output is correct |
26 |
Correct |
39 ms |
106004 KB |
Output is correct |
27 |
Correct |
40 ms |
107784 KB |
Output is correct |
28 |
Correct |
38 ms |
107808 KB |
Output is correct |
29 |
Correct |
39 ms |
107872 KB |
Output is correct |
30 |
Correct |
41 ms |
106076 KB |
Output is correct |
31 |
Correct |
213 ms |
108452 KB |
Output is correct |
32 |
Correct |
183 ms |
108280 KB |
Output is correct |
33 |
Correct |
280 ms |
106380 KB |
Output is correct |
34 |
Correct |
203 ms |
106420 KB |
Output is correct |
35 |
Correct |
207 ms |
106384 KB |
Output is correct |
36 |
Correct |
203 ms |
106564 KB |
Output is correct |
37 |
Correct |
193 ms |
108488 KB |
Output is correct |
38 |
Correct |
147 ms |
108316 KB |
Output is correct |
39 |
Correct |
162 ms |
108268 KB |
Output is correct |
40 |
Correct |
163 ms |
108488 KB |
Output is correct |
41 |
Correct |
202 ms |
108368 KB |
Output is correct |
42 |
Correct |
198 ms |
108368 KB |
Output is correct |
43 |
Correct |
216 ms |
108368 KB |
Output is correct |
44 |
Correct |
137 ms |
108628 KB |
Output is correct |
45 |
Correct |
183 ms |
108620 KB |
Output is correct |
46 |
Correct |
181 ms |
108660 KB |
Output is correct |
47 |
Correct |
161 ms |
108624 KB |
Output is correct |
48 |
Correct |
139 ms |
109056 KB |
Output is correct |
49 |
Correct |
146 ms |
108880 KB |
Output is correct |
50 |
Correct |
132 ms |
109064 KB |
Output is correct |
51 |
Correct |
625 ms |
107964 KB |
Output is correct |
52 |
Execution timed out |
2043 ms |
91232 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |