PAT甲级 -- 1021 Deepest Root (25 分)

本文探讨了在一个连通且无环的图中寻找最深根节点的问题,即那些能够生成最高树形结构的根节点。文章详细介绍了通过深度优先搜索(DFS)算法来判断图的连通性和确定最深根节点的过程,同时处理了图不是树的情况。

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

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 (≤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 componentswhere 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

我的思路:

1. 先判断连同分量,若  !=1 则输出Error,若 == 1, 此时再求最深root 

2. dfs递归是我的短板 ,逻辑不太好写,自己写了一半就被自己绕晕... 后面补!

先上不完整代码:

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;


const int MAXN = 10010;
bool visit[MAXN];
int G[MAXN][MAXN];


int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;

void dfsConnected(int no)
{
	visit[no] = true;
	for (int v = 1; v <= n; v++)
	{
		if (visit[v] == false && G[no][v] == 1)
		{
			dfsConnected(v);
		}
	}
}


void dfsRoot(int root,int depth)
{
	if (depth < maxDepth)
	{
		maxRoot.clear();
		maxRoot.push_back(root);
		maxDepth = depth;
	}else if(depth == maxDepth)
	{
		maxRoot.push_back(root);
	}

	visit[root] = true;
	for (int i = 1; i <= n; i++)
	{
		if (G[root][i] == 1 && visit[i] == false)
		{
			dfsRoot(i,depth+1);
		}
	}
}

int main()
{
	fill(G[0], G[0]+MAXN*MAXN, 0);
	scanf("%d", &n);
	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		G[u][v] = G[v][u] = 1;
	}
	fill(visit, visit+MAXN, false);
	for (int i = 1; i <= n; i++)
	{
		if(visit[i] == false)
		{
			dfsConnected(i);
			k++;
		}
	}

	if (k == 1)
	{
		maxRoot.clear();
		fill(visit, visit+MAXN, false);
		dfsRoot(1, 0);
		
		for (int i = 0; i < maxRoot.size(); i++)
		{
			printf("%d\n", maxRoot[i]);
		}

		
	}else
	{
		printf("Error: %d components", k);
	}
	return 0;
}

补代码,23分:

1. 增加了在 k == 1 处   增加了循环

2. 一个测试点显示段错误,考虑是不是数组问题,改成vector变成了内存超限

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;


const int MAXN = 10010;
bool visit[MAXN];
int G[MAXN][MAXN];


int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;

void dfsConnected(int no)
{
	visit[no] = true;
	for (int v = 1; v <= n; v++)
	{
		if (visit[v] == false && G[no][v] == 1)
		{
			dfsConnected(v);
		}
	}
}


void dfsRoot(int root,int depth)
{
	if (depth > maxDepth)
	{
		maxRoot.clear();
		maxRoot.push_back(root);
		maxDepth = depth;
	}else if(depth == maxDepth)
	{
		maxRoot.push_back(root);
	}

	visit[root] = true;
	for (int i = 1; i <= n; i++)
	{
		if (G[root][i] == 1 && visit[i] == false)
		{
			dfsRoot(i,depth+1);
		}
	}
}

int main()
{
	fill(G[0], G[0]+MAXN*MAXN, 0);
	scanf("%d", &n);
	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		G[u][v] = G[v][u] = 1;
	}
	fill(visit, visit+MAXN, false);
	for (int i = 1; i <= n; i++)
	{
		if(visit[i] == false)
		{
			dfsConnected(i);
			k++;
		}
	}

	if (k == 1)
	{
		for(int i = 1; i <= n; i++)
		{
			fill(visit, visit+MAXN, false);
			dfsRoot(i, 1);
		}
		
		set<int> s;
		
		for (int i = 0; i < maxRoot.size(); i++)
		{
			s.insert(maxRoot[i]);
			
		}

		for (auto it = s.begin(); it != s.end(); it++)
		{
			printf("%d\n", *it);
		}

		
	}else if(k>=2)
	{
		printf("Error: %d components", k);
	}
	return 0;
}



//改用vector
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;


const int MAXN = 10001;   
bool visit[MAXN];
//开太大内存超限,改用vector
//int G[MAXN][MAXN];
vector<vector<int>> G;


int n; // 结点个数
int k; //连同分量个数
vector<int> maxRoot;
int maxDepth = 0;

void dfsConnected(int no)
{
	visit[no] = true;
	for (int v = 0; v < G[no].size(); v++)
	{
		if (visit[G[no][v]] == false)
		{
			dfsConnected(G[no][v]);  //注意参数的变化
		}
	}
}


void dfsRoot(int root,int depth)
{
	if (depth > maxDepth)
	{
		maxRoot.clear();
		maxRoot.push_back(root);
		maxDepth = depth;
	}else if(depth == maxDepth)
	{
		maxRoot.push_back(root);
	}

	visit[root] = true;
	for (int i = 0; i < G[root].size(); i++)
	{
		if (visit[G[root][i]] == false)
		{
			dfsRoot(G[root][i],depth+1);
		}
	}
}

int main()
{
	//fill(G[0], G[0]+MAXN*MAXN, 0);
	scanf("%d", &n);
	G.resize(n+1);
	for (int i = 0; i < n-1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}

	fill(visit, visit+MAXN, false);

	for (int i = 1; i <= n; i++)
	{
		if(visit[i] == false)
		{
			dfsConnected(i);
			k++;
		}
	}

	if (k == 1)
	{
		for(int i = 1; i <= n; i++)
		{
			fill(visit, visit+MAXN, false);
			dfsRoot(i, 1);
		}
		
		set<int> s;
		
		for (int i = 0; i < maxRoot.size(); i++)
		{
			s.insert(maxRoot[i]);
			
		}

		for (auto it = s.begin(); it != s.end(); it++)
		{
			printf("%d\n", *it);
		}

		
	}else if(k >= 2)
	{
		printf("Error: %d components", k);
	}
	return 0;
}

贴柳神的代码学习:

看了一下,思路应该是:

1. 在判断连通性的时候,找到最深的点,加入集合s

2. 从最深的点出发,看最远到达的点,加入s。

 

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int n, maxheight = 0;
vector<vector<int>> v;
bool visit[10010];
set<int> s;
vector<int> temp;
void dfs(int node, int height) {
	if(height > maxheight) {
		temp.clear();
		temp.push_back(node);
		maxheight = height;
	} else if(height == maxheight){
		temp.push_back(node);
	}
	visit[node] = true;
	for(int i = 0; i < v[node].size(); i++) {
		if(visit[v[node][i]] == false)
			dfs(v[node][i], height + 1);
	}
}
int main() {
	scanf("%d", &n);
	v.resize(n + 1);
	int a, b, cnt = 0, s1 = 0;
	for(int i = 0; i < n - 1; i++) {
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i = 1; i <= n; i++) {
		if(visit[i] == false) {
			dfs(i, 1);
			if(i == 1) {
				if (temp.size() != 0) s1 = temp[0];
				for(int j = 0; j < temp.size(); j++)
					s.insert(temp[j]);
			}
			cnt++;
		}
	}
	if(cnt >= 2) {
		printf("Error: %d components", cnt);
	} else {
		temp.clear();
		maxheight = 0;
		fill(visit, visit + 10010, false);
		dfs(s1, 1);
		for(int i = 0; i < temp.size(); i++)
			s.insert(temp[i]);
		for(auto it = s.begin(); it != s.end(); it++)
			printf("%d\n", *it);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值