Farthest Nodes in a Tree (II) LightOJ - 1257

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define MAX_N 300010
using namespace std;
vector<pair<int,int> >G[MAX_N];
int dis1[MAX_N],dis2[MAX_N],vis[MAX_N];
int N;
void bfs(int s,int &t,int dis[])
{
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(s); // 把起点入队
    vis[s] = 1;
    dis[s] = 0;
    int maxx = -1;
    while(!Q.empty())
    {
        int e = Q.front();
        Q.pop();
        int sz = G[e].size();
        for(int i = 0; i < sz; i++)
        {
            int v = G[e][i].first;  // 当前到达的点
            int w = G[e][i].second; // 当前边的权值
            if(vis[v]) continue;
            dis[v] = dis[e] + w;
            if(dis[v] > maxx)
            {
                t = v;
                maxx = dis[v];
            }
            vis[v] = 1;
            Q.push(v);
        }
    }
    return ;
}
void Init()
{
    for(int i = 0; i < N; i++)
        G[i].clear();
}
int main()
{
    int T,cas,a,b,c;
    scanf("%d",&T);
    for(cas = 1; cas <= T; cas++)
    {
        scanf("%d",&N);
        Init();
        for(int i = 0; i < N-1; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            G[a].push_back(make_pair(b,c));    // 等价于 g[a][b] = c;
            G[b].push_back(make_pair(a,c));   //  g[b][a] = c;
       }
        int u,v,x;
        bfs(0,u,dis1);
        bfs(u,v,dis1);
        bfs(v,x,dis2);
        printf("Case %d:\n",cas);
        for(int i = 0; i < N; i++)
        {
            printf("%d\n",max(dis1[i],dis2[i]));
        }
    }
    return 0;
}

求无环图(树)上每一个节点可以到达的最远距离

(转)证明方法

主要是利用了反证法:
假设 s-t这条路径为树的直径,或者称为树上的最长路
现有结论,从任意一点u出发搜到的最远的点一定是s、t中的一点,
然后再从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路
证明:
1
设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T
则dis(u,T) >dis(u,s) 且 dis(u,T)>dis(u,t)   则最长路不是s-t了,与假设矛盾
2
设u不为s-t路径上的点 首先明确,假如u走到了s-t路径上的一点,那么接下来的路径肯定都在s-t上了,
而且终点为s或t,在1中已经证明过了
所以现在又有两种情况了:
1:u走到了s-t路径上的某点,假设为X,最后肯定走到某个端点,假设是t
   则路径总长度为dis(u,X)+dis(X,t)
2:u走到最远点的路径u-T与s-t无交点,则dis(u-T) > dis(u,X)+dis(X,t);
   显然,如果这个式子成立,
   则dis(u,T)+dis(s,X)+dis(u,X)>dis(s,X)+dis(X,t)=dis(s,t)
   最长路不是s-t矛盾

时间: 2024-10-08 09:33:23

Farthest Nodes in a Tree (II) LightOJ - 1257的相关文章

lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstrin

LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)

题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,于是果断树分治. 对于每次分治的子树,计算其所有结点到根的距离:对于每个结点,找到另一个离根最远的且与该结点路径过根的结点,二者的距离和就是这个点在过这棵子树的根能到的最远距离. 现在问题就是怎么比较快地找到这另一个最远距离的点..两点路径过根,说明两点间不存在一点是另一点的祖先..我一开始还想用DFS序+线段树来着..想了想,想出了线性的算法: 记录每个结点属于根的哪个儿子,把当前

Lightoj 1094 - Farthest Nodes in a Tree 【树的直径 裸题】

1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 undire

lightoj1094 - Farthest Nodes in a Tree

1094 - Farthest Nodes in a Tree   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 undi

light oj 1094 Farthest Nodes in a Tree(树的直径模板)

1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 undire

lightoj-1094 Farthest Nodes in a Tree(求树的直径)

1094 - Farthest Nodes in a Tree PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBGiven 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 undirect

Farthest Nodes in a Tree (求树的直径)

题目链接,密码:hpu Description 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 a

Farthest Nodes in a Tree LightOJ - 1094

树上最远点对(树的直径) 树形dp,类似要求出离任意一个点最远的点的方法. 最长路一定是经过树上的某一个节点的.ans[i]表示i点往下走的最长路.每个节点处都统计一下向下的最长和次长链,将两条拼接起来去更新答案. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 typedef long long L

SPOJ QTREE2 Query on a tree II

Query on a tree II Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: QTREE264-bit integer IO format: %lld      Java class name: Main You are given a tree (an undirected acyclic connected graph) with N nodes,