1021 Deepest Root(dfs,图的联通子集个数,树的深度)

1021 Deepest Root

0、题目

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 N N ( ≤ 1 0 4 ≤10^4 104) which is the number of nodes, and hence the nodes are numbered from 1 1 1 to N N N. Then N − 1 N−1 N1 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

1、大致题意

给出无向图的顶点个数 N N N ,和 N − 1 N-1 N1 条边,判断这个无向图可不可以转化成树。

  • 如果可以,给出所有具有最大深度树的树根
  • 如果不行,输出连通子集个数

2、基本思路

2.1 无向图转化为树的条件

首先一个问题,怎样可以判断一个无向图可以转化为树?

  • 必须是无回路的连通图
  • 当该图为有n-1条边的连通图,也为树

这边可以思考一下为什么满足以上两个条件只一就行。应该画个图就行。

2.1 判断图连通分量的个数

有两种方法 ——dfs 和 并查集。

在前面的 1013 Battle Over Cities 使用过使用并查集查找连通子集的个数,这边就使用 dfs

  • vis[] 记录访问历史, 访问过该结点就跳过,没有访问过就 dfs(i) ,同时 连通分量数+1,即:如果图是连通的,dfs一次就可以访问到所有结点。
  • 如果该图为树,让每个结点都做一次根,deep[] 记录每个结点为根时的树的深度,对每个结点 dfs_deep(),用 tmp 记录当前的深度,并与 max_deep 比较,如果有更大的就更新 vector<int>ans

3、解题过程

3.1 在线画图工具

首先,安利一个在线画图工具

例如:

Sample Output 1:
5
1 2
1 3
1 4
2 5

在这里插入图片描述

Sample Input 2:
5
1 3
1 4
2 5
3 4

在这里插入图片描述

3.2 AC代码

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=10040;
const int maxm=100050;

struct Edge {
	int to,next;
} edge[maxm];

int n,head[maxn],vis[maxn],num_edge;
int num_components,max_deep,tmp;
vector<int>ans;

void addedge(int from,int to) { //头插法插入结点
	edge[++num_edge].next=head[from];
	edge[num_edge].to=to;
	head[from]=num_edge;
}

void dfs(int x) {
	int k=head[x];
	while(k!=0) {
		if(vis[edge[k].to]==1) {
			k=edge[k].next;
			continue;
		}
		vis[edge[k].to]=1;
		dfs(edge[k].to);
		k=edge[k].next;
	}
}

void dfs_deep(int x,int deep) { //dfs查找最大深度
	if(!vis[x]) {
		vis[x]=1;
		int k=head[x];
		while(k!=0) {
			if(vis[edge[k].to]==1) {
				k=edge[k].next;
				continue;
			}
			dfs_deep(edge[k].to,deep+1);
			tmp=max(deep+1,tmp);
			k=edge[k].next;
		}
	}
}

int main() {
	cin>>n;
	int a,b;
	memset(edge,0,sizeof(edge));
	memset(vis,0,sizeof(vis));
	memset(head,0,sizeof(head));

	for(int i=0; i<n-1; i++) {
		cin>>a>>b;
		addedge(a,b);
		addedge(b,a);
	}
	num_components=0;
	for(int i=1; i<=n; i++) {
		if(vis[i]==0) {
			dfs(i);
			num_components++;
		}
	}
	if(num_components!=1) { //图不能转化为树
		cout<<"Error: "<<num_components<<" components";
	} else {
		max_deep=-1;
		for(int i=1; i<=n; i++) {
			memset(vis,0,sizeof(vis));
			tmp=0;
			dfs_deep(i,0);
			if(tmp>max_deep) { //当前深度大于最大深度
				ans.clear();
				ans.push_back(i);
				max_deep=tmp;
			} else if(tmp==max_deep) { //当前深度等于最大深度
				ans.push_back(i);
				max_deep=tmp;
			}
		}
		cout<<ans[0];
		for(int i=1; i<ans.size(); i++) {
			cout<<"\n"<<ans[i];
		}
	}
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值