1021 Deepest Root (25 point(s))
A graph which is connected and acyclic can be considered a tree. The hight 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
Experiential Summing-up
This question can be solved by DFS and BFS. It's just DFS faster than BFS. But the third test point cost more than 1000ms at least. Don't be surprised~ It's OK. Just because the data set or the number of the graph's vertex are so large.
(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6 _(:з」∠)_ )
Accepted Code
DFS:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=0x3fffffff;
const int maxn=10010;
int n;
vector<int> adj[maxn];
bool vis[maxn];
int deepest;
void DFS(int x,int depth)
{
vis[x]=true;
for(int i=0;i<adj[x].size();++i)
{
if(vis[adj[x][i]]==false)
DFS(adj[x][i],depth+1);
}
if(depth>deepest)
deepest=depth;
}
int main()
{
int s,e;
scanf("%d",&n);
for(int i=0;i<n-1;++i)
{
scanf("%d %d",&s,&e);
adj[s].push_back(e);
adj[e].push_back(s);
}
vector<int> ans;
int deep=-1;
int k=1;
for(int i=1;i<=n;++i)
{
memset(vis,0,sizeof(vis));
deepest=-1;
DFS(i,1);
if(deepest>deep)
{
ans.clear();
deep=deepest;
ans.push_back(i);
}
else if(deepest==deep)
{
ans.push_back(i);
}
for(int j=1;j<=n;++j)
{
if(vis[j]==false)
{
++k;
DFS(j,1);
}
}
if(k!=1)
break;
}
if(k!=1)
printf("Error: %d components\n",k);
else
{
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();++i)
printf("%d\n",ans[i]);
}
return 0;
}
BFS:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=0x3fffffff;
const int maxn=10010;
int n;
vector<int> adj[maxn];
bool vis[maxn];
int deepest;
struct node
{
int level;
int v;
node(int a,int b):v(a),level(b){}
};
void BFS(int x)
{
queue<node> q;
vis[x]=true;
q.push(node(x,1));
while(q.size())
{
node a=q.front();
if(a.level>deepest)
deepest=a.level;
q.pop();
for(int i=0;i<adj[a.v].size();++i)
{
if(vis[adj[a.v][i]]==false)
{
vis[adj[a.v][i]]=true;
q.push(node(adj[a.v][i],a.level+1));
}
}
}
}
int main()
{
int s,e;
scanf("%d",&n);
for(int i=0;i<n-1;++i)
{
scanf("%d %d",&s,&e);
adj[s].push_back(e);
adj[e].push_back(s);
}
vector<int> ans;
int deep=-1;
int k=1;
for(int i=1;i<=n;++i)
{
memset(vis,0,sizeof(vis));
deepest=-1;
BFS(i);
if(deepest>deep)
{
ans.clear();
deep=deepest;
ans.push_back(i);
}
else if(deepest==deep)
{
ans.push_back(i);
}
for(int j=1;j<=n;++j)
{
if(vis[j]==false)
{
++k;
BFS(j);
}
}
if(k!=1)
break;
}
if(k!=1)
printf("Error: %d components\n",k);
else
{
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();++i)
printf("%d\n",ans[i]);
}
return 0;
}