• HDU 2196 Computer


    第一次dfs求出每个点的最大和次大长度,由下向上更新

    第二次由上向下更新。第二次dfs是为了处理某个点,最大长度不是向子节点延伸的长度,而是从父亲节点过来的

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<string>
      5 #include<set>
      6 #include<vector>
      7 #include<map>
      8 #include<algorithm>
      9 #include<cmath>
     10 #include<stack>
     11 #include<stdlib.h>
     12 using namespace std;
     13 #define mmax 10000+10
     14 int F_max[mmax],S_max[mmax],F_maxid[mmax],S_maxid[mmax];
     15 const int MAXN=10010;
     16 int tol;
     17 struct Node
     18 {
     19     int to;
     20     int next;
     21     int len;
     22 }edge[MAXN*2];
     23 int head[MAXN];
     24 void add(int a,int b,int len)
     25 {
     26     edge[tol].to=b;
     27     edge[tol].len=len;
     28     edge[tol].next=head[a];
     29     head[a]=tol++;
     30     edge[tol].to=a;
     31     edge[tol].len=len;
     32     edge[tol].next=head[b];
     33     head[b]=tol++;
     34 }
     35 int n;
     36 void dfs1(int pos,int fa)
     37 {
     38    //cout<<"spo="<<pos<<endl;
     39     F_max[pos]=S_max[pos]=0;
     40     F_maxid[pos]=S_maxid[pos]=0;
     41     for(int i=head[pos]; i!=-1; i=edge[i].next){
     42         int to=edge[i].to;
     43         if(fa==to)
     44             continue;
     45         int llen=edge[i].len;
     46         dfs1(to,pos);
     47         if(S_max[pos]<F_max[to]+llen){
     48             S_max[pos]=F_max[to]+llen;
     49             S_maxid[pos]=to;
     50             if(S_max[pos]>F_max[pos]){
     51                 swap(S_max[pos],F_max[pos]);
     52                 swap(S_maxid[pos],F_maxid[pos]);
     53             }
     54         }
     55     }
     56 
     57 }
     58 void dfs2(int pos,int fa)
     59 {
     60     for(int i=head[pos]; i!=-1; i=edge[i].next){
     61         int to=edge[i].to;
     62         int llen=edge[i].len;
     63         if(to==fa)
     64             continue;
     65         if(to==F_maxid[pos]){
     66             if(S_max[to]<S_max[pos]+llen){
     67                 S_max[to]=S_max[pos]+llen;
     68                 S_maxid[to]=pos;
     69                 if(S_max[to]>F_max[to]){
     70                 swap(S_max[to],F_max[to]);
     71                 swap(S_maxid[to],F_maxid[to]);
     72                 }
     73             }
     74         }
     75         else{
     76              if(S_max[to]<F_max[pos]+llen){
     77                 S_max[to]=F_max[pos]+llen;
     78                 S_maxid[to]=pos;
     79                 if(S_max[to]>F_max[to]){
     80                 swap(S_max[to],F_max[to]);
     81                 swap(S_maxid[to],F_maxid[to]);
     82                 }
     83             }
     84         }
     85         dfs2(to,pos);
     86     }
     87 }
     88 void solve()
     89 {
     90     dfs1(1,0);
     91     dfs2(1,0);
     92     for(int i=1;i<=n;i++)
     93         cout<<F_max[i]<<endl;
     94 }
     95 int main()
     96 {
     97     while(cin>>n)
     98     {
     99         memset(head,-1,sizeof(head));
    100         tol=0;
    101         for(int i=1; i<n; i++){
    102             int a,b;
    103             cin>>a>>b;
    104             add(i+1,a,b);
    105         }
    106         solve();
    107     }
    108 }
  • 相关阅读:
    第二次冲刺-个人总结01
    构建之法阅读笔记03
    第一次冲刺-个人总结07
    第十四周总结
    第一次冲刺-个人总结07
    第一次冲刺-个人总结06
    第一次冲刺-个人总结05
    mysql优化
    springmvc常用注解标签详解
    弄懂JDK、JRE和JVM到底是什么
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3854993.html
Copyright © 2020-2023  润新知