#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
/*------------------------------------code------------------------------------*/
const ll MAXN=3*1e5+9;
const ll INF=1e9+9;
const ll R=(1<<18);
const int X=18;
//const ll M=1e9+7;
//const ll P=47;
int n, k;
vector<pair<int, int>> adj[MAXN];
vector<int> sz(MAXN);
vector<int> done(MAXN);
int ans=INF, curr;
vector<int> dist[2000009];
vector<int> deg(MAXN);
vector<int> group(MAXN);
int dfs(int v, int p)
{
sz[v]=1;
for(auto [u, w] : adj[v])
{
if(u==p || done[u])
continue;
sz[v]+=dfs(u, v);
}
return sz[v];
}
int get(int v, int p, int mx)
{
for(auto [u, w] : adj[v])
{
if(u==p || done[u])
continue;
if(sz[u]>mx)
return get(u, v, mx);
}
return v;
}
void mark(int v, int p, ll d)
{
dist[d].pb(v);
group[v]=curr;
deg[v]=deg[p]+1;
for(auto [u, w] : adj[v])
{
if(u==p || done[u] || d+w>k || deg[v]>=ans)
continue;
mark(u, v, d+w);
}
}
void solve(int v, int p, ll d)
{
if(d==k)
{
ans=min(ans, deg[v]);
return;
}
for(int u : dist[k-d])
if(group[u]!=group[v])
ans=min(ans, deg[v]+deg[u]);
for(auto [u, w] : adj[v])
{
if(u==p || done[u] || d+w>k || deg[v]>=ans)
continue;
solve(u, v, d+w);
}
}
void cls(int v, int p, ll d)
{
group[v]=deg[v]=0;
dist[d].pop_back();
for(auto [u, w] : adj[v])
if(group[u])
cls(u, v, d+w);
}
void decompose(int v)
{
int c = get(v, -1, dfs(v, -1)/2);
done[c]=true;
deg[c]=0;
curr=1;
for(auto [u, w] : adj[c])
{
if(done[u] || w>k)
continue;
mark(u, c, w);
curr++;
}
for(auto [u, w] : adj[c])
{
if(done[u] || w>k)
continue;
solve(u, c, w);
}
for(auto [u, w] : adj[c])
if(!done[u] && w<=k)
cls(u, v, w);
for(auto [u, w] : adj[c])
if(!done[u])
decompose(u);
}
int best_path(int N, int K, int edges[][2], int weights[])
{
if(k==1)
return 0;
n=N;
k=K;
for(int i=0;i<n-1;i++)
{
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb({v, weights[i]});
adj[v].pb({u, weights[i]});
}
decompose(1);
return (ans==INF ? -1 : ans);
}
/*
int main()
{
cin.tie(0); ios_base::sync_with_stdio(0);
solve();
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58972 KB |
Output is correct |
2 |
Correct |
34 ms |
58960 KB |
Output is correct |
3 |
Correct |
25 ms |
58972 KB |
Output is correct |
4 |
Correct |
32 ms |
59092 KB |
Output is correct |
5 |
Correct |
27 ms |
58964 KB |
Output is correct |
6 |
Correct |
26 ms |
58964 KB |
Output is correct |
7 |
Correct |
24 ms |
58972 KB |
Output is correct |
8 |
Correct |
27 ms |
58972 KB |
Output is correct |
9 |
Correct |
25 ms |
59060 KB |
Output is correct |
10 |
Correct |
25 ms |
58972 KB |
Output is correct |
11 |
Correct |
27 ms |
58964 KB |
Output is correct |
12 |
Correct |
23 ms |
58972 KB |
Output is correct |
13 |
Correct |
26 ms |
58968 KB |
Output is correct |
14 |
Correct |
24 ms |
58968 KB |
Output is correct |
15 |
Correct |
25 ms |
58968 KB |
Output is correct |
16 |
Correct |
24 ms |
58972 KB |
Output is correct |
17 |
Correct |
25 ms |
58972 KB |
Output is correct |
18 |
Correct |
26 ms |
58960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58972 KB |
Output is correct |
2 |
Correct |
34 ms |
58960 KB |
Output is correct |
3 |
Correct |
25 ms |
58972 KB |
Output is correct |
4 |
Correct |
32 ms |
59092 KB |
Output is correct |
5 |
Correct |
27 ms |
58964 KB |
Output is correct |
6 |
Correct |
26 ms |
58964 KB |
Output is correct |
7 |
Correct |
24 ms |
58972 KB |
Output is correct |
8 |
Correct |
27 ms |
58972 KB |
Output is correct |
9 |
Correct |
25 ms |
59060 KB |
Output is correct |
10 |
Correct |
25 ms |
58972 KB |
Output is correct |
11 |
Correct |
27 ms |
58964 KB |
Output is correct |
12 |
Correct |
23 ms |
58972 KB |
Output is correct |
13 |
Correct |
26 ms |
58968 KB |
Output is correct |
14 |
Correct |
24 ms |
58968 KB |
Output is correct |
15 |
Correct |
25 ms |
58968 KB |
Output is correct |
16 |
Correct |
24 ms |
58972 KB |
Output is correct |
17 |
Correct |
25 ms |
58972 KB |
Output is correct |
18 |
Correct |
26 ms |
58960 KB |
Output is correct |
19 |
Correct |
24 ms |
58972 KB |
Output is correct |
20 |
Correct |
29 ms |
58972 KB |
Output is correct |
21 |
Correct |
25 ms |
59228 KB |
Output is correct |
22 |
Correct |
30 ms |
59228 KB |
Output is correct |
23 |
Correct |
26 ms |
59228 KB |
Output is correct |
24 |
Correct |
24 ms |
59224 KB |
Output is correct |
25 |
Correct |
25 ms |
59196 KB |
Output is correct |
26 |
Correct |
28 ms |
59108 KB |
Output is correct |
27 |
Correct |
27 ms |
58964 KB |
Output is correct |
28 |
Correct |
26 ms |
59224 KB |
Output is correct |
29 |
Correct |
28 ms |
59228 KB |
Output is correct |
30 |
Correct |
26 ms |
59232 KB |
Output is correct |
31 |
Correct |
30 ms |
59216 KB |
Output is correct |
32 |
Correct |
27 ms |
59216 KB |
Output is correct |
33 |
Correct |
26 ms |
59228 KB |
Output is correct |
34 |
Correct |
24 ms |
59216 KB |
Output is correct |
35 |
Correct |
26 ms |
59228 KB |
Output is correct |
36 |
Correct |
28 ms |
59188 KB |
Output is correct |
37 |
Correct |
27 ms |
59228 KB |
Output is correct |
38 |
Correct |
26 ms |
59172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58972 KB |
Output is correct |
2 |
Correct |
34 ms |
58960 KB |
Output is correct |
3 |
Correct |
25 ms |
58972 KB |
Output is correct |
4 |
Correct |
32 ms |
59092 KB |
Output is correct |
5 |
Correct |
27 ms |
58964 KB |
Output is correct |
6 |
Correct |
26 ms |
58964 KB |
Output is correct |
7 |
Correct |
24 ms |
58972 KB |
Output is correct |
8 |
Correct |
27 ms |
58972 KB |
Output is correct |
9 |
Correct |
25 ms |
59060 KB |
Output is correct |
10 |
Correct |
25 ms |
58972 KB |
Output is correct |
11 |
Correct |
27 ms |
58964 KB |
Output is correct |
12 |
Correct |
23 ms |
58972 KB |
Output is correct |
13 |
Correct |
26 ms |
58968 KB |
Output is correct |
14 |
Correct |
24 ms |
58968 KB |
Output is correct |
15 |
Correct |
25 ms |
58968 KB |
Output is correct |
16 |
Correct |
24 ms |
58972 KB |
Output is correct |
17 |
Correct |
25 ms |
58972 KB |
Output is correct |
18 |
Correct |
26 ms |
58960 KB |
Output is correct |
19 |
Correct |
109 ms |
63920 KB |
Output is correct |
20 |
Correct |
112 ms |
63880 KB |
Output is correct |
21 |
Correct |
123 ms |
64200 KB |
Output is correct |
22 |
Correct |
204 ms |
64596 KB |
Output is correct |
23 |
Correct |
78 ms |
64220 KB |
Output is correct |
24 |
Correct |
65 ms |
64084 KB |
Output is correct |
25 |
Correct |
173 ms |
68944 KB |
Output is correct |
26 |
Correct |
171 ms |
70564 KB |
Output is correct |
27 |
Correct |
126 ms |
68944 KB |
Output is correct |
28 |
Correct |
185 ms |
82460 KB |
Output is correct |
29 |
Correct |
205 ms |
78160 KB |
Output is correct |
30 |
Correct |
126 ms |
71760 KB |
Output is correct |
31 |
Correct |
120 ms |
71760 KB |
Output is correct |
32 |
Correct |
186 ms |
71992 KB |
Output is correct |
33 |
Correct |
185 ms |
70736 KB |
Output is correct |
34 |
Correct |
149 ms |
71504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58972 KB |
Output is correct |
2 |
Correct |
34 ms |
58960 KB |
Output is correct |
3 |
Correct |
25 ms |
58972 KB |
Output is correct |
4 |
Correct |
32 ms |
59092 KB |
Output is correct |
5 |
Correct |
27 ms |
58964 KB |
Output is correct |
6 |
Correct |
26 ms |
58964 KB |
Output is correct |
7 |
Correct |
24 ms |
58972 KB |
Output is correct |
8 |
Correct |
27 ms |
58972 KB |
Output is correct |
9 |
Correct |
25 ms |
59060 KB |
Output is correct |
10 |
Correct |
25 ms |
58972 KB |
Output is correct |
11 |
Correct |
27 ms |
58964 KB |
Output is correct |
12 |
Correct |
23 ms |
58972 KB |
Output is correct |
13 |
Correct |
26 ms |
58968 KB |
Output is correct |
14 |
Correct |
24 ms |
58968 KB |
Output is correct |
15 |
Correct |
25 ms |
58968 KB |
Output is correct |
16 |
Correct |
24 ms |
58972 KB |
Output is correct |
17 |
Correct |
25 ms |
58972 KB |
Output is correct |
18 |
Correct |
26 ms |
58960 KB |
Output is correct |
19 |
Correct |
24 ms |
58972 KB |
Output is correct |
20 |
Correct |
29 ms |
58972 KB |
Output is correct |
21 |
Correct |
25 ms |
59228 KB |
Output is correct |
22 |
Correct |
30 ms |
59228 KB |
Output is correct |
23 |
Correct |
26 ms |
59228 KB |
Output is correct |
24 |
Correct |
24 ms |
59224 KB |
Output is correct |
25 |
Correct |
25 ms |
59196 KB |
Output is correct |
26 |
Correct |
28 ms |
59108 KB |
Output is correct |
27 |
Correct |
27 ms |
58964 KB |
Output is correct |
28 |
Correct |
26 ms |
59224 KB |
Output is correct |
29 |
Correct |
28 ms |
59228 KB |
Output is correct |
30 |
Correct |
26 ms |
59232 KB |
Output is correct |
31 |
Correct |
30 ms |
59216 KB |
Output is correct |
32 |
Correct |
27 ms |
59216 KB |
Output is correct |
33 |
Correct |
26 ms |
59228 KB |
Output is correct |
34 |
Correct |
24 ms |
59216 KB |
Output is correct |
35 |
Correct |
26 ms |
59228 KB |
Output is correct |
36 |
Correct |
28 ms |
59188 KB |
Output is correct |
37 |
Correct |
27 ms |
59228 KB |
Output is correct |
38 |
Correct |
26 ms |
59172 KB |
Output is correct |
39 |
Correct |
109 ms |
63920 KB |
Output is correct |
40 |
Correct |
112 ms |
63880 KB |
Output is correct |
41 |
Correct |
123 ms |
64200 KB |
Output is correct |
42 |
Correct |
204 ms |
64596 KB |
Output is correct |
43 |
Correct |
78 ms |
64220 KB |
Output is correct |
44 |
Correct |
65 ms |
64084 KB |
Output is correct |
45 |
Correct |
173 ms |
68944 KB |
Output is correct |
46 |
Correct |
171 ms |
70564 KB |
Output is correct |
47 |
Correct |
126 ms |
68944 KB |
Output is correct |
48 |
Correct |
185 ms |
82460 KB |
Output is correct |
49 |
Correct |
205 ms |
78160 KB |
Output is correct |
50 |
Correct |
126 ms |
71760 KB |
Output is correct |
51 |
Correct |
120 ms |
71760 KB |
Output is correct |
52 |
Correct |
186 ms |
71992 KB |
Output is correct |
53 |
Correct |
185 ms |
70736 KB |
Output is correct |
54 |
Correct |
149 ms |
71504 KB |
Output is correct |
55 |
Correct |
40 ms |
59984 KB |
Output is correct |
56 |
Correct |
29 ms |
59808 KB |
Output is correct |
57 |
Correct |
732 ms |
66224 KB |
Output is correct |
58 |
Correct |
251 ms |
65732 KB |
Output is correct |
59 |
Correct |
144 ms |
73552 KB |
Output is correct |
60 |
Correct |
429 ms |
87472 KB |
Output is correct |
61 |
Correct |
155 ms |
72248 KB |
Output is correct |
62 |
Correct |
156 ms |
73256 KB |
Output is correct |
63 |
Correct |
215 ms |
73044 KB |
Output is correct |
64 |
Correct |
499 ms |
78632 KB |
Output is correct |
65 |
Correct |
149 ms |
72784 KB |
Output is correct |
66 |
Correct |
402 ms |
85680 KB |
Output is correct |
67 |
Correct |
125 ms |
73924 KB |
Output is correct |
68 |
Correct |
195 ms |
76628 KB |
Output is correct |
69 |
Correct |
217 ms |
76620 KB |
Output is correct |
70 |
Correct |
197 ms |
75860 KB |
Output is correct |