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:5 1 2 1 3 1 4 2 5Sample Output 1:
3 4 5Sample Input 2:
5 1 3 1 4 2 5 3 4Sample Output 2:
Error: 2 components
参照算法笔记P343.
1.连通分量问题用并查集。
2.选择合适的根结点,使得树的高度最大。具体做法:先任意选择一个结点,从该结点开始遍历整颗树,获得能达到的最深的顶点(记为结点集合A)。然后从集合A中任意一个结点出发遍历整棵树,获得能达到的最深的顶点(记为集合B)。这样从集合A与集合B的并集即为所求的使树高度最大的根结点。
对vector排序:sort(ans.begin(),ans.end());
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>adj[10001];//邻接表
vector<int>temp,ans;
int tree[10001];
int visit[10001];
int maxH;
int findroot(int x){
if(tree[x]==-1)return x;
return tree[x]=findroot(tree[x]);
}
void init(int n){
int i;
for(i=1;i<=n;i++){
tree[i]=-1;
visit[i]=0;
}
}
void dfs(int s,int high){
if(high>maxH){
temp.clear();
temp.push_back(s);
maxH=high;
}
else if(high==maxH){
temp.push_back(s);
}
visit[s]=1;
int i;
for(i=0;i<adj[s].size();i++){
if(visit[adj[s][i]]==0){
dfs(adj[s][i],high+1);
}
}
}
int main(){
int n,i,c1,c2;
scanf("%d",&n);
init(n);
for(i=1;i<n;i++){
scanf("%d %d",&c1,&c2);
adj[c1].push_back(c2);
adj[c2].push_back(c1);
c1=findroot(c1);
c2=findroot(c2);
if(c1!=c2){
tree[c1]=c2;
}
}
int cou=0;
for(i=1;i<=n;i++){
if(tree[i]==-1){
cou++;
}
}
if(cou!=1){
printf("Error: %d components",cou);
}
else{
dfs(1,1);
ans=temp;
init(n);
dfs(temp[0],1);
for(i=0;i<temp.size();i++){
ans.push_back(temp[i]);
}
sort(ans.begin(),ans.end());
printf("%d\n",ans[0]);
for(i=1;i<ans.size();i++){
if(ans[i]!=ans[i-1]){
printf("%d\n",ans[i]);
}
}
}
}
#include<stdio.h>
#include<vector>
#include<set>
using namespace std;
int tree[10010];
int visit[10010];
vector<int>adj[10010];
set<int>temp,ans;
int deepest=0;
void init(int n){
int i;
for(i=1;i<=n;i++){
tree[i]=-1;
visit[i]=0;
}
}
int findroot(int x){
if(tree[x]==-1)return x;
return tree[x]=findroot(tree[x]);
}
void dfs(int s,int depth){
visit[s]=1;
if(depth>deepest){
deepest=depth;
temp.clear();
temp.insert(s);
}
else if(depth==deepest){
temp.insert(s);
}
int i;
for(i=0;i<adj[s].size();i++){
int next=adj[s][i];
if(visit[next]==0){
dfs(next,depth+1);
}
}
}
int main(){
int n,i,c1,c2,fa1,fa2;
scanf("%d",&n);
init(n);
for(i=0;i<n-1;i++){
scanf("%d %d",&c1,&c2);
adj[c1].push_back(c2);
adj[c2].push_back(c1);
fa1=findroot(c1);
fa2=findroot(c2);
if(fa1!=fa2){
tree[fa1]=fa2;
}
}
int cou=0;
for(i=1;i<=n;i++){
if(tree[i]==-1){
cou++;
}
}
if(cou>1){
printf("Error: %d components",cou);
}
else{
dfs(1,1);
ans=temp;
init(n);
set<int>::iterator it=temp.begin();
dfs(*it,1);
it=temp.begin();
for(;it!=temp.end();it++){
ans.insert(*it);
}
it=ans.begin();
for(;it!=ans.end();it++){
printf("%d\n",*it);
}
}
}