/*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];
unordered_map<int,int> mp[maxn];
vector<pii> v[maxn];
pii off[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];
off[rv].f+=xx.s;
off[rv].s++;
if(sz(mp[ru])<sz(mp[rv])) swap(ru,rv);
for(auto x:mp[rv]){
if(mp[ru].count(k-x.f-off[rv].f-off[ru].f)){
MNTO(ans,mp[ru][k-x.f-off[rv].f-off[ru].f]+off[ru].s+off[rv].s+x.s);
}
}
for(auto x:mp[rv]){
if((!mp[ru].count(x.f+off[rv].f-off[ru].f) or mp[ru][x.f+off[rv].f-off[ru].f]>x.s+off[rv].s-off[ru].s)){
mp[ru][x.f+off[rv].f-off[ru].f]=x.s+off[rv].s-off[ru].s;
}
}
rt[u]=ru;
mp[rv].clear();
}
/*cout<<u<<' '<<off[rt[u]].f<<' '<<off[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 |
26 ms |
39552 KB |
Output is correct |
2 |
Correct |
27 ms |
39552 KB |
Output is correct |
3 |
Correct |
27 ms |
39680 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
27 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39552 KB |
Output is correct |
7 |
Correct |
27 ms |
39552 KB |
Output is correct |
8 |
Correct |
27 ms |
39552 KB |
Output is correct |
9 |
Correct |
26 ms |
39552 KB |
Output is correct |
10 |
Correct |
26 ms |
39552 KB |
Output is correct |
11 |
Correct |
27 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39544 KB |
Output is correct |
13 |
Correct |
27 ms |
39552 KB |
Output is correct |
14 |
Correct |
26 ms |
39552 KB |
Output is correct |
15 |
Correct |
27 ms |
39552 KB |
Output is correct |
16 |
Correct |
26 ms |
39552 KB |
Output is correct |
17 |
Correct |
27 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39552 KB |
Output is correct |
2 |
Correct |
27 ms |
39552 KB |
Output is correct |
3 |
Correct |
27 ms |
39680 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
27 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39552 KB |
Output is correct |
7 |
Correct |
27 ms |
39552 KB |
Output is correct |
8 |
Correct |
27 ms |
39552 KB |
Output is correct |
9 |
Correct |
26 ms |
39552 KB |
Output is correct |
10 |
Correct |
26 ms |
39552 KB |
Output is correct |
11 |
Correct |
27 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39544 KB |
Output is correct |
13 |
Correct |
27 ms |
39552 KB |
Output is correct |
14 |
Correct |
26 ms |
39552 KB |
Output is correct |
15 |
Correct |
27 ms |
39552 KB |
Output is correct |
16 |
Correct |
26 ms |
39552 KB |
Output is correct |
17 |
Correct |
27 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39424 KB |
Output is correct |
20 |
Correct |
27 ms |
39552 KB |
Output is correct |
21 |
Correct |
28 ms |
39552 KB |
Output is correct |
22 |
Correct |
28 ms |
39680 KB |
Output is correct |
23 |
Correct |
28 ms |
39800 KB |
Output is correct |
24 |
Correct |
28 ms |
39680 KB |
Output is correct |
25 |
Correct |
28 ms |
39680 KB |
Output is correct |
26 |
Correct |
29 ms |
39680 KB |
Output is correct |
27 |
Correct |
28 ms |
39680 KB |
Output is correct |
28 |
Correct |
28 ms |
39680 KB |
Output is correct |
29 |
Correct |
28 ms |
39680 KB |
Output is correct |
30 |
Correct |
28 ms |
39680 KB |
Output is correct |
31 |
Correct |
28 ms |
39680 KB |
Output is correct |
32 |
Correct |
27 ms |
39680 KB |
Output is correct |
33 |
Correct |
28 ms |
39672 KB |
Output is correct |
34 |
Correct |
28 ms |
39680 KB |
Output is correct |
35 |
Correct |
27 ms |
39680 KB |
Output is correct |
36 |
Correct |
27 ms |
39680 KB |
Output is correct |
37 |
Correct |
27 ms |
39672 KB |
Output is correct |
38 |
Correct |
27 ms |
39800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39552 KB |
Output is correct |
2 |
Correct |
27 ms |
39552 KB |
Output is correct |
3 |
Correct |
27 ms |
39680 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
27 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39552 KB |
Output is correct |
7 |
Correct |
27 ms |
39552 KB |
Output is correct |
8 |
Correct |
27 ms |
39552 KB |
Output is correct |
9 |
Correct |
26 ms |
39552 KB |
Output is correct |
10 |
Correct |
26 ms |
39552 KB |
Output is correct |
11 |
Correct |
27 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39544 KB |
Output is correct |
13 |
Correct |
27 ms |
39552 KB |
Output is correct |
14 |
Correct |
26 ms |
39552 KB |
Output is correct |
15 |
Correct |
27 ms |
39552 KB |
Output is correct |
16 |
Correct |
26 ms |
39552 KB |
Output is correct |
17 |
Correct |
27 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
167 ms |
50188 KB |
Output is correct |
20 |
Correct |
174 ms |
50040 KB |
Output is correct |
21 |
Correct |
167 ms |
50040 KB |
Output is correct |
22 |
Correct |
169 ms |
49912 KB |
Output is correct |
23 |
Correct |
198 ms |
52856 KB |
Output is correct |
24 |
Correct |
172 ms |
50808 KB |
Output is correct |
25 |
Correct |
160 ms |
55928 KB |
Output is correct |
26 |
Correct |
108 ms |
62968 KB |
Output is correct |
27 |
Correct |
262 ms |
59256 KB |
Output is correct |
28 |
Correct |
393 ms |
89396 KB |
Output is correct |
29 |
Correct |
389 ms |
88008 KB |
Output is correct |
30 |
Correct |
259 ms |
59420 KB |
Output is correct |
31 |
Correct |
264 ms |
59300 KB |
Output is correct |
32 |
Correct |
345 ms |
59640 KB |
Output is correct |
33 |
Correct |
332 ms |
59512 KB |
Output is correct |
34 |
Correct |
521 ms |
71236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39552 KB |
Output is correct |
2 |
Correct |
27 ms |
39552 KB |
Output is correct |
3 |
Correct |
27 ms |
39680 KB |
Output is correct |
4 |
Correct |
27 ms |
39552 KB |
Output is correct |
5 |
Correct |
27 ms |
39552 KB |
Output is correct |
6 |
Correct |
26 ms |
39552 KB |
Output is correct |
7 |
Correct |
27 ms |
39552 KB |
Output is correct |
8 |
Correct |
27 ms |
39552 KB |
Output is correct |
9 |
Correct |
26 ms |
39552 KB |
Output is correct |
10 |
Correct |
26 ms |
39552 KB |
Output is correct |
11 |
Correct |
27 ms |
39552 KB |
Output is correct |
12 |
Correct |
27 ms |
39544 KB |
Output is correct |
13 |
Correct |
27 ms |
39552 KB |
Output is correct |
14 |
Correct |
26 ms |
39552 KB |
Output is correct |
15 |
Correct |
27 ms |
39552 KB |
Output is correct |
16 |
Correct |
26 ms |
39552 KB |
Output is correct |
17 |
Correct |
27 ms |
39552 KB |
Output is correct |
18 |
Correct |
27 ms |
39552 KB |
Output is correct |
19 |
Correct |
27 ms |
39424 KB |
Output is correct |
20 |
Correct |
27 ms |
39552 KB |
Output is correct |
21 |
Correct |
28 ms |
39552 KB |
Output is correct |
22 |
Correct |
28 ms |
39680 KB |
Output is correct |
23 |
Correct |
28 ms |
39800 KB |
Output is correct |
24 |
Correct |
28 ms |
39680 KB |
Output is correct |
25 |
Correct |
28 ms |
39680 KB |
Output is correct |
26 |
Correct |
29 ms |
39680 KB |
Output is correct |
27 |
Correct |
28 ms |
39680 KB |
Output is correct |
28 |
Correct |
28 ms |
39680 KB |
Output is correct |
29 |
Correct |
28 ms |
39680 KB |
Output is correct |
30 |
Correct |
28 ms |
39680 KB |
Output is correct |
31 |
Correct |
28 ms |
39680 KB |
Output is correct |
32 |
Correct |
27 ms |
39680 KB |
Output is correct |
33 |
Correct |
28 ms |
39672 KB |
Output is correct |
34 |
Correct |
28 ms |
39680 KB |
Output is correct |
35 |
Correct |
27 ms |
39680 KB |
Output is correct |
36 |
Correct |
27 ms |
39680 KB |
Output is correct |
37 |
Correct |
27 ms |
39672 KB |
Output is correct |
38 |
Correct |
27 ms |
39800 KB |
Output is correct |
39 |
Correct |
167 ms |
50188 KB |
Output is correct |
40 |
Correct |
174 ms |
50040 KB |
Output is correct |
41 |
Correct |
167 ms |
50040 KB |
Output is correct |
42 |
Correct |
169 ms |
49912 KB |
Output is correct |
43 |
Correct |
198 ms |
52856 KB |
Output is correct |
44 |
Correct |
172 ms |
50808 KB |
Output is correct |
45 |
Correct |
160 ms |
55928 KB |
Output is correct |
46 |
Correct |
108 ms |
62968 KB |
Output is correct |
47 |
Correct |
262 ms |
59256 KB |
Output is correct |
48 |
Correct |
393 ms |
89396 KB |
Output is correct |
49 |
Correct |
389 ms |
88008 KB |
Output is correct |
50 |
Correct |
259 ms |
59420 KB |
Output is correct |
51 |
Correct |
264 ms |
59300 KB |
Output is correct |
52 |
Correct |
345 ms |
59640 KB |
Output is correct |
53 |
Correct |
332 ms |
59512 KB |
Output is correct |
54 |
Correct |
521 ms |
71236 KB |
Output is correct |
55 |
Correct |
42 ms |
40960 KB |
Output is correct |
56 |
Correct |
37 ms |
40440 KB |
Output is correct |
57 |
Correct |
117 ms |
49528 KB |
Output is correct |
58 |
Correct |
85 ms |
48880 KB |
Output is correct |
59 |
Correct |
117 ms |
64668 KB |
Output is correct |
60 |
Correct |
423 ms |
88500 KB |
Output is correct |
61 |
Correct |
288 ms |
60664 KB |
Output is correct |
62 |
Correct |
256 ms |
59284 KB |
Output is correct |
63 |
Correct |
335 ms |
59384 KB |
Output is correct |
64 |
Correct |
570 ms |
71856 KB |
Output is correct |
65 |
Correct |
594 ms |
74432 KB |
Output is correct |
66 |
Correct |
414 ms |
84348 KB |
Output is correct |
67 |
Correct |
246 ms |
58340 KB |
Output is correct |
68 |
Correct |
410 ms |
67772 KB |
Output is correct |
69 |
Correct |
459 ms |
70592 KB |
Output is correct |
70 |
Correct |
385 ms |
66620 KB |
Output is correct |