Terrorist’s destroy HDU - 4679

Terrorist’s destroy HDU - 4679 

 

There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But now he is alone, he can only destroy one road, then the city will be divided into two cities. Impression of the city is a number defined as the distance between the farthest two houses (As it relates to the fare).When the terrorist destroyed a road, he needs to spend some energy, assuming that the number is a.At the same time,he will get a number b which is maximum of the Impression of two cities. The terrorist wants to know which road to destroy so that the product of a and b will be minimized.You should find the road's id. 
Note that the length of each road is one. 

InputThe first line contains integer T(1<=T<=20), denote the number of the test cases. 
For each test cases,the first line contains a integer n(1 < n <= 100000);denote the number of the houses; 
Each of the following (n-1) lines contains third integers u,v,w, indicating there is a road between house u and houses v,and will cost terrorist w energy to destroy it.The id of these road is number from 1 to n-1.(1<=u<=n , 1<=v<=n , 1<=w<=10000)OutputFor each test case, output the case number first,and then output the id of the road which the terrorist should destroy.If the answer is not unique,output the smallest id.Sample Input

2
5
4 5 1
1 5 1
2 1 1
3 5 1
5
1 4 1
1 3 1
5 1 1
2 5 1

Sample Output

Case #1: 2
Case #2: 3


题意:给出一颗树,删除其中的一个边,随后变成了两颗树,两颗树的直径为l1和l2,剪掉的边长为x,要求x*max(l1,l2)最小,求删除的是第几条边
思路:1.剪掉的是原来树上的枝,那么就是原来树的直径*删除的那条边的长度
   2.删除的是原来树上的直径的某一条,那么肯定是直径的一部分加枝条*删除的那条变的长度
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 2000000000;
const int maxn = 200020;
int q[maxn],dislong[maxn],deleteLeft[maxn],deleteRight[maxn],pre[maxn],father[maxn],dep[maxn];
int list[maxn],Next[maxn],p[maxn],c[maxn],id[maxn],maxx,n;
bool b[maxn],inlong[maxn];


int findlong(int xx,int n)
{
    int t,w,now,k,x;
    for (int i = 1; i <= n; i++)
    {
        b[i] = true;
    }
    t = 0; w = 1;
    q[1] = xx;
    dislong[1] = 1;
    b[xx] = false;
    pre[xx] = 0;
    maxx = 0;
    while (t < w)
    {
        t++; x = q[t];
        k = list[x];
        while (k > 0)
        {
            if (b[p[k]] == true)
            {
                w++;
                b[p[k]] = false;
                q[w] = p[k];
                dislong[w] = dislong[t]+1;
                pre[p[k]] = x;
                if (dislong[w] > maxx) {maxx = dislong[w]; now = p[k];}
            }
            k = Next[k];
        }
    }
    return now;
}
void init(int n)
{
    for (int i = 1; i <= n; i++)
    {
        list[i] = 0;
    }
}
void dfs_dep(int x,int pre1)
{
    int k;
    dep[x] = 1;
    k = list[x];
    while (k > 0)
    {
        if (inlong[p[k]] == false && p[k] != pre1)
        {
            dfs_dep(p[k],x);
            dep[x] = max(dep[x],dep[p[k]]+1);
        }
        k = Next[k];
    }
}

