#include "race.h"
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define endl '\n'
#define ff first
#define ss second
// #define int long long
#define MOD 1000000007
int n,k;
int ans=1e9;
map<int,int> mp;
const int mxN=2e5+1;
const int mxM=1e6+1;
vector<bool> vis(mxN);
vector<pair<int,int> > v[mxN];
vector<int> sub(mxN),cnt(mxM);
void cal(int a, int p, int d, int w){
if(mp.count(k-w)) ans=min(ans,d+mp[k-w]);
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
cal(node.ff,a,d+1,w+node.ss);
}
}
}
void up(int a, int p, int d, int w){
if(w<=k){
if(mp.count(w)) mp[w]=min(mp[w],d);
else mp[w]=d;
}else{
return;
}
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
up(node.ff,a,d+1,w+node.ss);
}
}
}
int dfs(int a, int p){
sub[a]=1;
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
sub[a]+=dfs(node.ff,a);
}
}
return sub[a];
}
int find(int a, int p, int s){
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]&&sub[node.ff]>s/2){
return find(node.ff,a,s);
}
}
return a;
}
void build(int a, int p){
int cd=find(a,p,dfs(a,p));
vis[cd]=1;
mp[0]=0;
for(auto node: v[cd]){
if(!vis[node.ff]){
cal(node.ff,cd,1,node.ss);
up(node.ff,cd,1,node.ss);
}
}
for(auto x: mp) cnt[x.ff]=1e9;
mp.clear();
for(auto node: v[cd]){
if(!vis[node.ff]) build(node.ff,cd);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
for(int i=0; i<n-1; i++){
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
build(1,-1);
if(ans==1e9) return -1;
return ans;
}
// signed main(){
// // freopen("file.in", "r", stdin);
// // freopen("file.out", "w", stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
// int T=1;
// // cin>>T;
// while(T--){
// int N,K; cin>>N>>K;
// int H[N][2],L[N];
// for(int i=0; i<N-1; i++) cin>>H[i][0]>>H[i][1];
// for(int i=0; i<N-1; i++) cin>>L[i];
// cout<<best_path(N,K,H,L);
// }
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13404 KB |
Output is correct |
3 |
Correct |
4 ms |
13404 KB |
Output is correct |
4 |
Correct |
4 ms |
13404 KB |
Output is correct |
5 |
Correct |
4 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13460 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
4 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13404 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13408 KB |
Output is correct |
18 |
Correct |
3 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13404 KB |
Output is correct |
3 |
Correct |
4 ms |
13404 KB |
Output is correct |
4 |
Correct |
4 ms |
13404 KB |
Output is correct |
5 |
Correct |
4 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13460 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
4 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13404 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13408 KB |
Output is correct |
18 |
Correct |
3 ms |
13404 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13404 KB |
Output is correct |
23 |
Correct |
4 ms |
13404 KB |
Output is correct |
24 |
Correct |
4 ms |
13532 KB |
Output is correct |
25 |
Correct |
4 ms |
13404 KB |
Output is correct |
26 |
Correct |
4 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13480 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
4 ms |
13404 KB |
Output is correct |
32 |
Correct |
5 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
4 ms |
13400 KB |
Output is correct |
35 |
Correct |
5 ms |
13404 KB |
Output is correct |
36 |
Correct |
5 ms |
13400 KB |
Output is correct |
37 |
Correct |
4 ms |
13404 KB |
Output is correct |
38 |
Correct |
4 ms |
13400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13404 KB |
Output is correct |
3 |
Correct |
4 ms |
13404 KB |
Output is correct |
4 |
Correct |
4 ms |
13404 KB |
Output is correct |
5 |
Correct |
4 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13460 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
4 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13404 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13408 KB |
Output is correct |
18 |
Correct |
3 ms |
13404 KB |
Output is correct |
19 |
Correct |
154 ms |
19296 KB |
Output is correct |
20 |
Correct |
144 ms |
19288 KB |
Output is correct |
21 |
Correct |
150 ms |
19288 KB |
Output is correct |
22 |
Correct |
144 ms |
19464 KB |
Output is correct |
23 |
Correct |
76 ms |
19628 KB |
Output is correct |
24 |
Correct |
65 ms |
19292 KB |
Output is correct |
25 |
Correct |
118 ms |
25912 KB |
Output is correct |
26 |
Correct |
83 ms |
26680 KB |
Output is correct |
27 |
Correct |
148 ms |
23136 KB |
Output is correct |
28 |
Correct |
229 ms |
36524 KB |
Output is correct |
29 |
Correct |
245 ms |
31316 KB |
Output is correct |
30 |
Correct |
150 ms |
23124 KB |
Output is correct |
31 |
Correct |
148 ms |
23124 KB |
Output is correct |
32 |
Correct |
199 ms |
23096 KB |
Output is correct |
33 |
Correct |
223 ms |
22096 KB |
Output is correct |
34 |
Correct |
143 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13404 KB |
Output is correct |
3 |
Correct |
4 ms |
13404 KB |
Output is correct |
4 |
Correct |
4 ms |
13404 KB |
Output is correct |
5 |
Correct |
4 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13460 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
4 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13404 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13408 KB |
Output is correct |
18 |
Correct |
3 ms |
13404 KB |
Output is correct |
19 |
Correct |
4 ms |
13404 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13404 KB |
Output is correct |
22 |
Correct |
4 ms |
13404 KB |
Output is correct |
23 |
Correct |
4 ms |
13404 KB |
Output is correct |
24 |
Correct |
4 ms |
13532 KB |
Output is correct |
25 |
Correct |
4 ms |
13404 KB |
Output is correct |
26 |
Correct |
4 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13480 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
4 ms |
13404 KB |
Output is correct |
32 |
Correct |
5 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
4 ms |
13400 KB |
Output is correct |
35 |
Correct |
5 ms |
13404 KB |
Output is correct |
36 |
Correct |
5 ms |
13400 KB |
Output is correct |
37 |
Correct |
4 ms |
13404 KB |
Output is correct |
38 |
Correct |
4 ms |
13400 KB |
Output is correct |
39 |
Correct |
154 ms |
19296 KB |
Output is correct |
40 |
Correct |
144 ms |
19288 KB |
Output is correct |
41 |
Correct |
150 ms |
19288 KB |
Output is correct |
42 |
Correct |
144 ms |
19464 KB |
Output is correct |
43 |
Correct |
76 ms |
19628 KB |
Output is correct |
44 |
Correct |
65 ms |
19292 KB |
Output is correct |
45 |
Correct |
118 ms |
25912 KB |
Output is correct |
46 |
Correct |
83 ms |
26680 KB |
Output is correct |
47 |
Correct |
148 ms |
23136 KB |
Output is correct |
48 |
Correct |
229 ms |
36524 KB |
Output is correct |
49 |
Correct |
245 ms |
31316 KB |
Output is correct |
50 |
Correct |
150 ms |
23124 KB |
Output is correct |
51 |
Correct |
148 ms |
23124 KB |
Output is correct |
52 |
Correct |
199 ms |
23096 KB |
Output is correct |
53 |
Correct |
223 ms |
22096 KB |
Output is correct |
54 |
Correct |
143 ms |
22100 KB |
Output is correct |
55 |
Correct |
17 ms |
14168 KB |
Output is correct |
56 |
Correct |
13 ms |
13928 KB |
Output is correct |
57 |
Correct |
113 ms |
19596 KB |
Output is correct |
58 |
Correct |
41 ms |
20484 KB |
Output is correct |
59 |
Correct |
250 ms |
30972 KB |
Output is correct |
60 |
Correct |
641 ms |
41844 KB |
Output is correct |
61 |
Correct |
212 ms |
24996 KB |
Output is correct |
62 |
Correct |
220 ms |
25428 KB |
Output is correct |
63 |
Correct |
265 ms |
25520 KB |
Output is correct |
64 |
Correct |
684 ms |
28500 KB |
Output is correct |
65 |
Correct |
158 ms |
26376 KB |
Output is correct |
66 |
Correct |
469 ms |
35464 KB |
Output is correct |
67 |
Correct |
104 ms |
27116 KB |
Output is correct |
68 |
Correct |
304 ms |
29208 KB |
Output is correct |
69 |
Correct |
291 ms |
27216 KB |
Output is correct |
70 |
Correct |
304 ms |
29220 KB |
Output is correct |