#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen(-1e9, 1e8); ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN = 2e5+5;
const int mod = 1e9+7;
const int mxlogN = 34;
const int mxK = 26;
//const ll inf = 1e18;
const int K = 100000;
struct edge
{
int u,v,w;
int other(int i)
{
return u^v^i;
}
};
edge es[mxN];
bool vis[mxN];
int cc[mxN];
vector<int> adj[mxN];
void init(int i, int p=-1)
{
cc[i]=1;
for(int e:adj[i])
{
int j=es[e].other(i);
if(p==j||vis[j]) continue;
init(j,i);
cc[i]+=cc[j];
}
}
int find_centroid(int i, int n, int p=-1)
{
for(int e:adj[i])
{
int j=es[e].other(i);
if(p==j||vis[j]) continue;
if(cc[j]>n/2) return find_centroid(j,n,i);
}
return i;
}
const int inf=2e6;
int k;
map<int, int> mp;
int solve2(int i, int r, int w, bool edit, int d=1, int p=-1)
{
if(w==k) return d;
if(w>k) return inf;
int ans=inf;
if(!edit)
{
if(mp.count(k-w)) ans=min(ans, d+mp[k-w]);
}
else
{
if(mp.count(w)) mp[w]=min(mp[w], d);
else mp[w]=d;
}
for(int e:adj[i])
{
int j=es[e].other(i);
if(p==j||vis[j]) continue;
ans=min(ans,solve2(j,r,w+es[e].w,edit,d+1,i));
}
return ans;
}
int solve(int i)
{
init(i);
i=find_centroid(i,cc[i]);
vis[i]=1;
int ans=inf;
for(int e:adj[i])
{
int j=es[e].other(i);
if(vis[j]) continue;
ans=min(ans,solve2(j,j,es[e].w,0));
solve2(j,j,es[e].w,1);
}
mp.clear();
for(int e:adj[i])
{
int j=es[e].other(i);
if(vis[j]) continue;
ans=min(ans,solve(j));
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i=0; i+1<N; i++)
{
int u=H[i][0], v=H[i][1], w=L[i];
es[i]={u,v,w};
adj[u].push_back(i);
adj[v].push_back(i);
}
int ans=solve(0);
if(ans==inf) ans=-1;
return ans;
}
/*
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
int H[n-1][2];
for(int i=0; i+1<n; i++) cin >> H[i][0] >> H[i][1];
int L[n-1];
for(int i=0; i+1<n; i++) cin >> L[i];
cout << best_path(n,k,H,L);
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5028 KB |
Output is correct |
23 |
Correct |
5 ms |
5136 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5160 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5028 KB |
Output is correct |
30 |
Correct |
4 ms |
5032 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5028 KB |
Output is correct |
33 |
Correct |
5 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
212 ms |
12272 KB |
Output is correct |
20 |
Correct |
212 ms |
12364 KB |
Output is correct |
21 |
Correct |
272 ms |
12400 KB |
Output is correct |
22 |
Correct |
241 ms |
12480 KB |
Output is correct |
23 |
Correct |
84 ms |
12320 KB |
Output is correct |
24 |
Correct |
72 ms |
12644 KB |
Output is correct |
25 |
Correct |
140 ms |
16448 KB |
Output is correct |
26 |
Correct |
107 ms |
20096 KB |
Output is correct |
27 |
Correct |
212 ms |
20404 KB |
Output is correct |
28 |
Correct |
278 ms |
35600 KB |
Output is correct |
29 |
Correct |
300 ms |
34476 KB |
Output is correct |
30 |
Correct |
214 ms |
20560 KB |
Output is correct |
31 |
Correct |
215 ms |
20576 KB |
Output is correct |
32 |
Correct |
242 ms |
20556 KB |
Output is correct |
33 |
Correct |
262 ms |
20104 KB |
Output is correct |
34 |
Correct |
211 ms |
20980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5028 KB |
Output is correct |
23 |
Correct |
5 ms |
5136 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5160 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5028 KB |
Output is correct |
30 |
Correct |
4 ms |
5032 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5028 KB |
Output is correct |
33 |
Correct |
5 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5032 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5028 KB |
Output is correct |
39 |
Correct |
212 ms |
12272 KB |
Output is correct |
40 |
Correct |
212 ms |
12364 KB |
Output is correct |
41 |
Correct |
272 ms |
12400 KB |
Output is correct |
42 |
Correct |
241 ms |
12480 KB |
Output is correct |
43 |
Correct |
84 ms |
12320 KB |
Output is correct |
44 |
Correct |
72 ms |
12644 KB |
Output is correct |
45 |
Correct |
140 ms |
16448 KB |
Output is correct |
46 |
Correct |
107 ms |
20096 KB |
Output is correct |
47 |
Correct |
212 ms |
20404 KB |
Output is correct |
48 |
Correct |
278 ms |
35600 KB |
Output is correct |
49 |
Correct |
300 ms |
34476 KB |
Output is correct |
50 |
Correct |
214 ms |
20560 KB |
Output is correct |
51 |
Correct |
215 ms |
20576 KB |
Output is correct |
52 |
Correct |
242 ms |
20556 KB |
Output is correct |
53 |
Correct |
262 ms |
20104 KB |
Output is correct |
54 |
Correct |
211 ms |
20980 KB |
Output is correct |
55 |
Correct |
21 ms |
5800 KB |
Output is correct |
56 |
Correct |
16 ms |
5744 KB |
Output is correct |
57 |
Correct |
142 ms |
12244 KB |
Output is correct |
58 |
Correct |
44 ms |
12332 KB |
Output is correct |
59 |
Correct |
303 ms |
23724 KB |
Output is correct |
60 |
Correct |
861 ms |
43328 KB |
Output is correct |
61 |
Correct |
323 ms |
20872 KB |
Output is correct |
62 |
Correct |
315 ms |
20728 KB |
Output is correct |
63 |
Correct |
429 ms |
20748 KB |
Output is correct |
64 |
Correct |
1124 ms |
25068 KB |
Output is correct |
65 |
Correct |
270 ms |
21020 KB |
Output is correct |
66 |
Correct |
527 ms |
32216 KB |
Output is correct |
67 |
Correct |
115 ms |
21056 KB |
Output is correct |
68 |
Correct |
446 ms |
24784 KB |
Output is correct |
69 |
Correct |
454 ms |
24836 KB |
Output is correct |
70 |
Correct |
433 ms |
23896 KB |
Output is correct |