/*input
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
#include "race.h"
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define MP make_pair
const ll INF64=4e12;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1){
res=(res*a)%MOD;
}
a=(a*a)%MOD;
b>>=1;
}
return res;
}
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
ll pref[maxn];
int n,k;
int ans=INF;
int rt[maxn];
map<int,int> mp[maxn];
vector<pii> v[maxn];
pii bias[maxn];
void dfs(int u,int p){
rt[u]=u;
mp[u][0]=0;
for(auto xx:v[u]){
int vv=xx.f;
if(vv==p) continue;
dfs(vv,u);
int ru=rt[u],rv=rt[vv];
bias[rv].f+=xx.s;
bias[rv].s++;
if(sz(mp[ru])<sz(mp[rv])) swap(ru,rv);
for(auto x:mp[rv]){
if(mp[ru].count(k-x.f-bias[rv].f-bias[ru].f)){
MNTO(ans,mp[ru][k-x.f-bias[rv].f-bias[ru].f]+bias[ru].s+bias[rv].s+x.s);
}
}
for(auto x:mp[rv]){
if((!mp[ru].count(x.f+bias[rv].f-bias[ru].f) or mp[ru][x.f+bias[rv].f-bias[ru].f]>x.s+bias[rv].s-bias[ru].s)){
mp[ru][x.f+bias[rv].f-bias[ru].f]=x.s+bias[rv].s-bias[ru].s;
}
}
rt[u]=ru;
mp[rv].clear();
}
/*cout<<u<<' '<<bias[rt[u]].f<<' '<<bias[rt[u]].s<<'\n';
for(auto x:mp[rt[u]]) cout<<x.f<<' '<<x.s<<'\n';
cout<<'\n';*/
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
REP(i,n-1){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
dfs(0,-1);
if(ans>n) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
35584 KB |
Output is correct |
2 |
Correct |
22 ms |
35456 KB |
Output is correct |
3 |
Correct |
22 ms |
35584 KB |
Output is correct |
4 |
Correct |
23 ms |
35584 KB |
Output is correct |
5 |
Correct |
22 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35584 KB |
Output is correct |
7 |
Correct |
22 ms |
35584 KB |
Output is correct |
8 |
Correct |
23 ms |
35584 KB |
Output is correct |
9 |
Correct |
22 ms |
35584 KB |
Output is correct |
10 |
Correct |
22 ms |
35584 KB |
Output is correct |
11 |
Correct |
22 ms |
35584 KB |
Output is correct |
12 |
Correct |
22 ms |
35584 KB |
Output is correct |
13 |
Correct |
22 ms |
35584 KB |
Output is correct |
14 |
Correct |
22 ms |
35584 KB |
Output is correct |
15 |
Correct |
22 ms |
35584 KB |
Output is correct |
16 |
Correct |
25 ms |
35712 KB |
Output is correct |
17 |
Correct |
22 ms |
35584 KB |
Output is correct |
18 |
Correct |
22 ms |
35712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
35584 KB |
Output is correct |
2 |
Correct |
22 ms |
35456 KB |
Output is correct |
3 |
Correct |
22 ms |
35584 KB |
Output is correct |
4 |
Correct |
23 ms |
35584 KB |
Output is correct |
5 |
Correct |
22 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35584 KB |
Output is correct |
7 |
Correct |
22 ms |
35584 KB |
Output is correct |
8 |
Correct |
23 ms |
35584 KB |
Output is correct |
9 |
Correct |
22 ms |
35584 KB |
Output is correct |
10 |
Correct |
22 ms |
35584 KB |
Output is correct |
11 |
Correct |
22 ms |
35584 KB |
Output is correct |
12 |
Correct |
22 ms |
35584 KB |
Output is correct |
13 |
Correct |
22 ms |
35584 KB |
Output is correct |
14 |
Correct |
22 ms |
35584 KB |
Output is correct |
15 |
Correct |
22 ms |
35584 KB |
Output is correct |
16 |
Correct |
25 ms |
35712 KB |
Output is correct |
17 |
Correct |
22 ms |
35584 KB |
Output is correct |
18 |
Correct |
22 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35584 KB |
Output is correct |
20 |
Correct |
23 ms |
35584 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
24 ms |
35712 KB |
Output is correct |
24 |
Correct |
24 ms |
35712 KB |
Output is correct |
25 |
Correct |
23 ms |
35712 KB |
Output is correct |
26 |
Correct |
23 ms |
35712 KB |
Output is correct |
27 |
Correct |
23 ms |
35584 KB |
Output is correct |
28 |
Correct |
24 ms |
35712 KB |
Output is correct |
29 |
Correct |
23 ms |
35712 KB |
Output is correct |
30 |
Correct |
23 ms |
35712 KB |
Output is correct |
31 |
Correct |
22 ms |
35712 KB |
Output is correct |
32 |
Correct |
22 ms |
35712 KB |
Output is correct |
33 |
Correct |
22 ms |
35712 KB |
Output is correct |
34 |
Correct |
22 ms |
35704 KB |
Output is correct |
35 |
Correct |
22 ms |
35712 KB |
Output is correct |
36 |
Correct |
24 ms |
35840 KB |
Output is correct |
37 |
Correct |
24 ms |
35840 KB |
Output is correct |
38 |
Correct |
23 ms |
35840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
35584 KB |
Output is correct |
2 |
Correct |
22 ms |
35456 KB |
Output is correct |
3 |
Correct |
22 ms |
35584 KB |
Output is correct |
4 |
Correct |
23 ms |
35584 KB |
Output is correct |
5 |
Correct |
22 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35584 KB |
Output is correct |
7 |
Correct |
22 ms |
35584 KB |
Output is correct |
8 |
Correct |
23 ms |
35584 KB |
Output is correct |
9 |
Correct |
22 ms |
35584 KB |
Output is correct |
10 |
Correct |
22 ms |
35584 KB |
Output is correct |
11 |
Correct |
22 ms |
35584 KB |
Output is correct |
12 |
Correct |
22 ms |
35584 KB |
Output is correct |
13 |
Correct |
22 ms |
35584 KB |
Output is correct |
14 |
Correct |
22 ms |
35584 KB |
Output is correct |
15 |
Correct |
22 ms |
35584 KB |
Output is correct |
16 |
Correct |
25 ms |
35712 KB |
Output is correct |
17 |
Correct |
22 ms |
35584 KB |
Output is correct |
18 |
Correct |
22 ms |
35712 KB |
Output is correct |
19 |
Correct |
151 ms |
41848 KB |
Output is correct |
20 |
Correct |
149 ms |
43004 KB |
Output is correct |
21 |
Correct |
149 ms |
43000 KB |
Output is correct |
22 |
Correct |
157 ms |
43392 KB |
Output is correct |
23 |
Correct |
192 ms |
43512 KB |
Output is correct |
24 |
Correct |
153 ms |
43256 KB |
Output is correct |
25 |
Correct |
132 ms |
51832 KB |
Output is correct |
26 |
Correct |
90 ms |
60408 KB |
Output is correct |
27 |
Correct |
253 ms |
50936 KB |
Output is correct |
28 |
Correct |
326 ms |
85516 KB |
Output is correct |
29 |
Correct |
334 ms |
84220 KB |
Output is correct |
30 |
Correct |
249 ms |
51192 KB |
Output is correct |
31 |
Correct |
249 ms |
50936 KB |
Output is correct |
32 |
Correct |
297 ms |
51064 KB |
Output is correct |
33 |
Correct |
284 ms |
50168 KB |
Output is correct |
34 |
Correct |
424 ms |
60792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
35584 KB |
Output is correct |
2 |
Correct |
22 ms |
35456 KB |
Output is correct |
3 |
Correct |
22 ms |
35584 KB |
Output is correct |
4 |
Correct |
23 ms |
35584 KB |
Output is correct |
5 |
Correct |
22 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35584 KB |
Output is correct |
7 |
Correct |
22 ms |
35584 KB |
Output is correct |
8 |
Correct |
23 ms |
35584 KB |
Output is correct |
9 |
Correct |
22 ms |
35584 KB |
Output is correct |
10 |
Correct |
22 ms |
35584 KB |
Output is correct |
11 |
Correct |
22 ms |
35584 KB |
Output is correct |
12 |
Correct |
22 ms |
35584 KB |
Output is correct |
13 |
Correct |
22 ms |
35584 KB |
Output is correct |
14 |
Correct |
22 ms |
35584 KB |
Output is correct |
15 |
Correct |
22 ms |
35584 KB |
Output is correct |
16 |
Correct |
25 ms |
35712 KB |
Output is correct |
17 |
Correct |
22 ms |
35584 KB |
Output is correct |
18 |
Correct |
22 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35584 KB |
Output is correct |
20 |
Correct |
23 ms |
35584 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
24 ms |
35712 KB |
Output is correct |
24 |
Correct |
24 ms |
35712 KB |
Output is correct |
25 |
Correct |
23 ms |
35712 KB |
Output is correct |
26 |
Correct |
23 ms |
35712 KB |
Output is correct |
27 |
Correct |
23 ms |
35584 KB |
Output is correct |
28 |
Correct |
24 ms |
35712 KB |
Output is correct |
29 |
Correct |
23 ms |
35712 KB |
Output is correct |
30 |
Correct |
23 ms |
35712 KB |
Output is correct |
31 |
Correct |
22 ms |
35712 KB |
Output is correct |
32 |
Correct |
22 ms |
35712 KB |
Output is correct |
33 |
Correct |
22 ms |
35712 KB |
Output is correct |
34 |
Correct |
22 ms |
35704 KB |
Output is correct |
35 |
Correct |
22 ms |
35712 KB |
Output is correct |
36 |
Correct |
24 ms |
35840 KB |
Output is correct |
37 |
Correct |
24 ms |
35840 KB |
Output is correct |
38 |
Correct |
23 ms |
35840 KB |
Output is correct |
39 |
Correct |
151 ms |
41848 KB |
Output is correct |
40 |
Correct |
149 ms |
43004 KB |
Output is correct |
41 |
Correct |
149 ms |
43000 KB |
Output is correct |
42 |
Correct |
157 ms |
43392 KB |
Output is correct |
43 |
Correct |
192 ms |
43512 KB |
Output is correct |
44 |
Correct |
153 ms |
43256 KB |
Output is correct |
45 |
Correct |
132 ms |
51832 KB |
Output is correct |
46 |
Correct |
90 ms |
60408 KB |
Output is correct |
47 |
Correct |
253 ms |
50936 KB |
Output is correct |
48 |
Correct |
326 ms |
85516 KB |
Output is correct |
49 |
Correct |
334 ms |
84220 KB |
Output is correct |
50 |
Correct |
249 ms |
51192 KB |
Output is correct |
51 |
Correct |
249 ms |
50936 KB |
Output is correct |
52 |
Correct |
297 ms |
51064 KB |
Output is correct |
53 |
Correct |
284 ms |
50168 KB |
Output is correct |
54 |
Correct |
424 ms |
60792 KB |
Output is correct |
55 |
Correct |
38 ms |
36600 KB |
Output is correct |
56 |
Correct |
30 ms |
36344 KB |
Output is correct |
57 |
Correct |
97 ms |
43128 KB |
Output is correct |
58 |
Correct |
86 ms |
42860 KB |
Output is correct |
59 |
Correct |
117 ms |
60536 KB |
Output is correct |
60 |
Correct |
322 ms |
84600 KB |
Output is correct |
61 |
Correct |
285 ms |
52472 KB |
Output is correct |
62 |
Correct |
261 ms |
51060 KB |
Output is correct |
63 |
Correct |
299 ms |
51192 KB |
Output is correct |
64 |
Correct |
530 ms |
57720 KB |
Output is correct |
65 |
Correct |
533 ms |
61176 KB |
Output is correct |
66 |
Correct |
340 ms |
81272 KB |
Output is correct |
67 |
Correct |
225 ms |
51556 KB |
Output is correct |
68 |
Correct |
417 ms |
59640 KB |
Output is correct |
69 |
Correct |
434 ms |
62968 KB |
Output is correct |
70 |
Correct |
400 ms |
58616 KB |
Output is correct |