Diameter of spfa/Tree of tyvj:1520 tree

Source: Internet
Author: User

tyvj:1520 tree diameter time limit:1 Sec Memory Limit:131072kib
submit:9619 solved:3287 Topic Connection

http://www.tyvj.cn/p/1520

The diameter of the description tree, which is the distance from the two most distant nodes in the tree. The distance between each of the two adjacent nodes is 1, i.e. the distance between the father's node and the son node or the son node and the parent-child node is 1. Interestingly, from any node of a tree, go to the farthest node B, and then from Node B, the farthest distance that can go is the diameter of the tree. The distance between two adjacent nodes in the tree is 1. Your task is: Given a tree, ask for the distance from the two most distant nodes in the tree. Input inputs A total of n rows
The first line is a positive integer n, which represents the node number of this tree
The next n-1 line, with three positive integers per line, is a,b,w. Indicates that there is an edge between Node A and Node B, with a length of W
Data guarantee must be a tree, do not have to be wrong output output a Total row
The first line is only one number, indicating the farthest distance of the tree Sample Input41 2 1 3 1 4 Sample Output -hint10% data to meet 1<=n<=5
40% of data Meet 1<=n<=100
100% of data meet 1<=n<=10000 1<=a,b<=n 1<=w<=10000

Exercises

Take a point to SPFA, and then find the farthest point from this point, and then a SPFA, and then the point to get the farthest point, then this distance, is the diameter of the tree

Don't ask me how to know this conclusion, I do not know how I know ...

Remember just fine ...

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9//const int INF=0X7FFFFFFF; //infinitely LargeConst intinf=0x3f3f3f3f;/*inline ll read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;}*///**************************************************************************************inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}structedge{intx, y;};structnode{intx, y;}; Vector<edge>KISS[MAXN];voidAdd_edge (intAintBintc)    {Kiss[a].push_back (Edge) {b,c}); Kiss[b].push_back (Edge) {a,c});}intVIS[MAXN];intDIS[MAXN];intMain () {intN; N=read ();  for(intI=0; i<n-1; i++)    {        intA,b,c; A=read (), B=read (), c=read ();    Add_edge (A,B,C); } Queue<int>Q; Q.push (1);  for(intI=1; i<=n;i++) Dis[i]=inf; dis[1]=0; intans=0; intans1=0; memset (Vis,0,sizeof(VIS)); vis[1]=1;  while(!Q.empty ()) {        intnow=Q.front (); Vis[now]=0;        Q.pop ();  for(intI=0; I<kiss[now].size (); i++)        {            intnext=kiss[now][i].x; //cout<<dis[now]+kiss[now][i].y<< "" <<dis[next]<<endl;            if(dis[now]+kiss[now][i].y<Dis[next]) {Dis[next]=dis[now]+kiss[now][i].y; if(vis[next]==0) {Vis[next]=1;                Q.push (next); }            }        }    }     for(intI=1; i<=n;i++)    {        if(dis[i]!=inf&&dis[i]>ans1) {ans=i; Ans1=Dis[i]; }    }     for(intI=1; i<=n;i++) Dis[i]=inf; Dis[ans]=0; memset (Vis,0,sizeof(VIS));    Q.push (ANS); Vis[ans]=1; Ans=0; Ans1=0;  while(!Q.empty ()) {        intnow=Q.front (); Vis[now]=0;        Q.pop ();  for(intI=0; I<kiss[now].size (); i++)        {            intnext=kiss[now][i].x; if(dis[now]+kiss[now][i].y<Dis[next]) {Dis[next]=dis[now]+kiss[now][i].y; if(vis[next]==0) {Vis[next]=1;                Q.push (next); }            }        }    }     for(intI=1; i<=n;i++)    {        if(dis[i]!=inf) ans1=Max (ans1,dis[i]); } cout<<ans1<<Endl;}

Diameter of spfa/Tree of tyvj:1520 tree

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.