1021. Deepest Root (25)
来源:互联网 发布:ubuntu vi命令 编辑:程序博客网 时间:2024/06/10 17:47
1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is calledthe deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes' numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print "Error: K components" where K is the number of connected components in the graph.
Sample Input 1:51 21 31 42 5Sample Output 1:
345Sample Input 2:
51 31 42 53 4Sample Output 2:
Error: 2 components
#include<iostream>using namespace std;struct G{int node;G* Next;} graph[10001];int count=0;struct disj{int no;disj* father;}dis[10001];disj* find(disj* a){if(a->father!=NULL)return a->father=find(a->father);elsereturn a;}void unino(disj* a,disj* b){if(find(a)==find(b))count++;elsea->father=b;}void add(int a,int b){G* t=&graph[a];G* te;while(t->Next!=NULL){t=t->Next;if(t->node==b)return;}if(a>b)unino(&dis[a],&dis[b]);te=new G;te->Next=NULL;te->node=b;t->Next=te;}int isread[10001];int DFS(int a,int N){isread[a]=1;G* t=&graph[a];int max=-1,v;while(t->Next!=NULL){t=t->Next;if(isread[t->node]==0){v=DFS(t->node,N);if(v>max)max=v;}}return max+1;}int main(){int N,max=-1;cin>>N;int j;int *h=new int[N];int i,temp1,temp2,temp3;for(i=0;i<10001;i++){graph[i].Next=NULL;graph[i].node=i;dis[i].father=NULL;dis[i].no=i;}for(i=0;i<N-1;i++){cin>>temp1>>temp2;add(temp1,temp2);add(temp2,temp1);}if(count!=0){cout<<"Error: "<<count+1<<" components";return 0;}for(i=1;i<N+1;i++){h[i]=-1;if(graph[i].Next!=NULL&&graph[i].Next->Next==NULL){for(j=1;j<N+1;j++)isread[j]=0;h[i]=DFS(i,N);}if(h[i]>max)max=h[i];}for(i=1;i<N+1;i++){if(h[i]==max)cout<<i<<endl;}}
感想:1.判断联通环的个数,只要判断某项中两点都已经出现过的次数即可,数组即可实现,我只是为了复习一下并查集。
2。算深度建议用DFS,我也想到了给每个边一个值,初始值为0,每次加入一条边就用DFS给能增加深度的点深度+1,直接DFS更加简便,而且其实只要对只有一条边的点进行DFS就可以了。
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- codevs4373&洛谷1866 滑动窗口
- 实现shell四则运算
- 嵌入式Linux-内存管理基础
- NYOJ77 - 开灯问题
- android面试笔试题详解
- 1021. Deepest Root (25)
- Qt for Windows:Qt 5.8.0 MinGW 静态编译版本(包含OpenSSL)
- SPOJ - PUTNIK(想法+dp,好题)
- NYOJ100 - 1的个数
- 三星事件调查结果新鲜奉上 全球多家媒体共同出席发布会
- 斐波那契数列 Java实现
- mysql客户端工具
- hibernate的低级错误
- NYOJ96 - n-1位数