#1224: Racing
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://hihocoder.com/problemset/problem/1224
Description
Gensokyo has a racetrack. There are n locations in the racetrack. There is also a one-way road between locations.
These roads make the racetrack a structure of an outward tree. In other words, the road connects the n sites into a tree with a root. And all the edges are from the father pointing to the child.
Because the fragrance likes to stimulate, every time she goes to the circuit will go from the root node, choose the longest path to play.
But now the scent feels the longest path is too short, and she intends to create a new road in the racetrack that will make the longest path the longest.
At the same time, if the road formed a ring, then there may be traffic accidents, so the fragrance of new roads can not lead to the emergence of the ring.
Can you help to figure out the longest path after a new road? Delicate fragrance know that the root node must be number 1th point.
Input
A number N of a row, representing the number of places.
Next N-1 line, two numbers a and b per line, indicates a one-way path from point A to to B. All points are labeled from 1 to N.
Data range:
n<=100000.
The output row represents the longest path after creating a new edge. Sample Input
5
1 2
2 3
1 4
4 5
Sample Output4
HINT
Test instructions
Exercises
Http://media.hihocoder.com/contests/challenge14/hihoCoder-Challenge14-Solution.pdf
The official puzzle is on top.
First we first DFS out the longest chain, and then we're in the DFS out of each point can extend the longest how much
And then the final answer is the length of the longest chain + the longest extension of the number.
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>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 120051#defineMoD 10007#defineEPS 1e-9intNum;//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=~0u>>1; inline ll read () {ll x=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;}//**************************************************************************************Vector<int>E[MAXN];intFA[MAXN];intDEP[MAXN];intKEY[MAXN];voidDfsintXintDeep ) {Dep[x]=Deep ; for(intI=0; I<e[x].size (); i++) { intv=E[x][i]; if(Key[v])Continue; FA[V]=x; DFS (V,deep+1); }}intMain () {intn=read (); for(intI=1; i<n;i++) { intA=read (), b=read (); E[a].push_back (b); } DFS (1,1); intmx=0, BJ; for(intI=1; i<=n;i++) { if(dep[i]>mx) {mx=Dep[i]; BJ=i; } }//find the longest chain intans=MX; intx =BJ; while(x!=1) {Key[x]=1; X=Fa[x]; } key[1]=1; for(intI=0; i<=n;i++) { if(Key[i]) {DFS (I,0); }} MX=0; for(intI=1; i<=n;i++) { if(!key[i]) MX=Max (mx,dep[i]); } ans= Max (0, ans+mx-1); printf ("%d\n", ans); return 0;}
Hihocoder #1224: Racing Dfs