# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
953879 |
2024-03-26T18:56:21 Z |
angella |
LOSTIKS (INOI20_lostiks) |
C++17 |
|
1544 ms |
250300 KB |
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 1e6+23, lg = 20;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, s, t, m, tim, nd[22][22], dp[(1<<20)][21], par[lg][N];
int arr[22][5], mark[N], h[N], dst[22][22], tin[N], tout[N];
vector<int> adj[N];
void init(int v, int p=0) {
tin[v] = ++tim, par[0][v] = p, h[v] = h[p] + 1;
for(int i=1; i<lg; i++) par[i][v] = par[i-1][par[i-1][v]];
for(int u : adj[v]) {
if(u == p) continue;
init(u, v);
}
tout[v] = tim+1;
}
int getPar(int v, int dist) {
for(int i=0; i<lg; i++) if((dist>>i)%2 == 1) v = par[i][v];
return v;
}
int LCA(int v, int u) {
if(h[v] > h[u]) swap(v, u);
u = getPar(u, h[u] - h[v]);
if(v == u) return v;
for(int i=lg-1; i>=0; i--) {
if(par[i][v] != par[i][u]) {
v = par[i][v], u = par[i][u];
}
}
return par[0][v];
}
int calc(int v, int u) {
int lca = LCA(v, u);
return h[v]+h[u]-2*h[lca];
}
bool insub(int v, int u) {
if(tin[v] <= tin[u] && tout[v] > tin[u]) return true;
return false;
}
bool isinpath(int w, int v, int u) {
int lca = LCA(v, u);
if(insub(lca,w)==1&&(insub(w,v)==1||insub(w,u)==1)) return 1;
return 0;
}
int calc2(int v, int u) {
int res = 0;
for(int i=1; i<=m; i++) {
if(isinpath(arr[i][4],v,u)==1) {
res |= (1<<(i-1));
}
}
return res;
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>s>>t;
for(int v,u,w,i=1; i<n; i++) {
cin>>v>>u>>w;
adj[v].pb(u); adj[u].pb(v);
if(w > 0) {
m++;
arr[m][3]=w, arr[m][0]=v, arr[m][1]=u;
}
}
init(1);
for(int i=1; i<=m; i++) {
if(calc(arr[i][0], s) < calc(arr[i][1], s)) {
arr[i][2] = arr[i][0];
arr[i][4] = arr[i][1];
} else {
arr[i][2] = arr[i][1];
arr[i][4] = arr[i][0];
}
mark[arr[i][4]] = 1;
}
if(calc2(s, t) == 0) kill(calc(s, t));
for(int i=1; i<=m; i++) {
for(int j=1; j<=m; j++) {
dst[i][j] = calc(arr[i][2], arr[j][3]) + calc(arr[j][3], arr[j][2]);
nd[i][j] = (calc2(arr[i][2], arr[j][3]) | calc2(arr[j][3], arr[j][2]));
}
nd[0][i] = (calc2(s, arr[i][2]) | calc2(s, arr[i][3]));
nd[i][m+1] = calc2(arr[i][2], t);
}
for(int i=0; i<=m+1; i++) {
for(int j=0; j<=m+1; j++) {
int v = (i==0?s:(i==m+1?t:arr[i][2]));
int u = (j==0?s:(j==m+1?t:arr[j][2]));
if(i!=0 && i!=m+1 && j!=0 && j!=m+1) continue;
if(i==0&&j<=m) {
dst[i][j] = calc(v, arr[j][3]) + calc(arr[j][3], arr[j][2]);
} else {
dst[i][j] = calc(v, u);
}
}
}
for(int i=0; i<(1<<m); i++) fill(dp[i],dp[i]+21,1e9);
for(int i=1; i<=m; i++) {
if(nd[0][i] == 0) {
dp[(1<<(i-1))][i] = dst[0][i];
}
}
//fuck(nd[1][1]);
int ans = 1e9;
for(int mask=1; mask<(1<<m); mask++) {
for(int i=1; i<=m; i++) {
if((mask>>(i-1))%2==0) continue;
if(__builtin_popcount(mask) > 1) {
dp[mask][i] = 1e9;
int mask2 = mask - (1<<(i-1));
for(int j=1; j<=m; j++) {
if((mask2>>(j-1))%2==0) continue;
//int mask3 = mask2 - (1<<(j-1));
if((nd[j][i]&mask2) == nd[j][i]) {
dp[mask][i] = min(dp[mask][i], dp[mask2][j]+dst[j][i]);
}
}
}
if(mark[t] == 1) {
if(arr[i][4]==t)ans = min(ans, dp[mask][i]+1);
} else if((nd[i][m+1]&mask)==nd[i][m+1]) ans = min(ans, dp[mask][i]+dst[i][m+1]);
}
}
//fuck(nd[1][2]);
cout<<(ans==1e9 ? -1 : ans)<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
47708 KB |
Output is correct |
3 |
Correct |
52 ms |
57444 KB |
Output is correct |
4 |
Correct |
56 ms |
58960 KB |
Output is correct |
5 |
Correct |
52 ms |
57304 KB |
Output is correct |
6 |
Correct |
63 ms |
57640 KB |
Output is correct |
7 |
Correct |
53 ms |
57500 KB |
Output is correct |
8 |
Correct |
58 ms |
57480 KB |
Output is correct |
9 |
Correct |
53 ms |
57528 KB |
Output is correct |
10 |
Correct |
55 ms |
59160 KB |
Output is correct |
11 |
Correct |
52 ms |
57428 KB |
Output is correct |
12 |
Correct |
55 ms |
58188 KB |
Output is correct |
13 |
Correct |
52 ms |
60848 KB |
Output is correct |
14 |
Correct |
52 ms |
58192 KB |
Output is correct |
15 |
Correct |
49 ms |
58452 KB |
Output is correct |
16 |
Correct |
59 ms |
59472 KB |
Output is correct |
17 |
Correct |
50 ms |
58996 KB |
Output is correct |
18 |
Correct |
55 ms |
58716 KB |
Output is correct |
19 |
Correct |
55 ms |
61776 KB |
Output is correct |
20 |
Correct |
54 ms |
60400 KB |
Output is correct |
21 |
Correct |
56 ms |
63572 KB |
Output is correct |
22 |
Correct |
8 ms |
47704 KB |
Output is correct |
23 |
Correct |
9 ms |
47964 KB |
Output is correct |
24 |
Correct |
9 ms |
49840 KB |
Output is correct |
25 |
Correct |
9 ms |
47960 KB |
Output is correct |
26 |
Correct |
8 ms |
47704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
47708 KB |
Output is correct |
2 |
Correct |
9 ms |
47704 KB |
Output is correct |
3 |
Correct |
9 ms |
50008 KB |
Output is correct |
4 |
Correct |
9 ms |
48216 KB |
Output is correct |
5 |
Correct |
429 ms |
91660 KB |
Output is correct |
6 |
Correct |
422 ms |
91480 KB |
Output is correct |
7 |
Correct |
441 ms |
91660 KB |
Output is correct |
8 |
Correct |
428 ms |
91660 KB |
Output is correct |
9 |
Correct |
427 ms |
93456 KB |
Output is correct |
10 |
Correct |
410 ms |
93684 KB |
Output is correct |
11 |
Correct |
441 ms |
91672 KB |
Output is correct |
12 |
Correct |
419 ms |
91712 KB |
Output is correct |
13 |
Correct |
412 ms |
103768 KB |
Output is correct |
14 |
Correct |
428 ms |
103760 KB |
Output is correct |
15 |
Correct |
429 ms |
104016 KB |
Output is correct |
16 |
Correct |
423 ms |
103760 KB |
Output is correct |
17 |
Correct |
427 ms |
103772 KB |
Output is correct |
18 |
Correct |
408 ms |
103812 KB |
Output is correct |
19 |
Correct |
411 ms |
101796 KB |
Output is correct |
20 |
Correct |
412 ms |
99812 KB |
Output is correct |
21 |
Correct |
409 ms |
99788 KB |
Output is correct |
22 |
Correct |
422 ms |
99992 KB |
Output is correct |
23 |
Correct |
403 ms |
99672 KB |
Output is correct |
24 |
Correct |
411 ms |
99956 KB |
Output is correct |
25 |
Correct |
825 ms |
144316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
47708 KB |
Output is correct |
3 |
Correct |
52 ms |
57444 KB |
Output is correct |
4 |
Correct |
56 ms |
58960 KB |
Output is correct |
5 |
Correct |
52 ms |
57304 KB |
Output is correct |
6 |
Correct |
63 ms |
57640 KB |
Output is correct |
7 |
Correct |
53 ms |
57500 KB |
Output is correct |
8 |
Correct |
58 ms |
57480 KB |
Output is correct |
9 |
Correct |
53 ms |
57528 KB |
Output is correct |
10 |
Correct |
55 ms |
59160 KB |
Output is correct |
11 |
Correct |
52 ms |
57428 KB |
Output is correct |
12 |
Correct |
55 ms |
58188 KB |
Output is correct |
13 |
Correct |
52 ms |
60848 KB |
Output is correct |
14 |
Correct |
52 ms |
58192 KB |
Output is correct |
15 |
Correct |
49 ms |
58452 KB |
Output is correct |
16 |
Correct |
59 ms |
59472 KB |
Output is correct |
17 |
Correct |
50 ms |
58996 KB |
Output is correct |
18 |
Correct |
55 ms |
58716 KB |
Output is correct |
19 |
Correct |
55 ms |
61776 KB |
Output is correct |
20 |
Correct |
54 ms |
60400 KB |
Output is correct |
21 |
Correct |
56 ms |
63572 KB |
Output is correct |
22 |
Correct |
8 ms |
47704 KB |
Output is correct |
23 |
Correct |
9 ms |
47964 KB |
Output is correct |
24 |
Correct |
9 ms |
49840 KB |
Output is correct |
25 |
Correct |
9 ms |
47960 KB |
Output is correct |
26 |
Correct |
8 ms |
47704 KB |
Output is correct |
27 |
Correct |
8 ms |
47708 KB |
Output is correct |
28 |
Correct |
9 ms |
47704 KB |
Output is correct |
29 |
Correct |
9 ms |
50008 KB |
Output is correct |
30 |
Correct |
9 ms |
48216 KB |
Output is correct |
31 |
Correct |
429 ms |
91660 KB |
Output is correct |
32 |
Correct |
422 ms |
91480 KB |
Output is correct |
33 |
Correct |
441 ms |
91660 KB |
Output is correct |
34 |
Correct |
428 ms |
91660 KB |
Output is correct |
35 |
Correct |
427 ms |
93456 KB |
Output is correct |
36 |
Correct |
410 ms |
93684 KB |
Output is correct |
37 |
Correct |
441 ms |
91672 KB |
Output is correct |
38 |
Correct |
419 ms |
91712 KB |
Output is correct |
39 |
Correct |
412 ms |
103768 KB |
Output is correct |
40 |
Correct |
428 ms |
103760 KB |
Output is correct |
41 |
Correct |
429 ms |
104016 KB |
Output is correct |
42 |
Correct |
423 ms |
103760 KB |
Output is correct |
43 |
Correct |
427 ms |
103772 KB |
Output is correct |
44 |
Correct |
408 ms |
103812 KB |
Output is correct |
45 |
Correct |
411 ms |
101796 KB |
Output is correct |
46 |
Correct |
412 ms |
99812 KB |
Output is correct |
47 |
Correct |
409 ms |
99788 KB |
Output is correct |
48 |
Correct |
422 ms |
99992 KB |
Output is correct |
49 |
Correct |
403 ms |
99672 KB |
Output is correct |
50 |
Correct |
411 ms |
99956 KB |
Output is correct |
51 |
Correct |
825 ms |
144316 KB |
Output is correct |
52 |
Correct |
1066 ms |
166452 KB |
Output is correct |
53 |
Correct |
1085 ms |
166296 KB |
Output is correct |
54 |
Correct |
1109 ms |
166080 KB |
Output is correct |
55 |
Correct |
1077 ms |
166884 KB |
Output is correct |
56 |
Correct |
1053 ms |
166112 KB |
Output is correct |
57 |
Correct |
1062 ms |
166152 KB |
Output is correct |
58 |
Correct |
9 ms |
49756 KB |
Output is correct |
59 |
Correct |
9 ms |
50012 KB |
Output is correct |
60 |
Correct |
9 ms |
49752 KB |
Output is correct |
61 |
Correct |
1104 ms |
166008 KB |
Output is correct |
62 |
Correct |
1117 ms |
177036 KB |
Output is correct |
63 |
Correct |
1092 ms |
171340 KB |
Output is correct |
64 |
Correct |
64 ms |
64336 KB |
Output is correct |
65 |
Correct |
64 ms |
62548 KB |
Output is correct |
66 |
Correct |
1104 ms |
167464 KB |
Output is correct |
67 |
Correct |
1100 ms |
165600 KB |
Output is correct |
68 |
Correct |
1340 ms |
208036 KB |
Output is correct |
69 |
Correct |
1347 ms |
208060 KB |
Output is correct |
70 |
Correct |
1298 ms |
207440 KB |
Output is correct |
71 |
Correct |
1343 ms |
207600 KB |
Output is correct |
72 |
Correct |
1326 ms |
207416 KB |
Output is correct |
73 |
Correct |
1260 ms |
208168 KB |
Output is correct |
74 |
Correct |
1279 ms |
208684 KB |
Output is correct |
75 |
Correct |
1304 ms |
208328 KB |
Output is correct |
76 |
Correct |
1294 ms |
208696 KB |
Output is correct |
77 |
Correct |
1276 ms |
207900 KB |
Output is correct |
78 |
Correct |
1503 ms |
213496 KB |
Output is correct |
79 |
Correct |
1418 ms |
215152 KB |
Output is correct |
80 |
Correct |
1402 ms |
210172 KB |
Output is correct |
81 |
Correct |
1409 ms |
216712 KB |
Output is correct |
82 |
Correct |
1449 ms |
218044 KB |
Output is correct |
83 |
Correct |
1433 ms |
222032 KB |
Output is correct |
84 |
Correct |
1481 ms |
229244 KB |
Output is correct |
85 |
Correct |
1501 ms |
250300 KB |
Output is correct |
86 |
Correct |
1510 ms |
231252 KB |
Output is correct |
87 |
Correct |
1544 ms |
233024 KB |
Output is correct |