The longest road in the hihocoder1050-tree (deep Search)

Source: Internet
Author: User
Tags time limit

Topic Link: Portal

Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

On the last mention, Little Ho got a binary tree toy, which was connected by small balls and sticks, and in the process of dismantling it, Little Ho found that he could not only piece together a binary tree. can also be pieced together into a multi-fork tree-well, is actually more ordinary trees.

But anyway, little Ho's favorite toy has been upgraded, so he is more admiringly (in fact, the ball and sticks have what fun is it =). The toy tree in Small Ho's hands is now pieced together by n balls and N-1 sticks, all of which are marked by small ho with different numbers, and these numbers are all out of 1. Within the range of N, each stick is connected to two different balls, and there are no more than two different paths between any two spheres that can reach each other. In a word, it's a pretty fun toy.

But little hi saw small ho this way, think he was so addicted to it is not a good thing, so he thought to find some more questions let him think about it-but to small hi's level, nature is extremely easy.

So this day after breakfast, small hi will be holding a tree toy playing with the joy of Small Ho Road: You say you play this thing every day, I asked you a question, see if you can know. ”

Bad Little Ho didn't want to refuse.

"Then you continue to play, a time to return home when I do not call on you ~" Little hi Serious way.

Eh No, no, no, no, you said you said, I'm listening. "The Little Ho, who has always been used to opening follow patterns, cries out," he said.

Small hi satisfied nodded, then said: "This is right, my question is very simple, that is-you this tree which two nodes between the longest distance." Of course, the distance here refers to the number of sticks that go through one node to another. ”。

"Ah." Little Ho looked down at the toy tree in his hand and was puzzled. Tip One: The road always has a vertex, the path is no exception. input

Each test point (input file) has and has only one set of test data.

The first behavior of each set of test data is an integer n, meaning as described earlier.

Each group of test data of the line 2~n, each line describes a stick, wherein the I+1 Act Two integer Ai,bi, which represents the number of two balls connected by the root of the I stick.

For 20% of data, meet n<=10.

For 50% of data, meet n<=10^3.

For 100% of data, meet N<=10^5,1<=ai<=n, 1<=bi<=n

Little hi Tip: Those with array storage tree edge remember to open twice times the size Oh. Output

For each set of test data, output an integer ans, which indicates the distance between the two nodes farthest from the given tree. Sample input

8
1 2
1 3
1 4
4 5
3 6
6 7 7
8
Sample output
6

Problem-solving ideas: Find a node U (the origin) in the subtree of the longest path of the end of V, then V is a tree in one of the longest road endpoint (probably think about it), and then through the node v down to search the longest route is the longest tree, write two search OK.


#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <que ue> #include <set> #include <string> #include <stack> #include <algorithm> #include <map&
Gt  
#include <bitset> using namespace std;
typedef long Long LL;
const int N = 100008;
const int M = 100000000;
const int INF = 0X3FFFFFFF;
const int mod = 1E9+7;
Const double Pi = ACOs (-1.0);

Const double SM = 1e-9;
	struct edge{int nd;
Edge*next;
}M_EDGE[N*2];
Edge*head[n];

int vis[n],dist[n],ecnt;
	void Init () {ecnt = 0;
Fill (head, Head+n, (edge*) 0);
	} void Mkedge (int a, int b) {m_edge[ecnt].nd = b;
	M_edge[ecnt].next = Head[a];
Head[a] = m_edge+ecnt++;
	} void Dfs1 (int u) {vis[u] = 1;
	int ans = 0;
		for (edge*p = head[u]; p; p = p->next) {int v = p->nd;
			if (!vis[v]) {dist[v] = dist[u]+1;
		DFS1 (v);
	}}} int dfs2 (int u) {vis[u] = 1; for (edge*p = head[u]; p; p = p->next) {int v = P>nd;
		if (!vis[v]) {Dist[u] = max (Dist[u], DFS2 (v) +1);
}} return Dist[u];
	} int main () {int n,a,b;
		while (CIN >> N) {//if (n = = 1) {cout << 0 << Endl; continue;}
		Init ();
			for (int i = 0; i < n-1; ++i) {cin >> a >> b;
			Mkedge (A, b);
		Mkedge (B,a);
		} fill (Vis, vis+n, 0);
		Fill (Dist, dist+n, 0);
		DFS1 (1);
		int node,len=0;
				for (int i = 1; I <= n; ++i) {if (Len < dist[i]) {len = dist[i];
			node = i;
		}} fill (Vis, vis+n, 0);
		Fill (Dist, dist+n, 0);
		int ans = DFS2 (node);
	cout << ans << endl;
} return 0; }


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.