PAT - 甲级 - 1021. Deepest Root (25)(DFS图的连通分量,最大深度)

本文介绍了一种算法,用于从给定的连通无环图中找到能够形成最大深度树的根节点。通过深度优先搜索(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 called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) 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

给定条件:
1.n个节点
2.n-1条连线

题目要求:
1.判断给定的图是否连通的
2.如果连通,求出选取那些节点作为根节点,所建成的树的深度最大
3.按照编号从小到大的顺序,输出符合条件的根节点

解决方法:
1.dfs求图的连通分量,判断是否为1
2.分别选取所有的节点作为根节点,进行深度优先遍历,记录所到达的最大深度,并且在搜索过程中记录每个节点所能处于的最大深度
3.遍历所有节点,输出所在深度可以为最大深度的节点


#include<cstdio>
#include<vector>
using namespace std;

int N, a, b, maxd = -1;
int vis[10001];
int depth[10001];
vector<int> e[10001];

void dfs(int v, int d){
	vis[v] = 1;
	depth[v] = max(depth[v], d); // update node's depth
	maxd = max(maxd, d); // update the max depth
	for(int i = 0; i < e[v].size(); i++){
		int next = e[v][i];
		if(!vis[next]){
			dfs(next,d+1);
		}
	}
}

int main(){
	while(scanf("%d", &N) != EOF){
		// input
		for(int i = 1; i < N; i++){
			scanf("%d%d", &a, &b);
			e[a].push_back(b);
			e[b].push_back(a);
		}
		fill(vis, vis+10001, 0);

		int cnt = 0;
		for(int i = 1; i <= N; i++){
			if(!vis[i]){
				dfs(i,1);
				cnt++; // the number of components
			}	
		}
		if(cnt != 1){
			printf("Error: %d components\n",cnt);
		}else{
			// dfs every node
			for(int i = 1; i <= N; i++){
				fill(vis, vis+10001, 0);
				vis[i] = 1;
				dfs(i,1);
			}
			// find the node which depth is maxd
			for(int i = 1; i <=N; i++){
				if(depth[i] == maxd){
					printf("%d\n", i);
				}
			}
		}
	}
	return 0;
}



def search_astra_datalog( directory: str = os.getcwd(), filelist: list = [], depth: int = 0, max_depth: int = 10, ) -> list: """ Traverse the files and folders from the current working directory downwards to find and collect astra data log. Parameters ---------- directory : str, default current working directory Used to specify the absolute starting directory path. By default, it starts from the directory path where the script is run, and you can also customize the directory path. filelist : list, default [] The directory path used to store the astra data log from the collected. The default is an empty list, or you can use a defined list. depth : int, default 0 Used to specify the starting directory level. If you know the exact starting directory level, you can define it here to reduce operating time. max_depth : int, default 10 Used to specify the ending directory level. You can specify the deepest directory level so that you can end the traversal early. Returns ------- list The returns contains the absolute directory path of all astra data logs. Notes ----- ...... """ new_directory = directory if os.path.isfile(directory): if ( "csv" in directory and not "event" in directory and not "tar.gz" in directory and "#" in directory ): filelist.append(directory) elif os.path.isdir(directory): if depth < max_depth: try: for s in os.listdir(directory): new_directory = os.path.join(directory, s) search_astra_datalog(new_directory, filelist, depth + 1, max_depth) except PermissionError: print(f"Access denied to directoryectory: {directory}") return filelist
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值