1021. Deepest Root (25) and check set &&dfs

Source: Internet
Author: User


1021. Deepest Root (25)
Time limit
Ms
Memory limit
65536 KB
Code Length Limitations
16000 B
Procedures for the award of questions
Standard
Author
CHEN, Yue

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now is supposed to find the root of the results in a highest tree. Such A root is called the deepest root.

Input Specification:

Each input file contains the one test case. The first line contains a positive an integer N (<=10000) which is the number of nodes, and hence the nodes is numbered from 1 to N. Then N-1 lines follow, each describes a edge by given the adjacent nodes ' numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root is isn't unique, print them in increasing order of their numbers. In case that the given graph isn't a tree, print "Error:k components" where K is the number of connected He graph.
Sample Input 1:

5
1 2
1 3
1 4
2 5

Sample Output 1:

3
4
5

Sample Input 2:

5
1 3
1 4
2 5
3 4

Sample Output 2:

Error:2 components

Submit Code


It's important to note that the graph is stored with adjacency tables if the adjacency matrix is timed out (sparse graph)

The first time a DFS is written with a return value, it is not very skilled

#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include < algorithm>using namespace Std;const int maxn=10001;bool vis[maxn];int a[maxn];int n;int cnt=0;vector<int> ma[    maxn];//adjacency Table int d[maxn];//calculates the maximum depth int dfs (int s) When this point is the root, and//dfs represents the maximum depth {int ans=0, which can be reached starting with s;    if (Vis[s]) return 0;//this point has been visited, it is apparent that the maximum depth of this point is 0 vis[s]=true;    int m=ma[s].size (); for (int i=0; i<m; i++) {if (!vis[ma[s][i]]) {int Tmp=dfs (ma[s][i]);//The maximum depth that can be reached with the current point as a starting point, also It is to find the maximum Ans=max (ans,tmp) that can be reached in the adjacency point of S, and the maximum depth of the//ans record is {}} return ans+1;//can go here to show that the depth of s can be added one, that is, S. The depth of the adjacent vertex, the maximum value, plus the S point, so is ans+1}void init (int n)//This is the initialization of the set {for (int i=0; i<=n; i++) a[i]=i;}    int find (int x)//And find the father of X, with path compression {if (a[x]!=x) A[x]=find (a[x]); return a[x];}    void Unio (int x,int y)//merge x, Y to together {x=find (x);    Y=find (y);    if (A[x]==a[y]) return; A[x]=y;}    int main () {int i,j,k,t; Freopen ("In.txt", "R", stdin);   cin>>n;    Init (n);        for (I=1; i<n; i++) {int s,e;        cin>>s>>e;        Unio (s,e);        Ma[s].push_back (e);    Ma[e].push_back (s);    } int sum=0;//determines the number of connected components for (i=1; i<=n; i++) {if (a[i]==i) sum++;        } if (sum>1) {printf ("Error:%d components\n", sum);    return 0;                } else for (i=1; i<=n; i++) {memset (vis,0,sizeof (VIS));            D[i]=dfs (i);    } int Maxv=-1;int index=0;    for (i=1;i<=n;i++) if (D[I]&GT;MAXV) {maxv=d[i];index=i;} for (j=1;j<=n;j++) if (D[j]==d[index]) printf ("%d\n", j);}





1021. Deepest Root (25) and check set &&dfs

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.