int main()
{
    int t;
    scanf("%d",&t);
   int ca = 1;
   while(t--)
   {
        int tot = 0;
        scanf("%d",&n);
        init(n);
        for (int i = 1;i < n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            tot++;
            Next[tot] = list[u];
            list[u] = tot;
            p[tot] = v;
            c[tot] = w;
            id[tot] = i;
            tot++;
            Next[tot] = list[v];
            list[v] = tot;
            p[tot] = u;
            c[tot] = w;
            id[tot] = i;
        }
        if (n != 1)
        {
            memset(pre,0,sizeof(pre));
            int front = findlong(1,n);
            int rear = findlong(front,n);
            int sum = maxx-1;

            int k = rear;
            memset(inlong,false,sizeof(inlong));
            memset(father,0,sizeof(father));
            while (k > 0)
            {
                inlong[k] = true;
                father[pre[k]] = k;
                k = pre[k];
            }
            memset(dep,0,sizeof(dep));
            for(int i=1;i<=n;i++)
                if(inlong[i])
                    dfs_dep(i,0);
            memset(deleteLeft,0,sizeof deleteLeft);
            memset(deleteRight,0,sizeof deleteRight);
            k = front;
            int step = 0;
            while (k != rear)
            {
                step++;
                deleteLeft[k] = max(deleteLeft[pre[k]],step-1+dep[k]-1);
                k = father[k];
            }

            k = rear; step = 0;
            father[rear] = 0;
            while (k != front)
            {
                step++;
                deleteRight[k] = max(deleteRight[father[k]],step-1+dep[k]-1);
                k = pre[k];
            }
            //遍历直径
            int ans = INF;
            int result = INF;
            k = front;
            int kk;
            while (k != rear)
            {
                kk = list[k];
                while (kk > 0)
                {
                    if (p[kk] == father[k]) break;
                    kk = Next[kk];
                }
                if (ans > c[kk]*max(deleteLeft[k],deleteRight[father[k]]))
                {
                    ans = c[kk]*max(deleteLeft[k],deleteRight[father[k]]);
                    result = id[kk];

                }
                else if (ans == c[kk]*max(deleteLeft[k],deleteRight[father[k]]))
                {
                    if (result > id[kk]) result = id[kk];
                }
                k = father[k];
            }

            //遍历枝条
            for (int i = 1; i <= n; i++)
            {
                k = list[i];
                while (k > 0)
                {
                    if(!(inlong[i] && inlong[p[k]]))
                    {
                        if (ans > c[k]*sum)
                        {
                            ans = c[k]*sum;
                            result = id[k];

                        }
                        else if (ans == c[k]*sum)
                        {
                            if (result > id[k]) result = id[k];
                        }
                    }
                    k = Next[k];
                }
            }
            printf("Case #%d: %d\n",ca++,result);

        }
    }
    return 0;
}
View Code

 

网上也有思路说之间dfs直径的两个端点,但是会爆栈,要手动添栈,但在hdu上交了一发不用加栈。代码如下

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
struct Edge
{
    int to,next;
    int id,w;
}edge[maxn*2];
int mm[maxn*2];
int maxx[maxn],smaxx[maxn],head[maxn],tot;
int ans;
int dep[maxn];
int p[maxn];
bool used[maxn];
int cnt;
int Index;
int a[maxn];
void init()
{
    memset(head,-1,sizeof head);
    tot = 0;
}

void addedge(int u,int v,int w,int id)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].id = id;
    edge[tot].next = head[u];
    head[u] = tot++;
    edge[tot].to = u;
    edge[tot].w = w;
    edge[tot].id = id;
    edge[tot].next = head[v];
    head[v] = tot++;
}
void dfs1(int u,int pre)
{
    p[u] = pre;
    dep[u] = dep[pre] + 1;
    for(int i = head[u]; i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(v==pre)continue;
        dfs1(v,u);
    }
}
void dfs(int u,int pre)
{
    mm[u] = 0;
    maxx[u] = 0;
    smaxx[u] = 0;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == pre)continue;
        dfs(v,u);
        if(maxx[v]+1 > smaxx[u])
        {
            smaxx[u] = maxx[v] + 1;
            if(smaxx[u] > maxx[u])
            {
                swap(smaxx[u],maxx[u]);
            }
        }
        if(mm[v] > mm[u])
            mm[u] = mm[v];
    }
    mm[u] = max(mm[u],maxx[u]+smaxx[u]);
}
void solve(int u,int pre)
{
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        int w = edge[i].w;
        if(v == pre)continue;
        solve(v,u);
        if(used[v])
        {
            a[edge[i].id] = max(a[edge[i].id],w*mm[v]);
        }
        else
        {
            a[edge[i].id] = max(a[edge[i].id],w*cnt);
        }
    }
}


int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int n;
    scanf("%d",&T);
    int u,v,w;
    int iCase = 0;
    while(T--)
    {
        iCase ++;
        init();
        scanf("%d",&n);
        for(int i = 1;i < n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w,i);
        }
        dep[0] = 0;
        dfs1(1,0);
        u = 1;
        for(int i = 1;i <= n;i++)
            if(dep[u] < dep[i])
                u = i;
        dfs1(u,0);
        v = 1;
        for(int i =1;i <= n;i++)
            if(dep[v] < dep[i])
                v = i;
        cnt = dep[v]-1;
        memset(used,false,sizeof(used));
        int tmp = v;
        while(tmp)
        {
            used[tmp] = true;
            tmp = p[tmp];
        }
        for(int i = 1;i <= n;i++)
            a[i] = 0;
        ans = INF;
        dfs(u,0);
        solve(u,-1);
        dfs(v,0);
        solve(v,-1);
        for(int i = 1;i < n;i++)
            if(a[i]<ans)
            {
                ans = a[i];
                Index = i;
            }
        printf("Case #%d: %d\n",iCase,Index);
    }

    return 0;
}
View Code

 

posted @ 2019-01-23 15:31  千摆渡Qbd  阅读(291)  评论(0编辑  收藏  举报