///0-0 : what is your motivation, quan606303?
///quan606303 : Hutao
/*idea :
*/
#include <bits/stdc++.h>
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define file(a) freopen(a".inp","r",stdin); freopen(a".out","w",stdout);
using namespace std;
const int MOD=1e9+7;
const int maxn=2e5+7;
const int maxk=1e6+7;
int n,k,del[maxn],child[maxn];
vector<pair<int,int> > adj[maxn];
int ans=1e12,cnt[maxk];
const int inf=1e12;
void dfs(int u,int p)
{
child[u]=1;
for (auto v:adj[u])
{
if (v.fi!=p&&!del[v.fi])
{
dfs(v.fi,u);
child[u]+=child[v.fi];
}
}
}
int find_centroid(int u,int p,int N)
{
for (auto v:adj[u])
{
if (v.fi!=p&&!del[v.fi]&&child[v.fi]>N/2)
{
return find_centroid(v.fi,u,N);
}
}
return u;
}
int mxdis=0;
void cal(int u,int p,bool filling,int distance,int depth)
{
if (distance>k)return ;
mxdis=max(mxdis,distance);
if (filling)cnt[distance]=min(cnt[distance],depth);
else
{
ans=min(ans,cnt[k-distance]+depth);
// cout<<u<<" "<<distance<<" "<<depth<<" "<<cnt[k-distance]<<endl;
}
for (auto v:adj[u])
{
if (!del[v.fi]&&v.fi!=p)cal(v.fi,u,filling,distance+v.se,depth+1);
}
}
void solve(int u)
{
dfs(u,0);
int N=child[u];
int root=find_centroid(u,0,N);
// for (int i=1;i<=n;i++)cout<<child[i]<<endl;
del[root]=true;
mxdis=0;
for (auto v:adj[root])
{
if (!del[v.fi])
{
// cout<<v.fi<<" "<<root<<" "<<v.se<<" "<<1<<endl;
cal(v.fi,root,false,v.se,1);
cal(v.fi,root,true,v.se,1);
}
}
fill(cnt+1,cnt+1+mxdis,inf);
for (auto v:adj[root])
{
if (!del[v.fi])solve(v.fi);
}
}
int best_path(int N, int K, int H[][2], int L[]){
// file("");
n=N;
k=K;
for (int i=1;i<n;i++)
{
int x = H[i][0];
int y = H[i][1];
int w = L[i];
x++;
y++;
adj[x].push_back({y,w});
adj[y].push_back({x,w});
}
for (int i=1;i<maxk;i++)cnt[i]=inf;
solve(1);
if (ans==inf)return -1;
else return ans;
}
Compilation message
race.cpp:25:9: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+12' to '2147483647' [-Woverflow]
25 | int ans=1e12,cnt[maxk];
| ^~~~
race.cpp:26:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+12' to '2147483647' [-Woverflow]
26 | const int inf=1e12;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |