标签:思路 distance recent color most case imu panel max
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=2e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,MOD=2147493647; struct is { int v,w,nex; }edge[N]; int head[N],edg; int dis[N]; int deep,node1,node2; void init() { memset(head,-1,sizeof(head)); memset(dis,0,sizeof(dis)); edg=0; deep=0; } void add(int u,int v,int w) { edg++; edge[edg].v=v; edge[edg].w=w; edge[edg].nex=head[u]; head[u]=edg; } void dfs(int u,int fa,int val,int &node) { dis[u]=max(dis[u],val); if(val>deep) { deep=val; node=u; } for(int i=head[u];i!=-1;i=edge[i].nex) { int v=edge[i].v; int w=edge[i].w; if(v==fa)continue; dfs(v,u,val+w,node); } } int main() { int n; while(~scanf("%d",&n)) { init(); for(int i=2;i<=n;i++) { int v,w; scanf("%d%d",&v,&w); add(i,v,w); add(v,i,w); } dfs(1,-1,0,node1); deep=0; dfs(node1,-1,0,node2); deep=0; dfs(node2,-1,0,node1); for(int i=1;i<=n;i++) printf("%d\n",dis[i]); } return 0; }
标签:思路 distance recent color most case imu panel max
原文地址:http://www.cnblogs.com/jhz033/p/6095878.html