이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define endl '\n'
#define ff first
#define ss second
// #define int long long
#define MOD 1000000007
static const bool __boost = [](){
cin.tie(0);
cout.tie(0);
return ios::sync_with_stdio(0);
}();
int n,k;
int ans=1e9;
map<int,int> mp;
const int mxN=2e5+1;
const int mxM=1e6+1;
vector<bool> vis(mxN);
vector<pair<int,int> > v[mxN];
vector<int> sub(mxN),cnt(mxM);
void cal(int a, int p, int d, int w){
if(mp.count(k-w)) ans=min(ans,d+mp[k-w]);
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
cal(node.ff,a,d+1,w+node.ss);
}
}
}
void up(int a, int p, int d, int w){
if(w<=k){
if(mp.count(w)) mp[w]=min(mp[w],d);
else mp[w]=d;
}else{
return;
}
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
up(node.ff,a,d+1,w+node.ss);
}
}
}
int dfs(int a, int p){
sub[a]=1;
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]){
sub[a]+=dfs(node.ff,a);
}
}
return sub[a];
}
int find(int a, int p, int s){
for(auto node: v[a]){
if(node.ff!=p&&!vis[node.ff]&&sub[node.ff]>s/2){
return find(node.ff,a,s);
}
}
return a;
}
void build(int a, int p){
int cd=find(a,p,dfs(a,p));
vis[cd]=1;
mp[0]=0;
for(auto node: v[cd]){
if(!vis[node.ff]){
cal(node.ff,cd,1,node.ss);
up(node.ff,cd,1,node.ss);
}
}
for(auto x: mp) cnt[x.ff]=1e9;
mp.clear();
for(auto node: v[cd]){
if(!vis[node.ff]) build(node.ff,cd);
}
}
int best_path(int N, int K, int H[][2], int L[]){
__boost;
n=N;
k=K;
for(int i=0; i<n-1; i++){
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
build(1,-1);
if(ans==1e9) return -1;
return ans;
}
// signed main(){
// // freopen("file.in", "r", stdin);
// // freopen("file.out", "w", stdout);
// int T=1;
// // cin>>T;
// while(T--){
// int N,K; cin>>N>>K;
// int H[N][2],L[N];
// for(int i=0; i<N-1; i++) cin>>H[i][0]>>H[i][1];
// for(int i=0; i<N-1; i++) cin>>L[i];
// cout<<best_path(N,K,H,L);
// }
// return 0;
// }
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:98:5: warning: statement has no effect [-Wunused-value]
98 | __boost;
| ^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |