PTA甲级 1021 Deepest Root (25分) DFS

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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

生词如下:

PS:题目我基本上看懂了.

但是不会做!!!!!.还是看的算法笔记才懂.

那个定理的证明太麻烦了.没有看懂.

acyclic 非周期的

题目大意:

给你一棵树,问你选哪个结点才能让这颗树的高度最大.

如果有多个结点都可以,那么就按照升序排列.

思路如下:

PS:本题思路来自于算法笔记

① 这个树可能是不连通的,连通不连通可以通过查并集.当然也可以通过树的遍历

② 由于题目保证只有N-1条边,因此能确定是一棵树,下面的任务就是选择合适的根结点.使得树的高度最大.

下面是一个可以证明的定理:

先任意选择一个结点,从该结点出发开始遍历整课树,获取可以到达的最深的结点(记为结点集合B) 这样集合A与集合B的并集即为所求的使树高最大的根结点.

测试点1和2出错的原因

测试点1出错是n=1的时候

意思就是输入

1

最后输出也要是1

1

然后就是测试点2

就是我们一定要数连接块的个数.

算法笔记的代码如下:

vector

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 100010;
vector<int> G[N];		//邻接表
bool isRoot[N];			//记录每个结点是否作为某个集合的根结点
int father[N];			//查并集的老朋友
int findFather(int x) {	//查找x所在的根结点
	int a = x;
	while (x != father[x]) {
		x = father[x];
	}
	//路径压缩
	while (a != father[a]) {
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}
void Union(int a, int b) {	//合并a和b所在的集合
	int faA = findFather(a);
	int faB = findFather(b);
	if (faA != faB) {
		father[faA] = faB;
	}
}
void init(int n) {					//并查集初始化
	for (int i = 1; i <= n; ++i) {
		father[i] = i;
	}
}
int calBlock(int n) {		//计算连通块的个数
	int Block = 0;
	for (int i = 1; i <= n; ++i) {
		isRoot[findFather(i)] = true;		//i的根结点是findFather(i)
	}
	for (int i = 1; i <= n; ++i) {
		Block += isRoot[i];		//累加根结点的个数
	}
	return Block;
}
int maxH = 0;		//最大高度
vector<int> temp, Ans;			//temp临时存放DFS的最远结点结果,Ans保存答案
//DFS函数,u为当前访问结点编号,Height为当前树高,pre为u的父亲结点
void DFS(int u, int Height, int pre) {
	if (Height > maxH) {		//如果获得了更大的树高
		temp.clear();			//清空temp
		temp.push_back(u);		//将当前结点u加入temp中
		maxH = Height;
	}
	else if (Height == maxH) {	//将当前结点u加入到temp中
		temp.push_back(u);		//将当前结点加入temp中
	}
	for (int i = 0; i < G[u].size(); ++i) {			//遍历u的所有子结点
		//由于邻接表中存放的无向图,因此需要跳过回去的边
		if (G[u][i] == pre)continue;
		DFS(G[u][i], Height + 1, u);				//访问子结点
	}
}
int main() {
	int a, b, n;
	scanf("%d", &n);
	init(n);						//查并集初始化
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &a, &b);
		G[a].push_back(b);			//边a->b
		G[b].push_back(a);			//边b->a
		Union(a, b);				//合并a和b所在的集合
	}

	int Block = calBlock(n);		//计算集合数目
	if (Block != 1) {				//不止一个连通块
		printf("Error: %d components\n", Block);
	}
	else {
		DFS(1, 1, -1);				//从1号结点开始DFS,初始高度为1
		Ans = temp;					//temp为集合A,赋给Ans
		DFS(Ans[0], 1, -1);			//从任意一个根结点开始遍历
		for (int i = 0; i < temp.size(); ++i) {
			Ans.push_back(temp[i]);	//此时temp为集合b,将其加入ans中.
		}
		sort(Ans.begin(), Ans.end());	//按编号从小到大排序
		printf("%d\n", Ans[0]);
		for (int i = 1; i < Ans.size(); ++i) {
			if (Ans[i] != Ans[i - 1]) {	//重复编号不输出
				printf("%d\n", Ans[i]);
			}
		}
	}
	return 0;
}

下面的代码时我用set改写的方法

set

#include<iostream>
#include<set>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max = 10005;
set<int> temp, Ans;
vector<int> G[Max];
bool vis[Max];
int n,maxn=0;
void DFS_Trave(int v) {			//常见的DFS_Trave,遍历算法
	vis[v] = true;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!vis[G[v][i]])	DFS_Trave(G[v][i]);
	}
}
int Block() {					//计算连接快	
	int block = 0;
	for (int i = 1; i <= n ; ++i) {
		if (!vis[i]) {
			DFS_Trave(i);
			++block;
		}
	}
	return block;
}
void DFS_depth(int u,int depth,int pre) {
	if (depth > maxn) {
		temp.clear();
		temp.insert(u);
		maxn = depth;
	}
	else if (depth == maxn)	temp.insert(u);
	for (int i = 0; i < G[u].size(); ++i) {			//遍历u的所有子结点,由于邻接表中存放无向图
		if (G[u][i] == pre)	continue;	//因此需要跳回回去的边
		DFS_depth(G[u][i], depth + 1, u);			//访问子结点
	}
}
int main(void) {
	int  a, b;
	scanf("%d", &n);
	for (int i = 0; i < n-1; ++i) {
		scanf("%d%d", &a, &b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	memset(vis, false, sizeof(vis));
	int block = Block();
	if (block != 1&&block!=0)	printf("Error: %d components", block);
	else {
		DFS_depth(1, 1, -1);
		Ans = temp;
		DFS_depth(*Ans.begin(), 1, -1);
		for (auto i : temp)	Ans.insert(i);
		for (auto i : Ans)	printf("%d\n", i);
	}
	return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值