HDU 4679 terrorist ' s destroy (edge removal + tree diameter)

Source: Internet
Author: User

To a tree, each edge has a weight, remove any edge of the tree, divided into two sub-trees, the longest path of two subtree and the weight on the edge of the multiplication, to a product. Ask to remove that edge to make the product smallest.

First find the longest road on the tree, then there are two cases when you delete the edge:

1. This side is not the longest road side

2. This edge is the edge of the longest road

For the first case, it is easy to calculate the Chuyang product.

For the second case, it is only possible to calculate the longest path length within the two subtree that is divided, which can be preprocessed. Analysis can be found, in this case, the longest path of the two subtrees tree, must be the entire tree of the longest path of the two endpoints as the starting point, so only need to preprocess all points to two endpoints distance, and then according to the longest roadside removed to find the maximum number of two subtrees.

All preprocessing is DFS ...

My Code:

#pragma COMMENT (linker, "/stack:102400000,102400000") #include <cstdio> #include <cstring> #include <
Algorithm> using namespace std;
#define N 100100 int n;
int head[n],cnt,len,ans,id;
int dis_s[n],dis_t[n],mx_s[n],mx_t[n]; struct Edge {int next,to,w,id;}
E[N*2];
    void Add_edge (int a,int b,int w,int id) {e[cnt].id=id;
    E[cnt].to=b;
    E[cnt].w=w;
    E[cnt].next=head[a];
head[a]=cnt++;
        } void Dfs (int u, int fa, int dis[]) {for (int i = head[u]; I! =-1; i = e[i].next) {int v=e[i].to;
    if (V!=FA) {Dis[v]=dis[u]+1;dfs (V,u,dis);}
    }} void Get_max (int u, int fa, int mx[],int dis[]) {mx[u]=dis[u];
        for (int i = head[u]; I! =-1; i = e[i].next) {int v=e[i].to;
            if (V!=FA) {Get_max (V,u,mx,dis);
        Mx[u]=max (Mx[u],mx[v]);
        }}}} void Dfs2 (int u, int fa) {for (int i = head[u]; I! =-1; i = e[i].next) {int v=e[i].to;
if (V!=FA) {            int W;
            if (Dis_s[u]+dis_t[v]+1==len) W=e[i].w*max (Mx_s[u],mx_t[v]);
            else W=e[i].w*len;
            if (W<ans) {ans=w;id=e[i].id;}
            else if (W==ans) id=min (id,e[i].id);
        DFS2 (V,u);
    }}} int main () {int t;scanf ("%d", &t);
        for (int g = 1; G <= T; g++) {scanf ("%d", &n);
        memset (head,-1,sizeof (head));
        cnt=0;
            for (int i = 1; i < n; i++) {int a,b,w;scanf ("%d%d%d", &a,&b,&w);
            Add_edge (A,b,w,i);
        Add_edge (B,a,w,i);
        } dis_s[1]=0;
        DFS (1,-1,dis_s);
        int S=1;
        for (int i = 1; I <= n; i++) if (dis_s[s]<dis_s[i]) s=i;
        dis_s[s]=0;
        DFS (s,-1,dis_s);
        LEN=DIS_S[1];
        int t=1;
        for (int i = 1; I <= n; i++) if (Dis_s[t]<dis_s[i]) {t=i;len=dis_s[t];}
        dis_t[t]=0;
        DFS (t,-1,dis_t);
        Get_max (t,-1,mx_s,dis_s); Get_max (s,-1,mx_t, dis_t);
        ANS=0X7FFFFFF;
        DFS2 (s,-1);
    printf ("Case #%d:%d\n", g,id);
} return 0; }


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.