#include <bits/stdc++.h>
using namespace std;
#define edge(xxxx,yyyy) ((xxxx)*(m+5)+(yyyy))
#define oo 1e9
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) (((xxxx)>>((aaaa)-1))&1)
#define totbit(xxxx) (32-__builtin_clz(xxxx))
#define totbitll(xxxx) (64-__builtin_clzll(ll(xxxx)))
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
const ll base=333333349;
const ll mod=1e9+7;
const ld eps=1e-5;
const ll maxn=50009;
ll depth[200009],dist[200009],child[200009],res,n,k;
vector<pll> g[200009];
map<ll,ll> cost[200009];
void dfs(ll x,ll y) {
//cout<<"!"<<x<<'\n';
depth[x]=depth[y]+1;
child[x]=1;
for (pii ii:g[x]) {
if (ii.fi!=y) {
dist[ii.fi]=dist[x]+ii.se;
dfs(ii.fi,x);
child[x]+=child[ii.fi];
}
}
}
void solve(ll x,ll y) {
//cout<<"?"<<x<<'\n';
ll cap=0;
ll pos=0;
for (pii ii:g[x]) {
if (ii.fi!=y) {
solve(ii.fi,x);
if (child[ii.fi]>cap) {
cap=child[ii.fi];
pos=ii.fi;
}
}
}
if (pos==0) {
cost[x][dist[x]]=depth[x];
//cout<<dist[x]<<' '<<depth[x]<<'\n';
//cout<<"-----------------\n";
return;
}
swap(cost[x],cost[pos]);
//for (map<ll,ll>::const_iterator ij=cost[x].begin();ij!=cost[x].end();ij++) {
//cout<<ij->first<<' '<<ij->second<<'\n';
//}
//cout<<"a\n";
for (pii ii:g[x]) {
if (ii.fi!=y&&ii.fi!=pos) {
for (map<ll,ll>::const_iterator ij=cost[ii.fi].begin();ij!=cost[ii.fi].end();ij++) {
if (ij->second==0) continue;
if (cost[x][k+2*dist[x]-(ij->first)]) res=min(res,(ij->second)+cost[x][k+2*dist[x]-(ij->first)]-2*depth[x]);
//cout<<ij->first<<' '<<ij->second<<'\n';
}
for (map<ll,ll>::const_iterator ij=cost[ii.fi].begin();ij!=cost[ii.fi].end();ij++) {
if (ij->second==0) continue;
//if (cost[x][k+2*dist[x]-(ij->first)]) res=min(res,(ij->second)+cost[x][k+2*dist[x]-(ij->first)]-2*depth[x]);
//cout<<ij->first<<' '<<ij->second<<'\n';
if (!cost[x][(ij->first)]) cost[x][(ij->first)]=(ij->second);
else cost[x][(ij->first)]=min((ij->second),cost[x][(ij->first)]);
}
}
}
if (cost[x][k+dist[x]]) res=min(res,cost[x][k+dist[x]]-depth[x]);
if (cost[x][dist[x]]) cost[x][dist[x]]=min(cost[x][dist[x]],depth[x]);
else cost[x][dist[x]]=depth[x];
//cout<<dist[x]<<' '<<depth[x]<<'\n';
//cout<<"-----------------\n";
//cout<<"!"<<res<<'\n';
}
ll best_path(int _n,int _k,int h[][2],int l[]) {
res=oo;
n=_n;
k=_k;
for (ll i=0;i<n-1;i++) {
g[h[i][0]+1].push_back({h[i][1]+1,l[i]});
g[h[i][1]+1].push_back({h[i][0]+1,l[i]});
//cout<<h[i][0]+1<<' '<<h[i][1]+1<<' '<<l[i]<<'\n';
}
dist[1]=0;
dfs(1,1);
for (ll i=1;i<=n;i++) {
//cout<<depth[i]<<' ';
}
//cout<<'\n';
for (ll i=1;i<=n;i++) {
//cout<<dist[i]<<' ';
}
//cout<<'\n';
solve(1,1);
if (res==oo) return -1;
else return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14444 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14368 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14444 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14368 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14336 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
11 ms |
14804 KB |
Output is correct |
22 |
Correct |
9 ms |
14932 KB |
Output is correct |
23 |
Correct |
9 ms |
14804 KB |
Output is correct |
24 |
Correct |
9 ms |
14804 KB |
Output is correct |
25 |
Correct |
8 ms |
14804 KB |
Output is correct |
26 |
Correct |
9 ms |
14804 KB |
Output is correct |
27 |
Correct |
8 ms |
14532 KB |
Output is correct |
28 |
Correct |
8 ms |
14808 KB |
Output is correct |
29 |
Correct |
9 ms |
14804 KB |
Output is correct |
30 |
Correct |
8 ms |
14804 KB |
Output is correct |
31 |
Correct |
9 ms |
14820 KB |
Output is correct |
32 |
Correct |
9 ms |
14808 KB |
Output is correct |
33 |
Correct |
9 ms |
14852 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
12 ms |
14708 KB |
Output is correct |
36 |
Correct |
9 ms |
14616 KB |
Output is correct |
37 |
Correct |
8 ms |
14624 KB |
Output is correct |
38 |
Correct |
8 ms |
14784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14444 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14368 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
131 ms |
42700 KB |
Output is correct |
20 |
Correct |
152 ms |
43984 KB |
Output is correct |
21 |
Correct |
138 ms |
44088 KB |
Output is correct |
22 |
Correct |
130 ms |
43632 KB |
Output is correct |
23 |
Correct |
216 ms |
71356 KB |
Output is correct |
24 |
Correct |
148 ms |
52116 KB |
Output is correct |
25 |
Correct |
83 ms |
33996 KB |
Output is correct |
26 |
Correct |
52 ms |
41132 KB |
Output is correct |
27 |
Correct |
195 ms |
53324 KB |
Output is correct |
28 |
Correct |
200 ms |
93388 KB |
Output is correct |
29 |
Correct |
203 ms |
92572 KB |
Output is correct |
30 |
Correct |
203 ms |
53296 KB |
Output is correct |
31 |
Correct |
229 ms |
53372 KB |
Output is correct |
32 |
Correct |
276 ms |
54544 KB |
Output is correct |
33 |
Correct |
210 ms |
52064 KB |
Output is correct |
34 |
Correct |
423 ms |
123980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14444 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14368 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14336 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
11 ms |
14804 KB |
Output is correct |
22 |
Correct |
9 ms |
14932 KB |
Output is correct |
23 |
Correct |
9 ms |
14804 KB |
Output is correct |
24 |
Correct |
9 ms |
14804 KB |
Output is correct |
25 |
Correct |
8 ms |
14804 KB |
Output is correct |
26 |
Correct |
9 ms |
14804 KB |
Output is correct |
27 |
Correct |
8 ms |
14532 KB |
Output is correct |
28 |
Correct |
8 ms |
14808 KB |
Output is correct |
29 |
Correct |
9 ms |
14804 KB |
Output is correct |
30 |
Correct |
8 ms |
14804 KB |
Output is correct |
31 |
Correct |
9 ms |
14820 KB |
Output is correct |
32 |
Correct |
9 ms |
14808 KB |
Output is correct |
33 |
Correct |
9 ms |
14852 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
12 ms |
14708 KB |
Output is correct |
36 |
Correct |
9 ms |
14616 KB |
Output is correct |
37 |
Correct |
8 ms |
14624 KB |
Output is correct |
38 |
Correct |
8 ms |
14784 KB |
Output is correct |
39 |
Correct |
131 ms |
42700 KB |
Output is correct |
40 |
Correct |
152 ms |
43984 KB |
Output is correct |
41 |
Correct |
138 ms |
44088 KB |
Output is correct |
42 |
Correct |
130 ms |
43632 KB |
Output is correct |
43 |
Correct |
216 ms |
71356 KB |
Output is correct |
44 |
Correct |
148 ms |
52116 KB |
Output is correct |
45 |
Correct |
83 ms |
33996 KB |
Output is correct |
46 |
Correct |
52 ms |
41132 KB |
Output is correct |
47 |
Correct |
195 ms |
53324 KB |
Output is correct |
48 |
Correct |
200 ms |
93388 KB |
Output is correct |
49 |
Correct |
203 ms |
92572 KB |
Output is correct |
50 |
Correct |
203 ms |
53296 KB |
Output is correct |
51 |
Correct |
229 ms |
53372 KB |
Output is correct |
52 |
Correct |
276 ms |
54544 KB |
Output is correct |
53 |
Correct |
210 ms |
52064 KB |
Output is correct |
54 |
Correct |
423 ms |
123980 KB |
Output is correct |
55 |
Correct |
24 ms |
19156 KB |
Output is correct |
56 |
Correct |
19 ms |
16884 KB |
Output is correct |
57 |
Correct |
98 ms |
43732 KB |
Output is correct |
58 |
Correct |
49 ms |
29964 KB |
Output is correct |
59 |
Correct |
100 ms |
53460 KB |
Output is correct |
60 |
Correct |
253 ms |
92192 KB |
Output is correct |
61 |
Correct |
262 ms |
61080 KB |
Output is correct |
62 |
Correct |
183 ms |
54652 KB |
Output is correct |
63 |
Correct |
233 ms |
54492 KB |
Output is correct |
64 |
Correct |
598 ms |
157792 KB |
Output is correct |
65 |
Correct |
595 ms |
155788 KB |
Output is correct |
66 |
Correct |
262 ms |
91244 KB |
Output is correct |
67 |
Correct |
226 ms |
47160 KB |
Output is correct |
68 |
Correct |
433 ms |
88324 KB |
Output is correct |
69 |
Correct |
431 ms |
88780 KB |
Output is correct |
70 |
Correct |
406 ms |
85020 KB |
Output is correct |