#include <bits/stdc++.h>
#include "race.h"
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
using namespace std;
int n , k ;
const int mxn = 2e5+1 ;
vector<pii> adj[mxn] ;
vector<int> sz(mxn) , w(mxn) ,h(mxn) ;
void sub(int a , int p){
sz[a]=1 ;
if(p!=-1) h[a]=h[p]+1 ;
for(pii& x : adj[a]){
if(x.F==p) continue ;
w[x.F]=w[a]+x.S ;
sub(x.F,a) ;
sz[a]+=sz[x.F] ;
}
}
int mn = 1e9 ;
vector<int> p[mxn] ;
map<int,multiset<int>> cnt ;
void dfs(int a , int pp , int keep){
int bigchild = -1 , mx = -1 ;
for(pii& y : adj[a]){
int x = y.F ;
if(x==pp) continue ;
if(sz[x]>mx){
mx=sz[x] ;
bigchild= x ;
}
}
for(pii& y : adj[a]){
int x = y.F ;
if(x==pp||x==bigchild) continue ;
dfs(x,a,0) ;
}
if(bigchild!=-1){
dfs(bigchild,a,1) ;
// p[bigchild].swap(p[a]) ;
}
p[a].push_back(a) ;
int target = k+2*w[a] ;
if(cnt.count(target-w[a])) mn = min(mn,*(cnt[target-w[a]].begin())-h[a]) ;
cnt[w[a]].insert(h[a]) ;
for(pii& y : adj[a]){
if(y.F==pp||y.F==bigchild) continue ;
for(int& x : p[y.F]){
if(cnt.count(target-w[x])) mn = min(*(cnt[target-w[x]].begin())+h[x]-h[a]*2,mn) ;
p[a].pb(x) ;
}
for(int& x :p[y.F]) cnt[w[a]].insert(h[x]) ;
}
if(keep==0){
for(int& x: p[a]){
cnt[w[x]].erase(cnt[w[x]].find(h[x])) ;
if(cnt[w[x]].empty()) cnt.erase(cnt.find(w[x])) ;
}
}
}
int solve(){
sub(1,-1) ;
dfs(1,-1,1) ;
if(mn==1e9) return -1;
return mn ;
}
int best_path(int N , int K , int H[][2] , int L[]){
n = N ;
k = K ;
for(int i=0;i<n;i++){
adj[H[i][0]].push_back(mp(H[i][1],L[i]));
adj[H[i][1]].push_back(mp(H[i][0],L[i])) ;
}
return solve() ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
10 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
10 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Execution timed out |
3048 ms |
12032 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
10 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Runtime error |
114 ms |
35532 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
10 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Execution timed out |
3048 ms |
12032 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |