#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <bits/stdc++.h>
#define initrand mt19937 mt_rand(time(0));
#define rand mt_rand()
#define int long long
#define INF 10000000000000000
#define MOD 1000000007
using namespace std;
const int N = 200005;
const int K = 1000005;
map<int, bool> vis;
vector<pair<int, int>> adj[N];
vector<int> adj1[N];
int tb = 0;
int t = 0;
int tin[N], tout[N];
int depth[N], edges[N];
bool chosen[N];
int h[N][20];
int s[N], size = 0;
int ans = INF;
int LCA_DFS(int i, int par, int dep, int edep){
tin[i] = ++t;
depth[i] = dep;
edges[i] = edep;
if(i!=par){
h[i][0] = par;
for(int j = 1;j<=19;j++){
if(h[i][j-1] != -1) h[i][j] = h[h[i][j-1]][j-1];
}
}
for(pair<int, int> j: adj[i]){
if(j.first == par) continue;
LCA_DFS(j.first, i, dep + j.second, edep+1);
}
tout[i] = ++t;
}
bool anc(int u, int v){
if((tin[u] <= tin[v]) && (tout[u] >= tout[v])) return true;
return false;
}
int LCA(int u, int v){
if(anc(u, v)){
return u;
}
if(anc(v, u)) return v;
for(int i = 19;i>=0;i--){
if(h[u][i]!=-1 && !anc(h[u][i], v)) u = h[u][i];
}
return h[u][0];
}
pair<int, int> dist(int u, int v){
return {(depth[u] + depth[v] - 2*depth[LCA(u, v)]), (edges[u] + edges[v] - 2*edges[LCA(u, v)])};
}
int dfs(int i, int par){
size++;
s[i] = 1;
for(pair<int, int> j: adj[i]){
if(j.first == par) continue;
if(chosen[j.first]) continue;
s[i] += dfs(j.first, i);
}
return s[i];
}
int centroid(int i, int par){
for(pair<int, int> j: adj[i]){
if(j.first == par) continue;
if(chosen[j.first]) continue;
if(s[j.first] <= (size/2)) continue;
return centroid(j.first, i);
}
return i;
}
int decompose(int i, int par){
size = 0;
dfs(i, par);
int pp = centroid(i, par);
chosen[pp] = true;
for(pair<int, int> j: adj[pp]){
if(chosen[j.first]) continue;
int child = decompose(j.first, pp);
//cout<<pp<<" is a parent of "<<child<<endl;
adj1[pp].push_back(child);
}
return pp;
}
int findans(int i, int src, int mine[]){
pair<int, int> d = dist(i, src);
if(d.first<=tb) if(vis[tb - d.first]) ans = min(ans, d.second + mine[tb - d.first]);
for(int j: adj1[i]){
findans(j, src, mine);
}
}
int update(int i, int src, int mine[]){
pair<int, int> d = dist(i, src);
//cout<<"Distance from "<<i<<" to "<<src<<" is "<<d.first<<endl;
if(d.first<=tb){
if(!vis[d.first]) mine[d.first] = d.second;
else mine[d.first] = min(mine[d.first], d.second);
vis[d.first] = true;
}
for(int j: adj1[i]){
update(j, src, mine);
}
}
int getans(int i, int mine[]){
for(int j: adj1[i]){
findans(j, i, mine);
update(j, i, mine);
}
}
signed best_path(signed n, signed k, signed hh[][2], signed l[]){
tb = k;
ans = INF;
for(int i = 0;i<(n-1);i++){
adj[hh[i][0]+1].push_back({hh[i][1]+1, l[i]});
adj[hh[i][1]+1].push_back({hh[i][0]+1, l[i]});
}
for(int i = 1;i<=n;i++){
for(int j = 0;j<20;j++) h[i][j] = -1;
}
LCA_DFS(1, 1, 0, 0);
int cr = decompose(1, 1);
//cr is the new root of our tree
for(int i = 1;i<=n;i++){
int mine[K];
vis.clear();
mine[0] = 0;
vis[0] = true;
getans(i, mine);
}
if(ans>=INF) ans = -1;
return ans;
}
/*signed main(){
signed n, k;
cin>>n>>k;
signed bitch[n-1][2];
signed l[n-1];
for(int i =0 ;i<(n-1);i++) cin>>bitch[i][0]>>bitch[i][1]>>l[i];
cout<<best_path(n, k, bitch, l)<<endl;
}*/
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/
/*
3 3
0 1 1
1 2 1
*/
Compilation message
race.cpp: In function 'long long int LCA_DFS(long long int, long long int, long long int, long long int)':
race.cpp:44:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
race.cpp: In function 'long long int findans(long long int, long long int, long long int*)':
race.cpp:100:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
race.cpp: In function 'long long int update(long long int, long long int, long long int*)':
race.cpp:112:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
race.cpp: In function 'long long int getans(long long int, long long int*)':
race.cpp:118:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:130:9: warning: unused variable 'cr' [-Wunused-variable]
int cr = decompose(1, 1);
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
6 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
6 ms |
9856 KB |
Output is correct |
11 |
Correct |
6 ms |
9856 KB |
Output is correct |
12 |
Correct |
7 ms |
9856 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
6 ms |
9856 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
6 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
6 ms |
9856 KB |
Output is correct |
11 |
Correct |
6 ms |
9856 KB |
Output is correct |
12 |
Correct |
7 ms |
9856 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
6 ms |
9856 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
19 |
Correct |
8 ms |
9728 KB |
Output is correct |
20 |
Correct |
6 ms |
9856 KB |
Output is correct |
21 |
Correct |
10 ms |
10112 KB |
Output is correct |
22 |
Correct |
12 ms |
13440 KB |
Output is correct |
23 |
Correct |
11 ms |
12928 KB |
Output is correct |
24 |
Correct |
11 ms |
14208 KB |
Output is correct |
25 |
Correct |
11 ms |
13312 KB |
Output is correct |
26 |
Correct |
11 ms |
12416 KB |
Output is correct |
27 |
Correct |
7 ms |
10112 KB |
Output is correct |
28 |
Correct |
11 ms |
11648 KB |
Output is correct |
29 |
Correct |
11 ms |
12636 KB |
Output is correct |
30 |
Correct |
13 ms |
12928 KB |
Output is correct |
31 |
Correct |
12 ms |
13440 KB |
Output is correct |
32 |
Correct |
13 ms |
13440 KB |
Output is correct |
33 |
Correct |
13 ms |
14976 KB |
Output is correct |
34 |
Correct |
13 ms |
14080 KB |
Output is correct |
35 |
Correct |
11 ms |
14976 KB |
Output is correct |
36 |
Correct |
11 ms |
15400 KB |
Output is correct |
37 |
Correct |
11 ms |
13396 KB |
Output is correct |
38 |
Correct |
11 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
6 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
6 ms |
9856 KB |
Output is correct |
11 |
Correct |
6 ms |
9856 KB |
Output is correct |
12 |
Correct |
7 ms |
9856 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
6 ms |
9856 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
19 |
Correct |
550 ms |
37568 KB |
Output is correct |
20 |
Correct |
521 ms |
38904 KB |
Output is correct |
21 |
Correct |
492 ms |
38904 KB |
Output is correct |
22 |
Correct |
428 ms |
39032 KB |
Output is correct |
23 |
Correct |
335 ms |
39288 KB |
Output is correct |
24 |
Correct |
196 ms |
38020 KB |
Output is correct |
25 |
Correct |
460 ms |
41976 KB |
Output is correct |
26 |
Correct |
186 ms |
45048 KB |
Output is correct |
27 |
Correct |
390 ms |
67612 KB |
Output is correct |
28 |
Correct |
1094 ms |
80504 KB |
Output is correct |
29 |
Correct |
1135 ms |
79608 KB |
Output is correct |
30 |
Correct |
401 ms |
67576 KB |
Output is correct |
31 |
Correct |
403 ms |
67576 KB |
Output is correct |
32 |
Correct |
667 ms |
67664 KB |
Output is correct |
33 |
Correct |
1420 ms |
67304 KB |
Output is correct |
34 |
Correct |
1128 ms |
68028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9856 KB |
Output is correct |
2 |
Correct |
7 ms |
9856 KB |
Output is correct |
3 |
Correct |
6 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
7 ms |
9856 KB |
Output is correct |
6 |
Correct |
7 ms |
9856 KB |
Output is correct |
7 |
Correct |
6 ms |
9856 KB |
Output is correct |
8 |
Correct |
8 ms |
9856 KB |
Output is correct |
9 |
Correct |
8 ms |
9856 KB |
Output is correct |
10 |
Correct |
6 ms |
9856 KB |
Output is correct |
11 |
Correct |
6 ms |
9856 KB |
Output is correct |
12 |
Correct |
7 ms |
9856 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9856 KB |
Output is correct |
15 |
Correct |
10 ms |
9856 KB |
Output is correct |
16 |
Correct |
6 ms |
9856 KB |
Output is correct |
17 |
Correct |
6 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9856 KB |
Output is correct |
19 |
Correct |
8 ms |
9728 KB |
Output is correct |
20 |
Correct |
6 ms |
9856 KB |
Output is correct |
21 |
Correct |
10 ms |
10112 KB |
Output is correct |
22 |
Correct |
12 ms |
13440 KB |
Output is correct |
23 |
Correct |
11 ms |
12928 KB |
Output is correct |
24 |
Correct |
11 ms |
14208 KB |
Output is correct |
25 |
Correct |
11 ms |
13312 KB |
Output is correct |
26 |
Correct |
11 ms |
12416 KB |
Output is correct |
27 |
Correct |
7 ms |
10112 KB |
Output is correct |
28 |
Correct |
11 ms |
11648 KB |
Output is correct |
29 |
Correct |
11 ms |
12636 KB |
Output is correct |
30 |
Correct |
13 ms |
12928 KB |
Output is correct |
31 |
Correct |
12 ms |
13440 KB |
Output is correct |
32 |
Correct |
13 ms |
13440 KB |
Output is correct |
33 |
Correct |
13 ms |
14976 KB |
Output is correct |
34 |
Correct |
13 ms |
14080 KB |
Output is correct |
35 |
Correct |
11 ms |
14976 KB |
Output is correct |
36 |
Correct |
11 ms |
15400 KB |
Output is correct |
37 |
Correct |
11 ms |
13396 KB |
Output is correct |
38 |
Correct |
11 ms |
12288 KB |
Output is correct |
39 |
Correct |
550 ms |
37568 KB |
Output is correct |
40 |
Correct |
521 ms |
38904 KB |
Output is correct |
41 |
Correct |
492 ms |
38904 KB |
Output is correct |
42 |
Correct |
428 ms |
39032 KB |
Output is correct |
43 |
Correct |
335 ms |
39288 KB |
Output is correct |
44 |
Correct |
196 ms |
38020 KB |
Output is correct |
45 |
Correct |
460 ms |
41976 KB |
Output is correct |
46 |
Correct |
186 ms |
45048 KB |
Output is correct |
47 |
Correct |
390 ms |
67612 KB |
Output is correct |
48 |
Correct |
1094 ms |
80504 KB |
Output is correct |
49 |
Correct |
1135 ms |
79608 KB |
Output is correct |
50 |
Correct |
401 ms |
67576 KB |
Output is correct |
51 |
Correct |
403 ms |
67576 KB |
Output is correct |
52 |
Correct |
667 ms |
67664 KB |
Output is correct |
53 |
Correct |
1420 ms |
67304 KB |
Output is correct |
54 |
Correct |
1128 ms |
68028 KB |
Output is correct |
55 |
Correct |
40 ms |
12928 KB |
Output is correct |
56 |
Correct |
32 ms |
12672 KB |
Output is correct |
57 |
Correct |
252 ms |
39160 KB |
Output is correct |
58 |
Correct |
76 ms |
38236 KB |
Output is correct |
59 |
Correct |
582 ms |
54648 KB |
Output is correct |
60 |
Correct |
2741 ms |
106352 KB |
Output is correct |
61 |
Correct |
514 ms |
68216 KB |
Output is correct |
62 |
Correct |
551 ms |
68216 KB |
Output is correct |
63 |
Correct |
879 ms |
68216 KB |
Output is correct |
64 |
Correct |
2556 ms |
83024 KB |
Output is correct |
65 |
Correct |
780 ms |
69144 KB |
Output is correct |
66 |
Correct |
2096 ms |
86156 KB |
Output is correct |
67 |
Correct |
265 ms |
67804 KB |
Output is correct |
68 |
Correct |
979 ms |
82072 KB |
Output is correct |
69 |
Correct |
1038 ms |
82264 KB |
Output is correct |
70 |
Correct |
922 ms |
79096 KB |
Output is correct |