2599: [IOI2011]Race
分析
被memset卡。。。
点分治,对于重心,遍历子树,记录一个数组T[i],表示以重心为起点的长度为i的路径中最少的边数是多少。然后先遍历子树,更新答案,然后在遍历一边更新T,防止出现两个起点在同一棵子树中的情况。
代码
#include<cstdio>
#include<algorithm>
#include<cctype> using namespace std; const int N = ;
const int INF = 1e9; struct Edge{
int to,nxt,w;
Edge() {}
Edge(int a,int b,int c) {to = a,w = b,nxt = c;}
}e[N<<];
int head[N],siz[N],T[];
bool vis[N];
int n,k,tot,Size,Num,Root,Ans = N; inline int read() {
int x = ,f = ;char ch=getchar();
for (; !isdigit(ch); ch=getchar()) if(ch=='-')f=-;
for (; isdigit(ch); ch=getchar()) x=x*+ch-'';
return x*f;
}
void add_edge(int u,int v,int w) {
e[++tot] = Edge(v,w,head[u]);head[u] = tot;
e[++tot] = Edge(u,w,head[v]);head[v] = tot;
}
void getRoot(int u,int fa) {
siz[u] = ;
int mx = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v == fa || vis[v]) continue;
getRoot(v,u);
siz[u] += siz[v];
mx = max(siz[v],mx);
}
mx = max(Size-siz[u],mx);
if (mx < Num) Root = u,Num = mx;
}
void dfs1(int u,int fa,int L,int C) {
if (L > k) return ;
Ans = min(Ans,T[k-L]+C);
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v == fa || vis[v]) continue;
dfs1(v,u,L+e[i].w,C+);
}
}
void dfs2(int u,int fa,int L,int C,int f) {
if (L > k) return ;
if (f) T[L] = min(T[L],C);
else T[L] = INF;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v == fa || vis[v]) continue;
dfs2(v,u,L+e[i].w,C+,f);
}
}
void calcc(int u) {
T[] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (vis[v]) continue;
dfs1(v,u,e[i].w,);
dfs2(v,u,e[i].w,,);
}
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (vis[v]) continue;
dfs2(v,u,e[i].w,,);
}
}
void solve(int u) {
vis[u] = true;
calcc(u);
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (vis[v]) continue;
Size = siz[v],Root = ,Num = 1e9;
getRoot(v,);
solve(Root);
}
}
int main() {
n = read(),k = read();
for (int i=; i<=k; ++i) T[i] = INF;
for (int u,v,w,i=; i<n; ++i) {
u = read(),v = read(),w = read();
add_edge(u+,v+,w);
if (w == k) {printf("-1");return ;}
}
Size = n;Num = 1e9;
getRoot(,);
solve(Root);
if (Ans == N) puts("-1");
else printf("%d",Ans);
return ;
}