#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
/*------------------------------------code------------------------------------*/
const ll MAXN=1e5+9;
const ll INF=1e9+9;
const ll R=(1<<18);
const int X=18;
//const ll M=1e9+7;
//const ll P=47;
int n, k;
vector<pair<int, int>> adj[MAXN];
vector<int> sz(MAXN);
vector<int> done(MAXN);
int ans=INF, curr;
vector<int> dist[10*MAXN];
vector<int> deg(MAXN);
vector<int> group(MAXN);
int dfs(int v, int p)
{
sz[v]=1;
for(auto [u, w] : adj[v])
{
if(u==p || done[u])
continue;
sz[v]+=dfs(u, v);
}
return sz[v];
}
int get(int v, int p, int mx)
{
for(auto [u, w] : adj[v])
{
if(u==p || done[u])
continue;
if(sz[u]>mx)
return get(u, v, mx);
}
return v;
}
void mark(int v, int p, ll d)
{
if(d==k)
{
ans=min(ans, deg[v]);
return;
}
dist[d].pb(v);
group[v]=curr;
deg[v]=deg[p]+1;
for(auto [u, w] : adj[v])
{
if(u==p || done[u] || d+w>k || deg[v]>=ans)
continue;
mark(u, v, d+w);
}
}
void solve(int v, int p, ll d)
{
for(int u : dist[k-d])
if(group[u]!=group[v])
ans=min(ans, deg[v]+deg[u]);
for(auto [u, w] : adj[v])
{
if(u==p || done[u] || d+w>k || deg[v]>=ans)
continue;
solve(u, v, d+w);
}
}
void cls(int v, int p, ll d)
{
group[v]=0;
dist[d].clear();
deg[v]=0;
for(auto [u, w] : adj[v])
if(group[u])
cls(u, v, d+w);
}
void decompose(int v)
{
int c = get(v, -1, dfs(v, -1)/2);
done[c]=true;
deg[c]=0;
curr=1;
for(auto [u, w] : adj[c])
{
if(done[u] || w>k)
continue;
mark(u, c, w);
curr++;
}
for(auto [u, w] : adj[c])
{
if(done[u] || w>k)
continue;
solve(u, c, w);
}
for(int i=0;i<=k;i++)
dist[i].clear();
for(int i=0;i<=n;i++)
group[i]=deg[i]=0;
/*
for(auto [u, w] : adj[c])
if(!done[u] && w<=k)
cls(u, v, w);
*/
for(auto [u, w] : adj[c])
if(!done[u])
decompose(u);
}
int best_path(int N, int K, int edges[][2], int weights[])
{
if(k==1)
return 0;
n=N;
k=K;
for(int i=0;i<n-1;i++)
{
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb({v, weights[i]});
adj[v].pb({u, weights[i]});
}
decompose(1);
return (ans==INF ? -1 : ans);
}
/*
int main()
{
cin.tie(0); ios_base::sync_with_stdio(0);
solve();
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
27856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
27856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
27856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
27856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |