#include <bits/stdc++.h>
#include <race.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
int n,k,rs=MAXN,sz[MAXN],cnt[1000005];
bool del[MAXN];
vector<pair<int,int>> adj[MAXN];
set<int> s;
void GetSize(int u, int par){
sz[u]=1;
for(pair<int,int> p : adj[u])
if(p.fi!=par&&!del[p.fi]) GetSize(p.fi,u), sz[u]+=sz[p.fi];
}
int Centroid(int u, int par){
for(pair<int,int> p : adj[u])
if(p.fi!=par&&!del[p.fi]&&sz[p.fi]>(n>>1)) return Centroid(p.fi,u);
return u;
}
void dfs(int u, int par, bool type, int cur, ll len){
if(k<len||cur>rs) return;
if(type==1) cnt[len] = !cnt[len] ? cur : min(cnt[len],cur), s.ins(len);
else if(type==0&&cnt[k-len]) rs=min(rs,cnt[k-len]+cur);
else if(len==k) rs=min(rs,cur);
for(pair<int,int> p : adj[u])
if(p.fi!=par&&!del[p.fi]) dfs(p.fi,u,type,cur+1,len+p.se);
}
void CentroidDecomposition(int u){
GetSize(u,0), n=sz[u];
int root=Centroid(u,0);
for(pair<int,int> p : adj[root])
if(!del[p.fi]) dfs(p.fi,root,0,1,p.se), dfs(p.fi,root,1,1,p.se);
for(int x : s) cnt[x]=0;
set<int>().swap(s);
del[root]=1;
for(pair<int,int> p : adj[root])
if(!del[p.fi]) CentroidDecomposition(p.fi);
}
int best_path(int N, int K, int h[][2], int l[]){
n=N, k=K;
for(int i = 1; i<n; ++i)
adj[h[i-1][0]+1].pb(h[i-1][1]+1,l[i-1]), adj[h[i-1][1]+1].pb(h[i-1][0]+1,l[i-1]);
CentroidDecomposition(1);
return rs==MAXN ? -1 : rs;
}
//int main()
//{
// tt;
// if(fopen((NAME + ".INP").c_str(), "r")) fo;
// cin >> n >> k;
// int h[n][2], l[n];
// for(int i = 0; i<n-1; ++i)
// cin >> h[i][0] >> h[i][1] >> l[i];
// cout << best_path(n,k,h,l);
// cerr << 1.0L*clock()/CLOCKS_PER_SEC << "s\n";
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
5160 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Correct |
4 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5156 KB |
Output is correct |
7 |
Correct |
4 ms |
4944 KB |
Output is correct |
8 |
Correct |
4 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
4 ms |
5004 KB |
Output is correct |
13 |
Correct |
4 ms |
5200 KB |
Output is correct |
14 |
Correct |
4 ms |
4944 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
5156 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
5160 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Correct |
4 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5156 KB |
Output is correct |
7 |
Correct |
4 ms |
4944 KB |
Output is correct |
8 |
Correct |
4 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
4 ms |
5004 KB |
Output is correct |
13 |
Correct |
4 ms |
5200 KB |
Output is correct |
14 |
Correct |
4 ms |
4944 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
5156 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
5 ms |
4944 KB |
Output is correct |
20 |
Correct |
4 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
5200 KB |
Output is correct |
22 |
Correct |
6 ms |
7504 KB |
Output is correct |
23 |
Correct |
6 ms |
7260 KB |
Output is correct |
24 |
Correct |
6 ms |
7976 KB |
Output is correct |
25 |
Correct |
6 ms |
6992 KB |
Output is correct |
26 |
Correct |
5 ms |
6448 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
6 ms |
5968 KB |
Output is correct |
29 |
Correct |
7 ms |
6480 KB |
Output is correct |
30 |
Correct |
8 ms |
6480 KB |
Output is correct |
31 |
Correct |
9 ms |
6992 KB |
Output is correct |
32 |
Correct |
8 ms |
6992 KB |
Output is correct |
33 |
Correct |
12 ms |
7760 KB |
Output is correct |
34 |
Correct |
7 ms |
7220 KB |
Output is correct |
35 |
Correct |
8 ms |
8016 KB |
Output is correct |
36 |
Correct |
8 ms |
8272 KB |
Output is correct |
37 |
Correct |
7 ms |
6992 KB |
Output is correct |
38 |
Correct |
6 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
5160 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Correct |
4 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5156 KB |
Output is correct |
7 |
Correct |
4 ms |
4944 KB |
Output is correct |
8 |
Correct |
4 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
4 ms |
5004 KB |
Output is correct |
13 |
Correct |
4 ms |
5200 KB |
Output is correct |
14 |
Correct |
4 ms |
4944 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
5156 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
120 ms |
11848 KB |
Output is correct |
20 |
Correct |
98 ms |
10320 KB |
Output is correct |
21 |
Correct |
125 ms |
11868 KB |
Output is correct |
22 |
Correct |
100 ms |
11848 KB |
Output is correct |
23 |
Correct |
57 ms |
12156 KB |
Output is correct |
24 |
Correct |
48 ms |
11860 KB |
Output is correct |
25 |
Correct |
108 ms |
15372 KB |
Output is correct |
26 |
Correct |
91 ms |
19184 KB |
Output is correct |
27 |
Correct |
127 ms |
18704 KB |
Output is correct |
28 |
Correct |
179 ms |
33352 KB |
Output is correct |
29 |
Correct |
187 ms |
32168 KB |
Output is correct |
30 |
Correct |
131 ms |
18760 KB |
Output is correct |
31 |
Correct |
119 ms |
18940 KB |
Output is correct |
32 |
Correct |
152 ms |
19016 KB |
Output is correct |
33 |
Correct |
198 ms |
17636 KB |
Output is correct |
34 |
Correct |
120 ms |
18504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
5160 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Correct |
4 ms |
4944 KB |
Output is correct |
6 |
Correct |
4 ms |
5156 KB |
Output is correct |
7 |
Correct |
4 ms |
4944 KB |
Output is correct |
8 |
Correct |
4 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
4 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
4 ms |
5004 KB |
Output is correct |
13 |
Correct |
4 ms |
5200 KB |
Output is correct |
14 |
Correct |
4 ms |
4944 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
5156 KB |
Output is correct |
17 |
Correct |
4 ms |
4944 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
5 ms |
4944 KB |
Output is correct |
20 |
Correct |
4 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
5200 KB |
Output is correct |
22 |
Correct |
6 ms |
7504 KB |
Output is correct |
23 |
Correct |
6 ms |
7260 KB |
Output is correct |
24 |
Correct |
6 ms |
7976 KB |
Output is correct |
25 |
Correct |
6 ms |
6992 KB |
Output is correct |
26 |
Correct |
5 ms |
6448 KB |
Output is correct |
27 |
Correct |
4 ms |
5200 KB |
Output is correct |
28 |
Correct |
6 ms |
5968 KB |
Output is correct |
29 |
Correct |
7 ms |
6480 KB |
Output is correct |
30 |
Correct |
8 ms |
6480 KB |
Output is correct |
31 |
Correct |
9 ms |
6992 KB |
Output is correct |
32 |
Correct |
8 ms |
6992 KB |
Output is correct |
33 |
Correct |
12 ms |
7760 KB |
Output is correct |
34 |
Correct |
7 ms |
7220 KB |
Output is correct |
35 |
Correct |
8 ms |
8016 KB |
Output is correct |
36 |
Correct |
8 ms |
8272 KB |
Output is correct |
37 |
Correct |
7 ms |
6992 KB |
Output is correct |
38 |
Correct |
6 ms |
6224 KB |
Output is correct |
39 |
Correct |
120 ms |
11848 KB |
Output is correct |
40 |
Correct |
98 ms |
10320 KB |
Output is correct |
41 |
Correct |
125 ms |
11868 KB |
Output is correct |
42 |
Correct |
100 ms |
11848 KB |
Output is correct |
43 |
Correct |
57 ms |
12156 KB |
Output is correct |
44 |
Correct |
48 ms |
11860 KB |
Output is correct |
45 |
Correct |
108 ms |
15372 KB |
Output is correct |
46 |
Correct |
91 ms |
19184 KB |
Output is correct |
47 |
Correct |
127 ms |
18704 KB |
Output is correct |
48 |
Correct |
179 ms |
33352 KB |
Output is correct |
49 |
Correct |
187 ms |
32168 KB |
Output is correct |
50 |
Correct |
131 ms |
18760 KB |
Output is correct |
51 |
Correct |
119 ms |
18940 KB |
Output is correct |
52 |
Correct |
152 ms |
19016 KB |
Output is correct |
53 |
Correct |
198 ms |
17636 KB |
Output is correct |
54 |
Correct |
120 ms |
18504 KB |
Output is correct |
55 |
Correct |
14 ms |
5968 KB |
Output is correct |
56 |
Correct |
14 ms |
5712 KB |
Output is correct |
57 |
Correct |
96 ms |
11864 KB |
Output is correct |
58 |
Correct |
34 ms |
11716 KB |
Output is correct |
59 |
Correct |
182 ms |
22820 KB |
Output is correct |
60 |
Correct |
526 ms |
43304 KB |
Output is correct |
61 |
Correct |
155 ms |
19272 KB |
Output is correct |
62 |
Correct |
200 ms |
19016 KB |
Output is correct |
63 |
Correct |
230 ms |
19140 KB |
Output is correct |
64 |
Correct |
597 ms |
24920 KB |
Output is correct |
65 |
Correct |
154 ms |
19784 KB |
Output is correct |
66 |
Correct |
410 ms |
33928 KB |
Output is correct |
67 |
Correct |
81 ms |
19640 KB |
Output is correct |
68 |
Correct |
131 ms |
22088 KB |
Output is correct |
69 |
Correct |
127 ms |
21580 KB |
Output is correct |
70 |
Correct |
129 ms |
21916 KB |
Output is correct |