PAT 甲级 1021  Deepest Root

本文详细解析了PAT高级水平考试中的一道经典题目——寻找最深深根问题,通过深度优先搜索(DFS)和广度优先搜索(BFS)两种算法进行解答,并提供了完整的代码实现,同时对比了两种算法的效率,强调了数据集规模对运行时间的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (≤10​4​​) 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值