zoukankan      html  css  js  c++  java
  • Computer HDU

    Computer HDU - 2196 

    A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 


    Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4. 

    InputInput file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.OutputFor each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).Sample Input

    5
    1 1
    2 1
    3 1
    1 1

    Sample Output

    3
    2
    3
    4
    4

    题意:一棵树,问某一个点能够走的不重复点的最长的路径是多少;
    思路:先随便找一个点跑一遍树的直径,然后直径的两头各跑一遍dfs
    #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 = 40010;
    
    struct Edge {
        int u,v,next,len;
    }edge[maxn];
    
    int n;
    int tot;
    int head[maxn];
    bool vis[maxn];
    void addedge(int u,int v,int w)
    {
        edge[tot].u = u;
        edge[tot].v = v;
        edge[tot].len = w;
        edge[tot].next = head[u];
        head[u] = tot++;
    }
    int dfs(int start,int d[])
    {
        for(int i = 1;i <= n;i++)
            d[i] = INF;
        memset(vis,false,sizeof vis);
        d[start] = 0;
        vis[start] = true;
        queue<int>que;
        que.push(start);
        int maxx = 0;
        int pos;
        while(!que.empty())
        {
            int p = que.front();
            que.pop();
            vis[p] = false;;
            if(maxx < d[p])
            {
                maxx = d[p];
                pos = p;
            }
            for(int i=head[p];i!=-1;i=edge[i].next)
            {
                int v = edge[i].v;
                if(d[v] > d[p] + edge[i].len)
                {
                    d[v] = d[p] + edge[i].len;
                    if(!vis[v])
                    {
                        que.push(v);
                        vis[v] = true;
                    }
                }
            }
        }
        return pos;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            int d1[maxn],d2[maxn];
            memset(head,-1,sizeof head);
            tot = 0;
            for(int u = 2;u <= n;u++)
            {
                int v,w;
                scanf("%d %d",&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            int st = dfs(1,d1);
            int en = dfs(st,d1);
            dfs(en,d2);
            for(int i=1;i<=n;i++)
                printf("%d
    ",max(d1[i],d2[i]));
        }
    }
    View Code
  • 相关阅读:
    2018年9月28日CCPC秦皇岛站参赛总结
    数学:二次剩余与n次剩余
    数学:拓展Lucas定理
    BZOJ2301:莫比乌斯反演+二维容斥解决GCD范围计数
    数学:莫比乌斯反演-约数个数和
    数学:莫比乌斯反演-GCD计数
    关于cnblogs至github上blog的搬迁
    友链——一群dalao
    折半搜索(meet in the middle)
    关于爆搜
  • 原文地址:https://www.cnblogs.com/smallhester/p/10310732.html
Copyright © 2011-2022 走看看