#include <bits/stdc++.h>
#define ll long long
#define db long double
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#include "race.h"
using namespace std;
map <ll,ll> mp,in4[200002];
vector <II> a[200002];
ll h[200002],w[200002],sz[200002],i,u,v,m,res;
const ll oo=round(1e18);
void dfs(int u,int p)
{
sz[u]=1;
for(II adj:a[u])
{
int v=adj.fst;
if(v!=p)
{
h[v]=h[u]+1,w[v]=w[u]+adj.snd;
dfs(v,u),sz[u]+=sz[v];
}
}
}
//wu+wv-2*wlca=m
//=>wv=m+2*wlca-wu
void cal(ll wu,ll hu,int lca)
{
// if(lca==6 && hu==2 && wu==6)
if(mp.count(m+2*w[lca]-wu))
res=min(res,hu+mp[m+2*w[lca]-wu]-2*h[lca]);
}
void add(ll w,ll h)
{
if(!mp.count(w)) mp[w]=h;
else mp[w]=min(mp[w],h);
}
void DFS(int u,int p)
{
int x=0;
for(II adj:a[u])
{
int v=adj.fst;
if(v!=p && sz[x]<sz[v]) x=v;
}
for(II adj:a[u])
{
int v=adj.fst;
if(v!=p && v!=x)
{
DFS(v,u);
in4[v]=mp; mp.clear();
}
}
if(x>0) DFS(x,u);
cal(w[u],h[u],u); add(w[u],h[u]);
for(II adj:a[u])
{
int v=adj.fst;
if(v==p || v==x) continue;
for(auto x:in4[v]) cal(x.fst,x.snd,u);
for(auto x:in4[v]) add(x.fst,x.snd);
}
}
int best_path(int n,int k,int H[][2],int L[])
{
m=k;
for(i=0;i<n-1;i++)
{
int u=H[i][0]+1,v=H[i][1]+1,k=L[i];
// cerr<<u<<" "<<v<<'\n';
a[u].push_back({ v,k });
a[v].push_back({ u,k });
}
res=oo;
dfs(1,0);
DFS(1,0);
return (res==oo) ? -1 : res;
}
/*
int main()
{
freopen("race.inp","r",stdin);
freopen("race.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
int H[n+1][2],L[n+1];
for(int i=0;i<n-1;i++) cin>>H[i][0]>>H[i][1]>>L[i];
cout<<best_path(n,m,H,L);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18768 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18764 KB |
Output is correct |
6 |
Correct |
4 ms |
18620 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
5 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18768 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18764 KB |
Output is correct |
6 |
Correct |
4 ms |
18620 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
5 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
5 ms |
18776 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
6 ms |
18780 KB |
Output is correct |
22 |
Correct |
5 ms |
19288 KB |
Output is correct |
23 |
Correct |
5 ms |
18872 KB |
Output is correct |
24 |
Correct |
5 ms |
19036 KB |
Output is correct |
25 |
Correct |
6 ms |
19032 KB |
Output is correct |
26 |
Correct |
5 ms |
19032 KB |
Output is correct |
27 |
Correct |
4 ms |
18780 KB |
Output is correct |
28 |
Correct |
5 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19032 KB |
Output is correct |
30 |
Correct |
7 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19028 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
18780 KB |
Output is correct |
35 |
Correct |
4 ms |
18780 KB |
Output is correct |
36 |
Correct |
4 ms |
18780 KB |
Output is correct |
37 |
Correct |
4 ms |
18780 KB |
Output is correct |
38 |
Correct |
5 ms |
18780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18768 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18764 KB |
Output is correct |
6 |
Correct |
4 ms |
18620 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
5 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
107 ms |
40684 KB |
Output is correct |
20 |
Correct |
104 ms |
40276 KB |
Output is correct |
21 |
Correct |
93 ms |
40516 KB |
Output is correct |
22 |
Correct |
92 ms |
40432 KB |
Output is correct |
23 |
Correct |
141 ms |
54292 KB |
Output is correct |
24 |
Correct |
106 ms |
46112 KB |
Output is correct |
25 |
Correct |
80 ms |
36176 KB |
Output is correct |
26 |
Correct |
43 ms |
41208 KB |
Output is correct |
27 |
Correct |
197 ms |
51472 KB |
Output is correct |
28 |
Correct |
234 ms |
74836 KB |
Output is correct |
29 |
Correct |
250 ms |
73956 KB |
Output is correct |
30 |
Correct |
159 ms |
51376 KB |
Output is correct |
31 |
Correct |
146 ms |
51404 KB |
Output is correct |
32 |
Correct |
223 ms |
51692 KB |
Output is correct |
33 |
Correct |
160 ms |
50256 KB |
Output is correct |
34 |
Correct |
292 ms |
83028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18780 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18768 KB |
Output is correct |
4 |
Correct |
4 ms |
18780 KB |
Output is correct |
5 |
Correct |
4 ms |
18764 KB |
Output is correct |
6 |
Correct |
4 ms |
18620 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
4 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
18780 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
5 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
4 ms |
18780 KB |
Output is correct |
15 |
Correct |
4 ms |
18780 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
5 ms |
18776 KB |
Output is correct |
20 |
Correct |
4 ms |
18780 KB |
Output is correct |
21 |
Correct |
6 ms |
18780 KB |
Output is correct |
22 |
Correct |
5 ms |
19288 KB |
Output is correct |
23 |
Correct |
5 ms |
18872 KB |
Output is correct |
24 |
Correct |
5 ms |
19036 KB |
Output is correct |
25 |
Correct |
6 ms |
19032 KB |
Output is correct |
26 |
Correct |
5 ms |
19032 KB |
Output is correct |
27 |
Correct |
4 ms |
18780 KB |
Output is correct |
28 |
Correct |
5 ms |
19036 KB |
Output is correct |
29 |
Correct |
5 ms |
19032 KB |
Output is correct |
30 |
Correct |
7 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
5 ms |
19028 KB |
Output is correct |
33 |
Correct |
5 ms |
19036 KB |
Output is correct |
34 |
Correct |
4 ms |
18780 KB |
Output is correct |
35 |
Correct |
4 ms |
18780 KB |
Output is correct |
36 |
Correct |
4 ms |
18780 KB |
Output is correct |
37 |
Correct |
4 ms |
18780 KB |
Output is correct |
38 |
Correct |
5 ms |
18780 KB |
Output is correct |
39 |
Correct |
107 ms |
40684 KB |
Output is correct |
40 |
Correct |
104 ms |
40276 KB |
Output is correct |
41 |
Correct |
93 ms |
40516 KB |
Output is correct |
42 |
Correct |
92 ms |
40432 KB |
Output is correct |
43 |
Correct |
141 ms |
54292 KB |
Output is correct |
44 |
Correct |
106 ms |
46112 KB |
Output is correct |
45 |
Correct |
80 ms |
36176 KB |
Output is correct |
46 |
Correct |
43 ms |
41208 KB |
Output is correct |
47 |
Correct |
197 ms |
51472 KB |
Output is correct |
48 |
Correct |
234 ms |
74836 KB |
Output is correct |
49 |
Correct |
250 ms |
73956 KB |
Output is correct |
50 |
Correct |
159 ms |
51376 KB |
Output is correct |
51 |
Correct |
146 ms |
51404 KB |
Output is correct |
52 |
Correct |
223 ms |
51692 KB |
Output is correct |
53 |
Correct |
160 ms |
50256 KB |
Output is correct |
54 |
Correct |
292 ms |
83028 KB |
Output is correct |
55 |
Correct |
16 ms |
21592 KB |
Output is correct |
56 |
Correct |
10 ms |
20312 KB |
Output is correct |
57 |
Correct |
61 ms |
39140 KB |
Output is correct |
58 |
Correct |
49 ms |
34756 KB |
Output is correct |
59 |
Correct |
75 ms |
47608 KB |
Output is correct |
60 |
Correct |
180 ms |
74068 KB |
Output is correct |
61 |
Correct |
205 ms |
54868 KB |
Output is correct |
62 |
Correct |
143 ms |
51536 KB |
Output is correct |
63 |
Correct |
183 ms |
51628 KB |
Output is correct |
64 |
Correct |
409 ms |
101812 KB |
Output is correct |
65 |
Correct |
398 ms |
102488 KB |
Output is correct |
66 |
Correct |
239 ms |
72016 KB |
Output is correct |
67 |
Correct |
177 ms |
51180 KB |
Output is correct |
68 |
Correct |
264 ms |
69600 KB |
Output is correct |
69 |
Correct |
271 ms |
69956 KB |
Output is correct |
70 |
Correct |
272 ms |
67524 KB |
Output is correct |