#include <bits/stdc++.h>
//#include "race.h"
//#include "grader.h"
using namespace std;
#define S second
#define F first
#define LL long long
const int N1 = 2e5+10;
const LL MOD = 1e9+7;
const LL LINF = 1e18;
const LL INF = 1e9;
map<LL,int> *mp[N1];
vector<pair<int,LL>>adj[N1];
LL depth[N1],dis[N1],sizes[N1],in[N1],out[N1],ans[N1];
vector<int>vec;
LL get_mn(LL x,int i){
if((*mp[i]).find(x)!=(*mp[i]).end())
return (*mp[i])[x];
else
return LINF;
}
void update(LL x,LL val,int i){
if((*mp[i]).find(x)!=(*mp[i]).end())
(*mp[i])[x] = min(1ll*(*mp[i])[x],val);
else
(*mp[i])[x] = val;
}
map<LL,int>mp1;
void dfs(int i,int pr,int k){
in[i] = vec.size();
vec.push_back(i);
mp[i] = new map<LL,int>;
sizes[i] = 1;
int heavy = -1;
for(auto x:adj[i]){
if(x.F==pr)continue;
depth[x.F] = depth[i]+1;
dis[x.F] = dis[i]+x.S;
dfs(x.F,i,k);
sizes[i] += sizes[x.F];
if(heavy==-1||sizes[x.F]>sizes[heavy])
heavy = x.F;
}
if(heavy==-1)heavy = i;
mp[i] = mp[heavy];
out[i] = vec.size();
vec.push_back(i);
ans[i] = get_mn(k+dis[i],i)-depth[i];
for(auto x:adj[i]){
int u = x.F;
if(u==pr||u==heavy)continue;
for(int j=in[u];j<=out[u];j++){
int v = vec[j];
ans[i] = min(ans[i],depth[v]+get_mn(k-dis[v]+dis[i]*2,i)-depth[i]*2);
if(dis[v]-dis[i]==k)
ans[i] = min(ans[i],depth[v]-depth[i]);
}
for(int j=in[u];j<=out[u];j++)
update(dis[vec[j]],depth[vec[j]],i);
}
update(dis[i],depth[i],i);
}
int best_path(int N, int K, int H[][2], int L[])
{
int n = N;
for(int i=0;i<n-1;i++){
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
dfs(0,0,K);
LL ans1 = LINF;
for(int i=0;i<n;i++)
ans1 = min(ans1,ans[i]);
return ans1<n?ans1:-1;
}
/*
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}
int main()
{
int ans1;
read_input();
ans1 = best_path(N,K,H,L);
if(ans1==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans1,solution);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
5 ms |
5324 KB |
Output is correct |
22 |
Correct |
6 ms |
5472 KB |
Output is correct |
23 |
Correct |
5 ms |
5452 KB |
Output is correct |
24 |
Correct |
5 ms |
5324 KB |
Output is correct |
25 |
Correct |
5 ms |
5324 KB |
Output is correct |
26 |
Correct |
5 ms |
5452 KB |
Output is correct |
27 |
Correct |
4 ms |
5324 KB |
Output is correct |
28 |
Correct |
5 ms |
5324 KB |
Output is correct |
29 |
Correct |
5 ms |
5444 KB |
Output is correct |
30 |
Correct |
6 ms |
5452 KB |
Output is correct |
31 |
Correct |
5 ms |
5452 KB |
Output is correct |
32 |
Correct |
5 ms |
5324 KB |
Output is correct |
33 |
Correct |
5 ms |
5452 KB |
Output is correct |
34 |
Correct |
6 ms |
5324 KB |
Output is correct |
35 |
Correct |
5 ms |
5268 KB |
Output is correct |
36 |
Correct |
5 ms |
5324 KB |
Output is correct |
37 |
Correct |
5 ms |
5324 KB |
Output is correct |
38 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
231 ms |
34928 KB |
Output is correct |
20 |
Correct |
230 ms |
34936 KB |
Output is correct |
21 |
Correct |
236 ms |
35024 KB |
Output is correct |
22 |
Correct |
227 ms |
34960 KB |
Output is correct |
23 |
Correct |
344 ms |
48680 KB |
Output is correct |
24 |
Correct |
233 ms |
40760 KB |
Output is correct |
25 |
Correct |
138 ms |
30608 KB |
Output is correct |
26 |
Correct |
69 ms |
37304 KB |
Output is correct |
27 |
Correct |
281 ms |
58976 KB |
Output is correct |
28 |
Correct |
329 ms |
82356 KB |
Output is correct |
29 |
Correct |
338 ms |
81360 KB |
Output is correct |
30 |
Correct |
274 ms |
58672 KB |
Output is correct |
31 |
Correct |
302 ms |
58692 KB |
Output is correct |
32 |
Correct |
404 ms |
58812 KB |
Output is correct |
33 |
Correct |
426 ms |
57476 KB |
Output is correct |
34 |
Correct |
631 ms |
90188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
5 ms |
5324 KB |
Output is correct |
22 |
Correct |
6 ms |
5472 KB |
Output is correct |
23 |
Correct |
5 ms |
5452 KB |
Output is correct |
24 |
Correct |
5 ms |
5324 KB |
Output is correct |
25 |
Correct |
5 ms |
5324 KB |
Output is correct |
26 |
Correct |
5 ms |
5452 KB |
Output is correct |
27 |
Correct |
4 ms |
5324 KB |
Output is correct |
28 |
Correct |
5 ms |
5324 KB |
Output is correct |
29 |
Correct |
5 ms |
5444 KB |
Output is correct |
30 |
Correct |
6 ms |
5452 KB |
Output is correct |
31 |
Correct |
5 ms |
5452 KB |
Output is correct |
32 |
Correct |
5 ms |
5324 KB |
Output is correct |
33 |
Correct |
5 ms |
5452 KB |
Output is correct |
34 |
Correct |
6 ms |
5324 KB |
Output is correct |
35 |
Correct |
5 ms |
5268 KB |
Output is correct |
36 |
Correct |
5 ms |
5324 KB |
Output is correct |
37 |
Correct |
5 ms |
5324 KB |
Output is correct |
38 |
Correct |
5 ms |
5324 KB |
Output is correct |
39 |
Correct |
231 ms |
34928 KB |
Output is correct |
40 |
Correct |
230 ms |
34936 KB |
Output is correct |
41 |
Correct |
236 ms |
35024 KB |
Output is correct |
42 |
Correct |
227 ms |
34960 KB |
Output is correct |
43 |
Correct |
344 ms |
48680 KB |
Output is correct |
44 |
Correct |
233 ms |
40760 KB |
Output is correct |
45 |
Correct |
138 ms |
30608 KB |
Output is correct |
46 |
Correct |
69 ms |
37304 KB |
Output is correct |
47 |
Correct |
281 ms |
58976 KB |
Output is correct |
48 |
Correct |
329 ms |
82356 KB |
Output is correct |
49 |
Correct |
338 ms |
81360 KB |
Output is correct |
50 |
Correct |
274 ms |
58672 KB |
Output is correct |
51 |
Correct |
302 ms |
58692 KB |
Output is correct |
52 |
Correct |
404 ms |
58812 KB |
Output is correct |
53 |
Correct |
426 ms |
57476 KB |
Output is correct |
54 |
Correct |
631 ms |
90188 KB |
Output is correct |
55 |
Correct |
26 ms |
9136 KB |
Output is correct |
56 |
Correct |
15 ms |
7764 KB |
Output is correct |
57 |
Correct |
148 ms |
35304 KB |
Output is correct |
58 |
Correct |
83 ms |
30816 KB |
Output is correct |
59 |
Correct |
99 ms |
43572 KB |
Output is correct |
60 |
Correct |
322 ms |
81456 KB |
Output is correct |
61 |
Correct |
381 ms |
62240 KB |
Output is correct |
62 |
Correct |
269 ms |
58648 KB |
Output is correct |
63 |
Correct |
353 ms |
58736 KB |
Output is correct |
64 |
Correct |
852 ms |
109168 KB |
Output is correct |
65 |
Correct |
815 ms |
109672 KB |
Output is correct |
66 |
Correct |
374 ms |
79456 KB |
Output is correct |
67 |
Correct |
263 ms |
57852 KB |
Output is correct |
68 |
Correct |
572 ms |
76660 KB |
Output is correct |
69 |
Correct |
535 ms |
77248 KB |
Output is correct |
70 |
Correct |
514 ms |
73356 KB |
Output is correct |