标签:res rip strong event ssi acm algorithm out struct
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31049 Accepted Submission(s): 3929
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<map> #include<stack> #include<set> #include<bitset> using namespace std; #define PI acos(-1.0) #define eps 1e-8 typedef long long ll; typedef pair<int,int > P; const int N=1e5+100,M=1e5+100; const int inf=0x3f3f3f3f; const ll INF=1e18+7,mod=1e9+7; struct edge { int from,to; ll w; int next; }; edge es[M]; int cnt,head[N]; ll dp[N][5]; void init() { cnt=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,ll w) { cnt++; es[cnt].from=u,es[cnt].to=v; es[cnt].w=w; es[cnt].next=head[u]; head[u]=cnt; } void dfs1(int u,int fa) { for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(e.to==fa) continue; dfs1(e.to,u); ll d=dp[e.to][0]+e.w; if(d>dp[u][0]) swap(d,dp[u][0]); if(d>dp[u][1]) swap(d,dp[u][1]); } } void dfs2(int u,int fa) { for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(e.to==fa) continue; if(dp[u][0]==dp[e.to][0]+e.w) dp[e.to][2]=max(dp[u][2],dp[u][1])+e.w; else dp[e.to][2]=max(dp[u][2],dp[u][0])+e.w; dfs2(e.to,u); } } int main() { int n; while(~scanf("%d",&n)) { init(); for(int i=2,j; i<=n; i++) { ll w; scanf("%d%lld",&j,&w); addedge(i,j,w); addedge(j,i,w); } memset(dp,0,sizeof(dp)); dfs1(1,0); dfs2(1,0); for(int i=1; i<=n; i++) printf("%d\n",max(dp[i][0],dp[i][2])); } return 0; }
标签:res rip strong event ssi acm algorithm out struct
原文地址:http://www.cnblogs.com/GeekZRF/p/7610697.html