Hdu 4679 Terrorist's destroy

Source: Internet
Author: User
Tags acos

Question:

There is a spanning tree with the length of each side being 1

The terrorists blow up one of the sides and divide them into two parts.

Get the larger diameter in the diameter of the temp two-digit multiplied by the weight of this edge.

Find the minimum temp value for which side to blow up.

 


Solution 1:

Tree-like DP. Calculate the maximum number of all branches of each node.

After an edge is cut off, select two uncut values from the three values, that is, the diameter of the tree.

 


Solution 2:

Calculate the diameter of the entire tree twice

If the cut side is not in the diameter, then temp = w * diameter

If the edges on the diameter are cut off. So temp = w * max (A-1), (len + 1-b)

Len is the diameter.

 

/*==========================================   *   @author: Dazdingo  *   @blog:   blog.csdn.net/lfj200411  *   @email:  lfj200411@163.com  *===========================================*/    #include <iostream>   #include <cstdio>   #include <algorithm>   #include <vector>   #include <queue>   #include <cmath>   #include <cstring>   #pragma comment(linker,"/STACK:102400000,102400000")   using namespace std;  #define MP make_pair   typedef pair<int, int >PII;  const int INF = 0x3f3f3f3f;  const double PI  = acos(-1.0);  struct EDGE{      int next,w,v,id;  }E[100010*2];  int head[100010];  int size;  void initEDGE(){      size=0;      memset(head,-1,sizeof head);  }  void addEDGE(int u,int v,int w,int id){      E[size].v=v;      E[size].id=id;      E[size].w=w;      E[size].next=head[u];      head[u]=size++;  }  int ans,ansid;  int depth[100010];  int max1[100010],max2[100010],max3[100010];  int v1[100010],v2[100010],v3[100010];  int dfs(int u,int pre){      for(int e=head[u];e!=-1;e=E[e].next){          int v=E[e].v;          if(pre==v) continue;          depth[u]=max(depth[u],dfs(v,u)+1);      }      return depth[u];  }  void dfs1(int u,int pre,int prew,int pid){      int temp;      for(int e=head[u];e!=-1;e=E[e].next){          int v=E[e].v;          if(v==pre) continue;          temp=depth[v]+1;          if(temp>max1[u]){              max3[u]=max2[u];v3[u]=v2[u];              max2[u]=max1[u];v2[u]=v1[u];              max1[u]=temp;              v1[u]=v;          }else if(temp>max2[u]){              max3[u]=max2[u];v3[u]=v2[u];              max2[u]=temp;              v2[u]=v;          }else if(temp>max3[u]){              max3[u]=temp;              v3[u]=v;          }      }      temp=0;      if(pre){          if(v1[pre]!=u) temp=max1[pre]+1;          else if(v2[pre]!=u) temp=max2[pre]+1;          else temp=max3[pre]+1;      if(temp>max1[u]){          max3[u]=max2[u];v3[u]=v2[u];          max2[u]=max1[u];v2[u]=v1[u];          max1[u]=temp;          v1[u]=pre;      }else if(temp>max2[u]){          max3[u]=max2[u];v3[u]=v2[u];          max2[u]=temp;          v2[u]=pre;      }else if(temp>max3[u]){          max3[u]=temp;          v3[u]=pre;      }      int umax1=max1[u];      int umax2=max2[u];      int premax1=max1[pre];      int premax2=max2[pre];      if(v1[u]==pre) umax1=max3[u];      else if(v2[u]==pre) umax2=max3[u];      if(v1[pre]==u) premax1=max3[pre];      else if(v2[pre]==u) premax2=max3[pre];      temp=max(umax1+umax2,premax1+premax2)*prew;      if(temp<ans){          ans=temp;          ansid=pid;      }else if(temp==ans){          ansid=min(pid,ansid);      }      }      for(int e=head[u];e!=-1;e=E[e].next){          int v=E[e].v;          int w=E[e].w;          int id=E[e].id;          if(v==pre) continue;          dfs1(v,u,w,id);      }  }    int main(){      int T;      int n,cas=1;      scanf("%d",&T);      while(T--){          initEDGE();          scanf("%d",&n);          for(int i=1;i<n;i++){              int u,v,w;              scanf("%d%d%d",&u,&v,&w);              addEDGE(u,v,w,i);              addEDGE(v,u,w,i);          }          memset(depth,0,sizeof depth);          dfs(1,0);          memset(max1,0,sizeof max1);          memset(max2,0,sizeof max2);          memset(max3,0,sizeof max3);          ans=INF;          ansid=0;          dfs1(1,0,0,0);          printf("Case #%d: %d\n",cas++,ansid);      }      return 0;  }  /*========================================== *   @author: Dazdingo *   @blog:   blog.csdn.net/lfj200411 *   @email:  lfj200411@163.com *===========================================*/#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <cstring>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MP make_pairtypedef pair<int, int >PII;const int INF = 0x3f3f3f3f;const double PI  = acos(-1.0);struct EDGE{    int next,w,v,id;}E[100010*2];int head[100010];int size;void initEDGE(){    size=0;    memset(head,-1,sizeof head);}void addEDGE(int u,int v,int w,int id){    E[size].v=v;    E[size].id=id;    E[size].w=w;    E[size].next=head[u];    head[u]=size++;}int ans,ansid;int depth[100010];int max1[100010],max2[100010],max3[100010];int v1[100010],v2[100010],v3[100010];int dfs(int u,int pre){    for(int e=head[u];e!=-1;e=E[e].next){        int v=E[e].v;        if(pre==v) continue;        depth[u]=max(depth[u],dfs(v,u)+1);    }    return depth[u];}void dfs1(int u,int pre,int prew,int pid){    int temp;    for(int e=head[u];e!=-1;e=E[e].next){        int v=E[e].v;        if(v==pre) continue;        temp=depth[v]+1;        if(temp>max1[u]){            max3[u]=max2[u];v3[u]=v2[u];            max2[u]=max1[u];v2[u]=v1[u];            max1[u]=temp;            v1[u]=v;        }else if(temp>max2[u]){            max3[u]=max2[u];v3[u]=v2[u];            max2[u]=temp;            v2[u]=v;        }else if(temp>max3[u]){            max3[u]=temp;            v3[u]=v;        }    }    temp=0;    if(pre){        if(v1[pre]!=u) temp=max1[pre]+1;        else if(v2[pre]!=u) temp=max2[pre]+1;        else temp=max3[pre]+1;    if(temp>max1[u]){        max3[u]=max2[u];v3[u]=v2[u];        max2[u]=max1[u];v2[u]=v1[u];        max1[u]=temp;        v1[u]=pre;    }else if(temp>max2[u]){        max3[u]=max2[u];v3[u]=v2[u];        max2[u]=temp;        v2[u]=pre;    }else if(temp>max3[u]){        max3[u]=temp;        v3[u]=pre;    }    int umax1=max1[u];    int umax2=max2[u];    int premax1=max1[pre];    int premax2=max2[pre];    if(v1[u]==pre) umax1=max3[u];    else if(v2[u]==pre) umax2=max3[u];    if(v1[pre]==u) premax1=max3[pre];    else if(v2[pre]==u) premax2=max3[pre];    temp=max(umax1+umax2,premax1+premax2)*prew;    if(temp<ans){        ans=temp;        ansid=pid;    }else if(temp==ans){        ansid=min(pid,ansid);    }    }    for(int e=head[u];e!=-1;e=E[e].next){        int v=E[e].v;        int w=E[e].w;        int id=E[e].id;        if(v==pre) continue;        dfs1(v,u,w,id);    }}int main(){    int T;    int n,cas=1;    scanf("%d",&T);    while(T--){        initEDGE();        scanf("%d",&n);        for(int i=1;i<n;i++){            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            addEDGE(u,v,w,i);            addEDGE(v,u,w,i);        }        memset(depth,0,sizeof depth);        dfs(1,0);        memset(max1,0,sizeof max1);        memset(max2,0,sizeof max2);        memset(max3,0,sizeof max3);        ans=INF;        ansid=0;        dfs1(1,0,0,0);        printf("Case #%d: %d\n",cas++,ansid);    }    return 0;}
#include <cstdio>   #include <cctype>   #include <vector>   #include <algorithm>   #include <cstring>   #include <list>   using namespace std;  #pragma comment(linker, "/STACK:1024000000,1024000000")   const int INF=0x3f3f3f3f;  struct Edge{      int v,next;      int id;      int w;  }E[100010*2];  int head[100010],size;  void addedge(int u,int v,int w,int id){      E[size].id=id;      E[size].v=v;      E[size].w=w;      E[size].next=head[u];      head[u]=size++;  }  void initedge(){      size = 0 ;      memset(head, -1,sizeof head);  }  int len,root;  int p[100010];  int q[100010];  int man[100010];  void dfs0(int u,int deep,int pre){      if(deep>len){          len=deep;          root=u;      }      for(int e=head[u] ; e!=-1 ; e=E[e].next){          int v=E[e].v;          if(v==pre) continue;          dfs0(v,deep+1,u);      }  }  int dep[100010];  void dfs(int u,int pre){      q[u] = pre;      dep[u] = dep[pre] + 1;      for(int e=head[u] ; e!=-1 ; e=E[e].next){          int v=E[e].v;          if(v==pre) continue;          dfs(v,u);      }  }  int ans,ansid;  void solve(int u,int pre){      for(int e=head[u] ; e!=-1 ; e=E[e].next){          int v=E[e].v;          int w=E[e].w;          int id=E[e].id;          if(v==pre) continue;          solve(v,u);          if(man[u]&&man[v]){              int a=man[u],b=man[v];              if(a>b) swap(a,b);              int tempmax=max((a-1) , (len+1-b) );              if(ans > w*tempmax){                  ans=w*tempmax;                  ansid=id;              }          }          else{              if(ans>w*len){                  ans=w*len;                  ansid=id;              }          }      }  }  int main(){      int n,T,cas=1;      scanf("%d",&T);      while(T--){          scanf("%d",&n);          initedge();          for(int i=1 ; i<n ; i++){              int u,v,w;              scanf("%d%d%d",&u,&v,&w);              addedge(u,v,w,i);              addedge(v,u,w,i);          }          len=-1;          dfs0(1,0,-1);          memset(p , 0 , sizeof p);          memset(man , 0 , sizeof man);          dfs(root,0);          int v=1,deep=1;          for(int i =1;i <= n;i++)              if(dep[v] < dep[i])                  v = i;          int tmp=v;          len=dep[v]-1;          while(tmp){              man[tmp]=deep++;              tmp=q[tmp];          }          ans=INF;          solve(1,-1);          printf("Case #%d: %d\n",cas++,ansid);      }      return 0;  }  #include <cstdio>#include <cctype>#include <vector>#include <algorithm>#include <cstring>#include <list>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")const int INF=0x3f3f3f3f;struct Edge{    int v,next;    int id;    int w;}E[100010*2];int head[100010],size;void addedge(int u,int v,int w,int id){    E[size].id=id;    E[size].v=v;    E[size].w=w;    E[size].next=head[u];    head[u]=size++;}void initedge(){    size = 0 ;    memset(head, -1,sizeof head);}int len,root;int p[100010];int q[100010];int man[100010];void dfs0(int u,int deep,int pre){    if(deep>len){        len=deep;        root=u;    }    for(int e=head[u] ; e!=-1 ; e=E[e].next){        int v=E[e].v;        if(v==pre) continue;        dfs0(v,deep+1,u);    }}int dep[100010];void dfs(int u,int pre){    q[u] = pre;    dep[u] = dep[pre] + 1;    for(int e=head[u] ; e!=-1 ; e=E[e].next){        int v=E[e].v;        if(v==pre) continue;        dfs(v,u);    }}int ans,ansid;void solve(int u,int pre){    for(int e=head[u] ; e!=-1 ; e=E[e].next){        int v=E[e].v;        int w=E[e].w;        int id=E[e].id;        if(v==pre) continue;        solve(v,u);        if(man[u]&&man[v]){            int a=man[u],b=man[v];            if(a>b) swap(a,b);            int tempmax=max((a-1) , (len+1-b) );            if(ans > w*tempmax){                ans=w*tempmax;                ansid=id;            }        }        else{            if(ans>w*len){                ans=w*len;                ansid=id;            }        }    }}int main(){    int n,T,cas=1;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        initedge();        for(int i=1 ; i<n ; i++){            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w,i);            addedge(v,u,w,i);        }        len=-1;        dfs0(1,0,-1);        memset(p , 0 , sizeof p);        memset(man , 0 , sizeof man);        dfs(root,0);        int v=1,deep=1;        for(int i =1;i <= n;i++)            if(dep[v] < dep[i])                v = i;        int tmp=v;        len=dep[v]-1;        while(tmp){            man[tmp]=deep++;            tmp=q[tmp];        }        ans=INF;        solve(1,-1);        printf("Case #%d: %d\n",cas++,ansid);    }    return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.