#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
#define N 200002
#define INF 1000000020
#define INFLL 1000000000000000020
#define fi first
#define se second
#define pb push_back
#define LOG 18
#define SQRT 25
typedef long int ll;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;
typedef vector< ll > vl;
typedef vector< pll > vll;
typedef tuple< int, int, int> tiii;
typedef tuple< ll, ll, ll> tlll;
vector<ll>*vet[N];
ll ans=N;
ll k;
ll sz[N];
ll lvl[N];
ll sum[N];
vector<ll>grafo[N];
map<pll,ll>qtd;
map<pll,ll> :: iterator it;
map<pll,ll>peso;
void ADD(ll val,ll nivel)
{
qtd[{val,nivel}]++;
return;
}
void REMOVE(ll val,ll nivel)
{
qtd[{val,nivel}]--;
if(!qtd[{val,nivel}])
{
qtd.erase({val,nivel});
}
return;
}
void build(ll u,ll p)
{
sz[u]=1;
ll w;
for(auto v : grafo[u])
{
if(v==p)
continue;
w=peso[{u,v}];
lvl[v]=lvl[u]+1LL;
sum[v]=sum[u]+w;
build(v,u);
sz[u]+=sz[v];
}
return;
}
void DFS(ll u,ll p,bool keep)
{
ll mx=-1,big;
for(auto v : grafo[u])
{
if(v==p)
continue;
if(mx<sz[v])
{
mx=sz[v];
big=v;
}
}
for(auto v : grafo[u])
{
if(v==p || v==big)
continue;
DFS(v,u,false);
}
if(mx!=-1)
{
DFS(big,u,true);
vet[u]=vet[big];
}else
{
vet[u]=new vector<ll>();
}
ll K=k+2*sum[u];
(*vet[u]).pb(u);
ADD(sum[u],lvl[u]);
for(auto v : grafo[u])
{
if(v==p || v==big)
continue;
for(auto x : (*vet[v]))
{
it=qtd.lower_bound({K-sum[x],0LL});
if(it!=qtd.end() &&
(*it).first.first==K-sum[x])
ans=min(ans,lvl[x]+
(*it).first.second-
2*lvl[u]);
}
for(auto x : (*vet[v]))
{
(*vet[u]).pb(x);
ADD(sum[x],lvl[x]);
}
}
it=qtd.lower_bound({k+sum[u],0LL});
if(it!=qtd.end() &&
(*it).first.first==k+sum[u])
ans=min(ans,(*it).first.second-lvl[u]);
if(!keep)
{
for(auto x : (*vet[u]))
{
REMOVE(sum[x],lvl[x]);
}
}
return;
}
int best_path(int n, int K, int H[][2], int L[])
{
ll i=0,u,v;
k=K;
while(i<n-1)
{
u=H[i][0];
v=H[i][1];
grafo[u].pb(v);
grafo[v].pb(u);
peso[{u,v}]=peso[{v,u}]=L[i];
i++;
}
lvl[0]=1;
build(0,0);
DFS(0,0,true);
if(ans>=n)
ans=-1;
return ans;
}
Compilation message
race.cpp: In function 'void DFS(ll, ll, bool)':
race.cpp:98:15: warning: 'big' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | if(v==p || v==big)
| ~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14780 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14804 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14796 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14852 KB |
Output is correct |
16 |
Correct |
5 ms |
14784 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14780 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14804 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14796 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14852 KB |
Output is correct |
16 |
Correct |
5 ms |
14784 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
5 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
5 ms |
14940 KB |
Output is correct |
26 |
Correct |
5 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14936 KB |
Output is correct |
28 |
Correct |
6 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
15028 KB |
Output is correct |
30 |
Correct |
6 ms |
14940 KB |
Output is correct |
31 |
Correct |
5 ms |
14936 KB |
Output is correct |
32 |
Correct |
5 ms |
14936 KB |
Output is correct |
33 |
Correct |
6 ms |
14940 KB |
Output is correct |
34 |
Correct |
4 ms |
15028 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
4 ms |
14936 KB |
Output is correct |
37 |
Correct |
4 ms |
14940 KB |
Output is correct |
38 |
Correct |
5 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14780 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14804 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14796 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14852 KB |
Output is correct |
16 |
Correct |
5 ms |
14784 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
247 ms |
42272 KB |
Output is correct |
20 |
Correct |
265 ms |
42060 KB |
Output is correct |
21 |
Correct |
237 ms |
42344 KB |
Output is correct |
22 |
Correct |
308 ms |
42188 KB |
Output is correct |
23 |
Correct |
346 ms |
44152 KB |
Output is correct |
24 |
Correct |
236 ms |
44316 KB |
Output is correct |
25 |
Correct |
204 ms |
50124 KB |
Output is correct |
26 |
Correct |
109 ms |
57928 KB |
Output is correct |
27 |
Correct |
422 ms |
64812 KB |
Output is correct |
28 |
Correct |
546 ms |
98948 KB |
Output is correct |
29 |
Correct |
589 ms |
96592 KB |
Output is correct |
30 |
Correct |
455 ms |
64868 KB |
Output is correct |
31 |
Correct |
432 ms |
64764 KB |
Output is correct |
32 |
Correct |
641 ms |
64708 KB |
Output is correct |
33 |
Correct |
525 ms |
58396 KB |
Output is correct |
34 |
Correct |
649 ms |
69856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14780 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14804 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14796 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14684 KB |
Output is correct |
15 |
Correct |
3 ms |
14852 KB |
Output is correct |
16 |
Correct |
5 ms |
14784 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
5 ms |
14940 KB |
Output is correct |
23 |
Correct |
5 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
5 ms |
14940 KB |
Output is correct |
26 |
Correct |
5 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14936 KB |
Output is correct |
28 |
Correct |
6 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
15028 KB |
Output is correct |
30 |
Correct |
6 ms |
14940 KB |
Output is correct |
31 |
Correct |
5 ms |
14936 KB |
Output is correct |
32 |
Correct |
5 ms |
14936 KB |
Output is correct |
33 |
Correct |
6 ms |
14940 KB |
Output is correct |
34 |
Correct |
4 ms |
15028 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
4 ms |
14936 KB |
Output is correct |
37 |
Correct |
4 ms |
14940 KB |
Output is correct |
38 |
Correct |
5 ms |
14936 KB |
Output is correct |
39 |
Correct |
247 ms |
42272 KB |
Output is correct |
40 |
Correct |
265 ms |
42060 KB |
Output is correct |
41 |
Correct |
237 ms |
42344 KB |
Output is correct |
42 |
Correct |
308 ms |
42188 KB |
Output is correct |
43 |
Correct |
346 ms |
44152 KB |
Output is correct |
44 |
Correct |
236 ms |
44316 KB |
Output is correct |
45 |
Correct |
204 ms |
50124 KB |
Output is correct |
46 |
Correct |
109 ms |
57928 KB |
Output is correct |
47 |
Correct |
422 ms |
64812 KB |
Output is correct |
48 |
Correct |
546 ms |
98948 KB |
Output is correct |
49 |
Correct |
589 ms |
96592 KB |
Output is correct |
50 |
Correct |
455 ms |
64868 KB |
Output is correct |
51 |
Correct |
432 ms |
64764 KB |
Output is correct |
52 |
Correct |
641 ms |
64708 KB |
Output is correct |
53 |
Correct |
525 ms |
58396 KB |
Output is correct |
54 |
Correct |
649 ms |
69856 KB |
Output is correct |
55 |
Correct |
22 ms |
17756 KB |
Output is correct |
56 |
Correct |
16 ms |
17244 KB |
Output is correct |
57 |
Correct |
171 ms |
44016 KB |
Output is correct |
58 |
Correct |
106 ms |
41872 KB |
Output is correct |
59 |
Correct |
113 ms |
57796 KB |
Output is correct |
60 |
Correct |
540 ms |
97140 KB |
Output is correct |
61 |
Correct |
494 ms |
68948 KB |
Output is correct |
62 |
Correct |
424 ms |
64708 KB |
Output is correct |
63 |
Correct |
611 ms |
64704 KB |
Output is correct |
64 |
Correct |
924 ms |
78416 KB |
Output is correct |
65 |
Correct |
907 ms |
79556 KB |
Output is correct |
66 |
Correct |
614 ms |
92288 KB |
Output is correct |
67 |
Correct |
516 ms |
69068 KB |
Output is correct |
68 |
Correct |
811 ms |
77248 KB |
Output is correct |
69 |
Correct |
834 ms |
77812 KB |
Output is correct |
70 |
Correct |
731 ms |
74452 KB |
Output is correct |