#include "race.h"
#include<vector>
#include<unordered_map>
#include<bitset>
#include<algorithm>
#define fi first
#define se second
using namespace std;
using pii = pair<int,int>;
using ll = long long;
constexpr int NMAX = 2e5 + 1;
vector<pii> fii[NMAX]; bitset<NMAX> scos;
int sz[NMAX],n,k,mx,ans;
void build(int r,int p = -1)
{
sz[r] = 1;
for(auto &it : fii[r])
if(it.first != p && !scos[it.first])
build(it.first,r),sz[r] += sz[it.first];
}
int centroid(int r,int n,int p = -1)
{
for(auto &it : fii[r])
if(it.first != p && !scos[it.first] && sz[it.first] * 2 > n)
return centroid(it.first,n,r);
return r;
}
void dfs(int a,ll cost,int d,int p,bool add,unordered_map<int,int> &m,unordered_map<int,bool> &avem)
{
if(cost > k) return;
if(add)
{
bool &u = avem[cost];
if(!u) u = 1 , m[cost] = d;
else
{
int &h = m[cost];
h = min(h,d);
}
}
else
{
bool &u = avem[k-cost];
if(u) ans = min(ans,m[k-cost]+d);
}
for(auto &it : fii[a])
if(!scos[it.fi] && it.fi != p) dfs(it.fi,cost+it.se,d+1,a,add,m,avem);
mx = max(mx,d);
}
void descompune(int root)
{
build(root); int c = centroid(root,sz[root]);
unordered_map<int,int> nou; unordered_map<int,bool> avem; avem[0] = 1;
for(auto &it : fii[c])
if(!scos[it.fi])
dfs(it.fi,it.se,1,c,0,nou,avem),
dfs(it.fi,it.se,1,c,1,nou,avem);
mx = 0; scos[c] = 1;
for(auto &it : fii[c])
if(!scos[it.fi]) descompune(it.fi);
}
int best_path(int N,int K,int H[][2],int L[])
{
n = N,k = K; ans = n + 1;
for(int i = 0 ; i < n - 1; i ++)
{
fii[H[i][0]].push_back({H[i][1],L[i]});
fii[H[i][1]].push_back({H[i][0],L[i]});
}
descompune(0); if(ans == n + 1) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8612 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
2 ms |
8732 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8612 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
2 ms |
8732 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8536 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8968 KB |
Output is correct |
26 |
Correct |
3 ms |
8612 KB |
Output is correct |
27 |
Correct |
2 ms |
8724 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
8792 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8612 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
2 ms |
8732 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
151 ms |
12840 KB |
Output is correct |
20 |
Correct |
136 ms |
12840 KB |
Output is correct |
21 |
Correct |
160 ms |
12772 KB |
Output is correct |
22 |
Correct |
131 ms |
12916 KB |
Output is correct |
23 |
Correct |
83 ms |
12892 KB |
Output is correct |
24 |
Correct |
69 ms |
12756 KB |
Output is correct |
25 |
Correct |
117 ms |
15600 KB |
Output is correct |
26 |
Correct |
111 ms |
19860 KB |
Output is correct |
27 |
Correct |
186 ms |
22092 KB |
Output is correct |
28 |
Correct |
207 ms |
33356 KB |
Output is correct |
29 |
Correct |
202 ms |
32340 KB |
Output is correct |
30 |
Correct |
186 ms |
22104 KB |
Output is correct |
31 |
Correct |
188 ms |
21844 KB |
Output is correct |
32 |
Correct |
183 ms |
22148 KB |
Output is correct |
33 |
Correct |
239 ms |
21072 KB |
Output is correct |
34 |
Correct |
123 ms |
21780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8612 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8536 KB |
Output is correct |
17 |
Correct |
2 ms |
8732 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8536 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8968 KB |
Output is correct |
26 |
Correct |
3 ms |
8612 KB |
Output is correct |
27 |
Correct |
2 ms |
8724 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
8792 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
151 ms |
12840 KB |
Output is correct |
40 |
Correct |
136 ms |
12840 KB |
Output is correct |
41 |
Correct |
160 ms |
12772 KB |
Output is correct |
42 |
Correct |
131 ms |
12916 KB |
Output is correct |
43 |
Correct |
83 ms |
12892 KB |
Output is correct |
44 |
Correct |
69 ms |
12756 KB |
Output is correct |
45 |
Correct |
117 ms |
15600 KB |
Output is correct |
46 |
Correct |
111 ms |
19860 KB |
Output is correct |
47 |
Correct |
186 ms |
22092 KB |
Output is correct |
48 |
Correct |
207 ms |
33356 KB |
Output is correct |
49 |
Correct |
202 ms |
32340 KB |
Output is correct |
50 |
Correct |
186 ms |
22104 KB |
Output is correct |
51 |
Correct |
188 ms |
21844 KB |
Output is correct |
52 |
Correct |
183 ms |
22148 KB |
Output is correct |
53 |
Correct |
239 ms |
21072 KB |
Output is correct |
54 |
Correct |
123 ms |
21780 KB |
Output is correct |
55 |
Correct |
18 ms |
10076 KB |
Output is correct |
56 |
Correct |
12 ms |
9052 KB |
Output is correct |
57 |
Correct |
116 ms |
14416 KB |
Output is correct |
58 |
Correct |
40 ms |
14100 KB |
Output is correct |
59 |
Correct |
308 ms |
37572 KB |
Output is correct |
60 |
Correct |
1038 ms |
80244 KB |
Output is correct |
61 |
Correct |
275 ms |
22708 KB |
Output is correct |
62 |
Correct |
283 ms |
22628 KB |
Output is correct |
63 |
Correct |
299 ms |
22756 KB |
Output is correct |
64 |
Correct |
747 ms |
52604 KB |
Output is correct |
65 |
Correct |
140 ms |
22840 KB |
Output is correct |
66 |
Correct |
653 ms |
34304 KB |
Output is correct |
67 |
Correct |
96 ms |
22728 KB |
Output is correct |
68 |
Correct |
362 ms |
40708 KB |
Output is correct |
69 |
Correct |
369 ms |
40900 KB |
Output is correct |
70 |
Correct |
335 ms |
39840 KB |
Output is correct |