#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;
vector<pair<int, int>> adj[N];
int tb = 0;
int t = 0;
int tin[N], tout[N];
int depth[N], edges[N];
int h[N][20];
int s[N], size = 0;
int ans = INF;
vector<int> ett;
map<int, int> d;
map<int, bool> vis;
int LCA_DFS(int i, int par, int dep, int edep){
tin[i] = ett.size();
ett.push_back(i);
depth[i] = dep;
edges[i] = edep;
s[i] = 1;
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;
s[i] += LCA_DFS(j.first, i, dep + j.second, edep+1);
}
tout[i] = ett.size();
return s[i];
}
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)])};
}
pair<int, int> dfs(int i, int par, int keep){
pair<int, int> comp = {0, 0};
int bigChild = -1, mx = 0;
for(pair<int, int> j: adj[i]){
if(j.first == par) continue;
if(s[j.first] > mx){
bigChild = j.first;
mx = s[j.first];
}
}
for(pair<int, int> j: adj[i]){
if(j.first == par || j.first == bigChild) continue;
dfs(j.first, i, 0);
}
if(bigChild != -1){
comp = dfs(bigChild, i, 1);
comp.first += dist(bigChild, i).first; //d[x] is actually at d[x - comp.first]
//when we want d[x], we have to try d[x + ]
comp.second++;//d[x] = y actually means d[x] = y + comp.second
}
vis[0 - comp.first] = true;
d[0 - comp.first] = -comp.second;
if(vis[tb - comp.first]) ans = min(ans, d[tb-comp.first] + comp.second);
for(pair<int, int> j: adj[i]){
if(j.first == par || j.first == bigChild) continue;
for(int x = tin[j.first];x<tout[j.first];x++){
pair<int, int> lol = dist(i, ett[x]);
if(lol.first > tb) continue;
if(vis[tb - lol.first - comp.first]) ans = min(ans, d[tb-lol.first-comp.first] + comp.second + lol.second);
}
for(int x = tin[j.first];x<tout[j.first];x++){
pair<int, int> lol = dist(i, ett[x]);
if(lol.first > tb) continue;
if(!vis[lol.first - comp.first]){
vis[lol.first - comp.first] = true;
d[lol.first - comp.first] = lol.second - comp.second;
}
else{
d[lol.first - comp.first] = min(d[lol.first - comp.first], lol.second - comp.second);
}
}
}
if(keep == 0){
d.clear();
vis.clear();
}
return comp;
}
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);
dfs(1 ,1 ,0);
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5084 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5084 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5376 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
4 ms |
5376 KB |
Output is correct |
24 |
Correct |
5 ms |
5376 KB |
Output is correct |
25 |
Correct |
6 ms |
5504 KB |
Output is correct |
26 |
Correct |
6 ms |
5376 KB |
Output is correct |
27 |
Correct |
4 ms |
5376 KB |
Output is correct |
28 |
Correct |
5 ms |
5504 KB |
Output is correct |
29 |
Correct |
6 ms |
5504 KB |
Output is correct |
30 |
Correct |
6 ms |
5504 KB |
Output is correct |
31 |
Correct |
6 ms |
5504 KB |
Output is correct |
32 |
Correct |
6 ms |
5632 KB |
Output is correct |
33 |
Correct |
6 ms |
5504 KB |
Output is correct |
34 |
Correct |
5 ms |
5504 KB |
Output is correct |
35 |
Correct |
6 ms |
5504 KB |
Output is correct |
36 |
Correct |
5 ms |
5504 KB |
Output is correct |
37 |
Correct |
5 ms |
5504 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5084 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
268 ms |
33396 KB |
Output is correct |
20 |
Correct |
256 ms |
33432 KB |
Output is correct |
21 |
Correct |
258 ms |
33396 KB |
Output is correct |
22 |
Correct |
278 ms |
33524 KB |
Output is correct |
23 |
Correct |
190 ms |
34036 KB |
Output is correct |
24 |
Correct |
181 ms |
33140 KB |
Output is correct |
25 |
Correct |
166 ms |
42480 KB |
Output is correct |
26 |
Correct |
92 ms |
50056 KB |
Output is correct |
27 |
Correct |
292 ms |
61676 KB |
Output is correct |
28 |
Correct |
514 ms |
132292 KB |
Output is correct |
29 |
Correct |
512 ms |
127068 KB |
Output is correct |
30 |
Correct |
284 ms |
61680 KB |
Output is correct |
31 |
Correct |
284 ms |
61732 KB |
Output is correct |
32 |
Correct |
408 ms |
62128 KB |
Output is correct |
33 |
Correct |
395 ms |
61036 KB |
Output is correct |
34 |
Correct |
405 ms |
61804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5084 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5376 KB |
Output is correct |
22 |
Correct |
5 ms |
5376 KB |
Output is correct |
23 |
Correct |
4 ms |
5376 KB |
Output is correct |
24 |
Correct |
5 ms |
5376 KB |
Output is correct |
25 |
Correct |
6 ms |
5504 KB |
Output is correct |
26 |
Correct |
6 ms |
5376 KB |
Output is correct |
27 |
Correct |
4 ms |
5376 KB |
Output is correct |
28 |
Correct |
5 ms |
5504 KB |
Output is correct |
29 |
Correct |
6 ms |
5504 KB |
Output is correct |
30 |
Correct |
6 ms |
5504 KB |
Output is correct |
31 |
Correct |
6 ms |
5504 KB |
Output is correct |
32 |
Correct |
6 ms |
5632 KB |
Output is correct |
33 |
Correct |
6 ms |
5504 KB |
Output is correct |
34 |
Correct |
5 ms |
5504 KB |
Output is correct |
35 |
Correct |
6 ms |
5504 KB |
Output is correct |
36 |
Correct |
5 ms |
5504 KB |
Output is correct |
37 |
Correct |
5 ms |
5504 KB |
Output is correct |
38 |
Correct |
5 ms |
5504 KB |
Output is correct |
39 |
Correct |
268 ms |
33396 KB |
Output is correct |
40 |
Correct |
256 ms |
33432 KB |
Output is correct |
41 |
Correct |
258 ms |
33396 KB |
Output is correct |
42 |
Correct |
278 ms |
33524 KB |
Output is correct |
43 |
Correct |
190 ms |
34036 KB |
Output is correct |
44 |
Correct |
181 ms |
33140 KB |
Output is correct |
45 |
Correct |
166 ms |
42480 KB |
Output is correct |
46 |
Correct |
92 ms |
50056 KB |
Output is correct |
47 |
Correct |
292 ms |
61676 KB |
Output is correct |
48 |
Correct |
514 ms |
132292 KB |
Output is correct |
49 |
Correct |
512 ms |
127068 KB |
Output is correct |
50 |
Correct |
284 ms |
61680 KB |
Output is correct |
51 |
Correct |
284 ms |
61732 KB |
Output is correct |
52 |
Correct |
408 ms |
62128 KB |
Output is correct |
53 |
Correct |
395 ms |
61036 KB |
Output is correct |
54 |
Correct |
405 ms |
61804 KB |
Output is correct |
55 |
Correct |
28 ms |
8696 KB |
Output is correct |
56 |
Correct |
18 ms |
7936 KB |
Output is correct |
57 |
Correct |
171 ms |
33516 KB |
Output is correct |
58 |
Correct |
87 ms |
33384 KB |
Output is correct |
59 |
Correct |
183 ms |
68460 KB |
Output is correct |
60 |
Correct |
588 ms |
130280 KB |
Output is correct |
61 |
Correct |
362 ms |
62440 KB |
Output is correct |
62 |
Correct |
320 ms |
62312 KB |
Output is correct |
63 |
Correct |
459 ms |
62308 KB |
Output is correct |
64 |
Correct |
1486 ms |
81668 KB |
Output is correct |
65 |
Correct |
432 ms |
62828 KB |
Output is correct |
66 |
Correct |
552 ms |
115556 KB |
Output is correct |
67 |
Correct |
313 ms |
63068 KB |
Output is correct |
68 |
Correct |
722 ms |
88036 KB |
Output is correct |
69 |
Correct |
743 ms |
88904 KB |
Output is correct |
70 |
Correct |
787 ms |
85480 KB |
Output is correct |