zoukankan      html  css  js  c++  java
  • LightOJ1094

    http://lightoj.com/volume_showproblem.php?problem=1094

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

    Input

    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

    Output

    For each case, print the case number and the maximum distance.

    Sample Input

    Output for Sample Input

    2

    4

    0 1 20

    1 2 30

    2 3 50

    5

    0 2 20

    2 1 10

    0 3 29

    0 4 50

    Case 1: 100

    Case 2: 80

    树的直径典型题目 

    树的直径是让求给你一棵树 所有节点和他们的距离然后求这棵树上距离最远的两个点之间的距离

    分析:

    先是任意找一个点,然后bfs找到离他最远距离的点,把这个点标记起来

    然后用这个点再bfs一次找到直径

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    #include <vector>
    #include <queue>
    
    using namespace std;
    #define N 30005
    int ans,Max,Index;
    int head[N],vis[N],dis[N];
    struct node
    {
        int v,f,next;
    }e[N*4];
    
    void Inn()
    {
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        ans=Index=0;
    }
    void Add(int u,int v,int f)
    {
        e[ans].v=v;
        e[ans].f=f;
        e[ans].next=head[u];
        head[u]=ans++;
    }
    
    void bfs(int u)
    {
        Max=0;
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        queue<int>Q;
        Q.push(u);
        vis[u]=1;
        while(!Q.empty())
        {
            int p,q;
            p=Q.front();
            Q.pop();
            for(int i=head[p];i!=-1;i=e[i].next)
            {
                q=e[i].v;
                if(!vis[q])
                {
                    vis[q]=1;
                    Q.push(q);
                    dis[q]=dis[p]+e[i].f;
                    if(Max<dis[q])
                    {
                        Max=dis[q];
                        Index=q;
                    }
                }
            }
        }
    }
    
    int main()
    {
        int T,n,U,V,W,t=1;
        scanf("%d",&T);
        while(T--)
        {
            Inn();
            scanf("%d",&n);
            for(int i=1;i<n;i++)
            {
                scanf("%d %d %d",&U,&V,&W);
                Add(U,V,W);
                Add(V,U,W);
            }
            bfs(0);
            bfs(Index);
            printf("Case %d: %d
    ",t++,Max);
        }
        return 0;
    }
  • 相关阅读:
    P3973 [TJOI2015]线性代数
    P3168 [CQOI2015]任务查询系统
    二次剩余学习笔记
    URL注入攻击+知识星球资源整理
    我的网络安全架构知识点的总结(待完善)
    Sudo提权
    DNSlog平台各种利用姿势(盲注)
    Wireshark抓包理论加实操
    Fofa搜索技巧(理论加实践的整理)
    Windows留后门维持权限(其中包括详细的telnet改端口与连接)
  • 原文地址:https://www.cnblogs.com/linliu/p/5279145.html
Copyright © 2011-2022 走看看