#include "race.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define pii pair<int,int>
using namespace std;
const int MAXN = 2e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
int n,k;
long long rez = inf;
vector<vector<pii>> graf(MAXN);
int nmnodes[MAXN];
bool wasCentroid[MAXN];
map<long long,long long> sviputevi;
vector<pair<long long,long long>> trsumsonpath;
int dfs(int nod, int p)
{
nmnodes[nod] = 1;
for(auto x: graf[nod])
{
if(x.fi==p || wasCentroid[x.fi])continue;
nmnodes[nod] += dfs(x.fi,nod);
}
return nmnodes[nod];
}
void dfs2(int nod, int p, int d, long long s)
{
if(s>k)return;
trsumsonpath.pb({s,d});
for(auto x:graf[nod])
{
if(x.fi==p || wasCentroid[x.fi] || s+x.sc > k)continue;
dfs2(x.fi, nod, d+1, s+x.sc);
}
}
int findCentroid(int nod, int treesize, int p)
{
for(auto x: graf[nod])
{
if(x.fi == p || wasCentroid[x.fi])continue;
if(nmnodes[x.fi]*2 > treesize)return findCentroid(x.fi,treesize,nod);
}
return nod;
}
void solve(int nod)
{
int treesize = dfs(nod,nod);
int centroid = findCentroid(nod, treesize, nod);
wasCentroid[centroid] = 1;
sviputevi.clear();
for(auto x: graf[centroid])
{
if(wasCentroid[x.fi])continue;
trsumsonpath.clear();
dfs2(x.fi, x.fi, 1,x.sc);
//for(auto y: trsumsonpath)cout << y.fi << " " << y.sc;cout << endl;
for(auto y:trsumsonpath)
{
if(k == y.fi)rez= min(rez, y.sc);
else if(k-y.fi > 0 && sviputevi.count(k-y.fi))
{
rez = min(rez,y.sc + sviputevi[k-y.fi]);
}
}
for(auto y:trsumsonpath)
{
if(sviputevi.count(y.fi))sviputevi[y.fi] = min(sviputevi[y.fi],y.sc);
else sviputevi[y.fi] =y.sc;
}
//for(auto y:sviputevi)cout << y.fi << " " << y.sc << endl;
}
for(auto x: graf[centroid])
{
if(wasCentroid[x.fi])continue;
solve(x.fi);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
for(int i=0; i<n-1; i++)
{
graf[H[i][0]].pb(mp(H[i][1], L[i]));
graf[H[i][1]].pb(mp(H[i][0], L[i]));
}
solve(0);
if(rez == inf)return -1;
return rez;
}
/*
4 7
0 1 1
1 2 2
1 3 4
1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5156 KB |
Output is correct |
15 |
Correct |
2 ms |
5136 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5156 KB |
Output is correct |
15 |
Correct |
2 ms |
5136 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4952 KB |
Output is correct |
19 |
Correct |
2 ms |
5160 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5292 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5080 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
4 ms |
5276 KB |
Output is correct |
34 |
Correct |
3 ms |
5172 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5172 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5156 KB |
Output is correct |
15 |
Correct |
2 ms |
5136 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4952 KB |
Output is correct |
19 |
Correct |
118 ms |
12048 KB |
Output is correct |
20 |
Correct |
115 ms |
11964 KB |
Output is correct |
21 |
Correct |
117 ms |
12244 KB |
Output is correct |
22 |
Correct |
109 ms |
12504 KB |
Output is correct |
23 |
Correct |
48 ms |
12116 KB |
Output is correct |
24 |
Correct |
51 ms |
12064 KB |
Output is correct |
25 |
Correct |
100 ms |
14412 KB |
Output is correct |
26 |
Correct |
70 ms |
18380 KB |
Output is correct |
27 |
Correct |
113 ms |
18768 KB |
Output is correct |
28 |
Correct |
151 ms |
30036 KB |
Output is correct |
29 |
Correct |
161 ms |
29264 KB |
Output is correct |
30 |
Correct |
110 ms |
18876 KB |
Output is correct |
31 |
Correct |
116 ms |
18732 KB |
Output is correct |
32 |
Correct |
121 ms |
18792 KB |
Output is correct |
33 |
Correct |
175 ms |
17748 KB |
Output is correct |
34 |
Correct |
106 ms |
18612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5156 KB |
Output is correct |
15 |
Correct |
2 ms |
5136 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4952 KB |
Output is correct |
19 |
Correct |
2 ms |
5160 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5292 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
3 ms |
5080 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
4 ms |
5276 KB |
Output is correct |
34 |
Correct |
3 ms |
5172 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5172 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5208 KB |
Output is correct |
39 |
Correct |
118 ms |
12048 KB |
Output is correct |
40 |
Correct |
115 ms |
11964 KB |
Output is correct |
41 |
Correct |
117 ms |
12244 KB |
Output is correct |
42 |
Correct |
109 ms |
12504 KB |
Output is correct |
43 |
Correct |
48 ms |
12116 KB |
Output is correct |
44 |
Correct |
51 ms |
12064 KB |
Output is correct |
45 |
Correct |
100 ms |
14412 KB |
Output is correct |
46 |
Correct |
70 ms |
18380 KB |
Output is correct |
47 |
Correct |
113 ms |
18768 KB |
Output is correct |
48 |
Correct |
151 ms |
30036 KB |
Output is correct |
49 |
Correct |
161 ms |
29264 KB |
Output is correct |
50 |
Correct |
110 ms |
18876 KB |
Output is correct |
51 |
Correct |
116 ms |
18732 KB |
Output is correct |
52 |
Correct |
121 ms |
18792 KB |
Output is correct |
53 |
Correct |
175 ms |
17748 KB |
Output is correct |
54 |
Correct |
106 ms |
18612 KB |
Output is correct |
55 |
Correct |
14 ms |
5976 KB |
Output is correct |
56 |
Correct |
10 ms |
5824 KB |
Output is correct |
57 |
Correct |
87 ms |
12976 KB |
Output is correct |
58 |
Correct |
29 ms |
11716 KB |
Output is correct |
59 |
Correct |
205 ms |
22940 KB |
Output is correct |
60 |
Correct |
571 ms |
42432 KB |
Output is correct |
61 |
Correct |
156 ms |
19284 KB |
Output is correct |
62 |
Correct |
169 ms |
19028 KB |
Output is correct |
63 |
Correct |
200 ms |
19244 KB |
Output is correct |
64 |
Correct |
561 ms |
26736 KB |
Output is correct |
65 |
Correct |
119 ms |
19608 KB |
Output is correct |
66 |
Correct |
364 ms |
27728 KB |
Output is correct |
67 |
Correct |
80 ms |
19744 KB |
Output is correct |
68 |
Correct |
262 ms |
24468 KB |
Output is correct |
69 |
Correct |
275 ms |
24476 KB |
Output is correct |
70 |
Correct |
238 ms |
23788 KB |
Output is correct |