经典换根dp——hdu2196
给定一棵边权树,求距离每个点最远的点,输出这个距离 #include<bits/stdc++.h> using namespace std; #define N 10005 struct Edge{int to,nxt,w;}e[N<<1]; int head[N],tot,n; void init(){memset(head,-1,sizeof head);tot=0;} void add(int u,int v,int w){ e[tot].to=v;e[tot].w=w;e[tot].nxt=head[u];head[u]=tot++; } int d[N]; void dfs1(int u,int pre){ d[u]=0; for(int i=head[u];i!=-1;i=e[i].nxt){ int v=e[i].to; if(v==pre)continue; dfs1(v,u); d[u]=max(d[u],d[v]+e[i].w); } } int ans[N]; void dfs2(int u,int pre,int up){ int mx1,mx2,id1,id2; mx1=mx2=0; id1=id2=0; for(int i=head[u];i!=-1;i=e[i].nxt){ int v=e[i].to; if(v==pre)continue; if(d