#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
vector<vector<PII>> adj(MAXN);
vector<int>sub(MAXN);
vector<bool>ded(MAXN);
int ans = 1e9,k = 0;
int cen(int v,int u,int n,int &mn,int &id){
int mx = 0;
sub[v] = 1;
for(pii y:adj[v]){
int x = y.fi;
if(x !=u && !ded[x]){
int z = cen(x,v,n,mn,id);
sub[v]+=z;
mx = max(mx,z);
}
}
if(u != -1 && !ded[u])mx = max(mx,n-sub[v]);
if(mn > mx){
mn = mx;
id = v;
}
return sub[v];
}
vector<int>minlen(1e6+5,1e9);
vector<int>changed;
//I find the bookkeeping harder than the actual problem : )))
void dfs(int v,int u,ll l,int d,bool mode){
if(l > k)return;
//cout<<v<<" "<<l<<" "<<d<<'\n';
for(pii x:adj[v]){
if(!ded[x.fi] && x.fi != u){
dfs(x.fi,v,l+x.se,d+1,mode);
}
}
if(!mode){
ll need = k-l;
if(minlen[need] != 1e9){
ans = min(ans,d+minlen[need]);
}
}else {
minlen[l] = min(minlen[l],d);
changed.pb(l);
}
}
void solve(int v,int n){
int mn=1e9,u = 0;
cen(v,-1,n,mn,u);
ded[u] = 1;
minlen[0] = 0;
for(pii x:adj[u]){
if(!ded[x.fi]){
dfs(x.fi,u,x.se,1,0);
dfs(x.fi,u,x.se,1,1);
//for(int i=0;i<=k;i++)cout<<minlen[k]<<" ";
//cout<<'\n';
}
}
for(int x:changed)minlen[x] = 1e9;
changed.clear();
for(pii x:adj[u]){
if(!ded[x.fi]){
solve(x.fi,sub[x.fi]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int n = N;k = K;
//cout<<"k:"<<k<<'\n';
for(int i=0;i<n-1;i++){
adj[H[i][1]].pb({H[i][0],L[i]});
adj[H[i][0]].pb({H[i][1],L[i]});
}
solve(1,n);
if(ans == 1e9)ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11868 KB |
Output is correct |
2 |
Correct |
3 ms |
11868 KB |
Output is correct |
3 |
Correct |
3 ms |
11868 KB |
Output is correct |
4 |
Correct |
3 ms |
11868 KB |
Output is correct |
5 |
Correct |
3 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
3 ms |
11864 KB |
Output is correct |
8 |
Correct |
4 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11868 KB |
Output is correct |
11 |
Correct |
4 ms |
11864 KB |
Output is correct |
12 |
Correct |
4 ms |
11868 KB |
Output is correct |
13 |
Correct |
4 ms |
11868 KB |
Output is correct |
14 |
Correct |
4 ms |
11868 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
4 ms |
11864 KB |
Output is correct |
18 |
Correct |
3 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11868 KB |
Output is correct |
2 |
Correct |
3 ms |
11868 KB |
Output is correct |
3 |
Correct |
3 ms |
11868 KB |
Output is correct |
4 |
Correct |
3 ms |
11868 KB |
Output is correct |
5 |
Correct |
3 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
3 ms |
11864 KB |
Output is correct |
8 |
Correct |
4 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11868 KB |
Output is correct |
11 |
Correct |
4 ms |
11864 KB |
Output is correct |
12 |
Correct |
4 ms |
11868 KB |
Output is correct |
13 |
Correct |
4 ms |
11868 KB |
Output is correct |
14 |
Correct |
4 ms |
11868 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
4 ms |
11864 KB |
Output is correct |
18 |
Correct |
3 ms |
11868 KB |
Output is correct |
19 |
Correct |
3 ms |
12076 KB |
Output is correct |
20 |
Correct |
3 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11864 KB |
Output is correct |
23 |
Correct |
3 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
25 |
Correct |
4 ms |
11868 KB |
Output is correct |
26 |
Correct |
4 ms |
11868 KB |
Output is correct |
27 |
Correct |
4 ms |
11868 KB |
Output is correct |
28 |
Correct |
3 ms |
11864 KB |
Output is correct |
29 |
Correct |
4 ms |
11868 KB |
Output is correct |
30 |
Correct |
4 ms |
11868 KB |
Output is correct |
31 |
Correct |
3 ms |
11868 KB |
Output is correct |
32 |
Correct |
3 ms |
11868 KB |
Output is correct |
33 |
Correct |
4 ms |
11868 KB |
Output is correct |
34 |
Correct |
5 ms |
11868 KB |
Output is correct |
35 |
Correct |
4 ms |
11868 KB |
Output is correct |
36 |
Correct |
5 ms |
11868 KB |
Output is correct |
37 |
Correct |
3 ms |
11868 KB |
Output is correct |
38 |
Correct |
3 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11868 KB |
Output is correct |
2 |
Correct |
3 ms |
11868 KB |
Output is correct |
3 |
Correct |
3 ms |
11868 KB |
Output is correct |
4 |
Correct |
3 ms |
11868 KB |
Output is correct |
5 |
Correct |
3 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
3 ms |
11864 KB |
Output is correct |
8 |
Correct |
4 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11868 KB |
Output is correct |
11 |
Correct |
4 ms |
11864 KB |
Output is correct |
12 |
Correct |
4 ms |
11868 KB |
Output is correct |
13 |
Correct |
4 ms |
11868 KB |
Output is correct |
14 |
Correct |
4 ms |
11868 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
4 ms |
11864 KB |
Output is correct |
18 |
Correct |
3 ms |
11868 KB |
Output is correct |
19 |
Correct |
92 ms |
19296 KB |
Output is correct |
20 |
Correct |
101 ms |
20564 KB |
Output is correct |
21 |
Correct |
104 ms |
20272 KB |
Output is correct |
22 |
Correct |
84 ms |
21200 KB |
Output is correct |
23 |
Correct |
55 ms |
20824 KB |
Output is correct |
24 |
Correct |
43 ms |
20152 KB |
Output is correct |
25 |
Correct |
91 ms |
28500 KB |
Output is correct |
26 |
Correct |
83 ms |
29688 KB |
Output is correct |
27 |
Correct |
110 ms |
26524 KB |
Output is correct |
28 |
Correct |
177 ms |
42580 KB |
Output is correct |
29 |
Correct |
172 ms |
36564 KB |
Output is correct |
30 |
Correct |
110 ms |
26960 KB |
Output is correct |
31 |
Correct |
139 ms |
27112 KB |
Output is correct |
32 |
Correct |
132 ms |
26804 KB |
Output is correct |
33 |
Correct |
143 ms |
25904 KB |
Output is correct |
34 |
Correct |
121 ms |
26196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11868 KB |
Output is correct |
2 |
Correct |
3 ms |
11868 KB |
Output is correct |
3 |
Correct |
3 ms |
11868 KB |
Output is correct |
4 |
Correct |
3 ms |
11868 KB |
Output is correct |
5 |
Correct |
3 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
3 ms |
11864 KB |
Output is correct |
8 |
Correct |
4 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11868 KB |
Output is correct |
11 |
Correct |
4 ms |
11864 KB |
Output is correct |
12 |
Correct |
4 ms |
11868 KB |
Output is correct |
13 |
Correct |
4 ms |
11868 KB |
Output is correct |
14 |
Correct |
4 ms |
11868 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
4 ms |
11864 KB |
Output is correct |
18 |
Correct |
3 ms |
11868 KB |
Output is correct |
19 |
Correct |
3 ms |
12076 KB |
Output is correct |
20 |
Correct |
3 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11864 KB |
Output is correct |
23 |
Correct |
3 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
25 |
Correct |
4 ms |
11868 KB |
Output is correct |
26 |
Correct |
4 ms |
11868 KB |
Output is correct |
27 |
Correct |
4 ms |
11868 KB |
Output is correct |
28 |
Correct |
3 ms |
11864 KB |
Output is correct |
29 |
Correct |
4 ms |
11868 KB |
Output is correct |
30 |
Correct |
4 ms |
11868 KB |
Output is correct |
31 |
Correct |
3 ms |
11868 KB |
Output is correct |
32 |
Correct |
3 ms |
11868 KB |
Output is correct |
33 |
Correct |
4 ms |
11868 KB |
Output is correct |
34 |
Correct |
5 ms |
11868 KB |
Output is correct |
35 |
Correct |
4 ms |
11868 KB |
Output is correct |
36 |
Correct |
5 ms |
11868 KB |
Output is correct |
37 |
Correct |
3 ms |
11868 KB |
Output is correct |
38 |
Correct |
3 ms |
11868 KB |
Output is correct |
39 |
Correct |
92 ms |
19296 KB |
Output is correct |
40 |
Correct |
101 ms |
20564 KB |
Output is correct |
41 |
Correct |
104 ms |
20272 KB |
Output is correct |
42 |
Correct |
84 ms |
21200 KB |
Output is correct |
43 |
Correct |
55 ms |
20824 KB |
Output is correct |
44 |
Correct |
43 ms |
20152 KB |
Output is correct |
45 |
Correct |
91 ms |
28500 KB |
Output is correct |
46 |
Correct |
83 ms |
29688 KB |
Output is correct |
47 |
Correct |
110 ms |
26524 KB |
Output is correct |
48 |
Correct |
177 ms |
42580 KB |
Output is correct |
49 |
Correct |
172 ms |
36564 KB |
Output is correct |
50 |
Correct |
110 ms |
26960 KB |
Output is correct |
51 |
Correct |
139 ms |
27112 KB |
Output is correct |
52 |
Correct |
132 ms |
26804 KB |
Output is correct |
53 |
Correct |
143 ms |
25904 KB |
Output is correct |
54 |
Correct |
121 ms |
26196 KB |
Output is correct |
55 |
Correct |
9 ms |
12380 KB |
Output is correct |
56 |
Correct |
10 ms |
12596 KB |
Output is correct |
57 |
Correct |
63 ms |
20440 KB |
Output is correct |
58 |
Correct |
30 ms |
20788 KB |
Output is correct |
59 |
Correct |
75 ms |
29652 KB |
Output is correct |
60 |
Correct |
314 ms |
36300 KB |
Output is correct |
61 |
Correct |
123 ms |
26708 KB |
Output is correct |
62 |
Correct |
139 ms |
28020 KB |
Output is correct |
63 |
Correct |
198 ms |
28028 KB |
Output is correct |
64 |
Correct |
295 ms |
26312 KB |
Output is correct |
65 |
Correct |
156 ms |
27580 KB |
Output is correct |
66 |
Correct |
231 ms |
39256 KB |
Output is correct |
67 |
Correct |
71 ms |
28860 KB |
Output is correct |
68 |
Correct |
172 ms |
27352 KB |
Output is correct |
69 |
Correct |
176 ms |
25044 KB |
Output is correct |
70 |
Correct |
146 ms |
27192 KB |
Output is correct |