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 called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) 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 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
题目大意:找到使一棵树深度最大的所有根节点,若不为树,则输出连通分量个数。
先一遍dfs得到深度最大的所有点的集合,再从集合中随便找一个节点,从该点出发再做一遍dfs,得到另一个深度最大的所有点的集合,两个集合的并集即为结果。
也可以直接暴力法:对每个节点做一遍dfs,将得到的深度最大的节点记录下来。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include <algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
const int maxn=10010;
bool visit[maxn]={false};
bool v[maxn][maxn]={false};
vector<vector<int>> e;
int n;
set<int>res;
vector<int>temp;
int maxheight=0;
void dfs(int now,int height){
visit[now]=true;
if(height>maxheight){
maxheight=height;
temp.clear();
temp.push_back(now);
}
else if(height==maxheight){
temp.push_back(now);
}
for(int i=0;i<e[now].size();i++){
if(visit[e[now][i]]==false){
dfs(e[now][i],height+1);
}
}
}
int main()
{
scanf("%d",&n);
e.resize(n+1);
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}
int cnt=0;
int now;
for(int i=1;i<=n;i++){
if(visit[i]==false){
dfs(i,1);
cnt++;
}
if(i==1){
if(temp.size()>0) {
now=temp[0];
for(int j=0;j<temp.size();j++){
res.insert(temp[j]);
}
temp.clear();
}
}
}
if(cnt>1){
printf("Error: %d components\n",cnt);
}
else{
for(int i=1;i<=n;i++) visit[i]=false;
dfs(now,1);
for(int j=0;j<temp.size();j++){
res.insert(temp[j]);
}
for(set<int>::iterator it=res.begin();it!=res.end();it++){
printf("%d\n",*it);
}
}
return 0;
}
暴力:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
const int maxn=10010;
const int inf=0x3fffffff;
bool visit[maxn];
vector<vector<int>> edge;
int maxheight=0,temp=0;
vector<int>res;
void dfs(int now,int height){
visit[now]=true;
if(height>temp){
temp=height;
}
for(int i=0;i<edge[now].size();i++){
if(visit[edge[now][i]]==false) dfs(edge[now][i],height+1);
}
}
int main()
{
int n,a,b;
scanf("%d",&n);
edge.resize(n+1);
for(int i=1;i<n;i++){
scanf("%d %d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
for(int i=1;i<=n;i++){
fill(visit+1,visit+n+1,false);
int sum=1;
bool flag=false;
dfs(i,1);
for(int j=1;j<=n;j++){
if(visit[j]==false){
dfs(j,1);
sum++;
flag=true;
}
}
if(flag){
printf("Error: %d components",sum);
return 0;
}
if(temp>maxheight){
res.clear();
res.push_back(i);
maxheight=temp;
}
else if(temp==maxheight){
res.push_back(i);
}
temp=0;
}
for(int i=0;i<res.size();i++){
printf("%d\n",res[i]);
}
return 0;
}