1021. Deepest Root (25) -- 并查集 dfs

本文介绍了一种通过多次深度优先搜索(DFS)寻找给定树形结构中最深根节点的方法,旨在解决特定类型的图论问题。针对连通且无环的图结构,探讨如何找出能使树高度最大的根节点,并讨论了非树结构输入的错误处理。

1021. Deepest Root (25)

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 (<=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


http://www.patest.cn/contests/pat-a-practise/1021

ac关键:进行多次的DFS,set<>集合应用

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<set>


using namespace std;


const int INF=0x7fffffff;
const int N=10005;


int n;


vector<int> vt[N];


int fa[N];
int Find(int x){
	if(x==fa[x])
		return x;
	return fa[x]=Find(fa[x]);
}


void Merge(int x,int y){
	int xx=Find(x);
	int yy=Find(y);
	fa[yy]=xx;
}


int maxHeight;
int Height[N];
bool vis[N];


int tmpMaxId;




void DFS(int root,int h){
	if(vis[root])
		return;
	vis[root]=true;
	Height[root]=h;
	if(h>maxHeight)
	{
		maxHeight=h;
		tmpMaxId = root;
	}
	int len=vt[root].size();
	for(int i=0;i<len;i++){
		int v=vt[root][i];
		DFS(v,h+1);
	}
}


int main(){
	//freopen("in.txt","r",stdin);
	while(scanf("%d",&n)!=EOF){
		int i;
		int u,v;
		for(i=1;i<=n;i++){
			fa[i]=i;
			vt[i].clear();
		}
		for(i=0;i<n-1;i++){
			scanf("%d%d",&u,&v);
			vt[u].push_back(v);
			vt[v].push_back(u);
			if(Find(u)!=Find(v))
				Merge(u,v);
		}
		set<int> st;
		for(i=1;i<=n;i++)
			st.insert(Find(i));
		int stlen=st.size();
		if(stlen>1)
			printf("Error: %d components\n",stlen);
		else{
			for(i=1;i<=n;i++)
				vis[i]=false;
			maxHeight=0;
			tmpMaxId = -1;
			DFS(1,1);
			//
			vector<int> vans;
		/*	for(i=1;i<=n;i++){
				if(Height[i]==maxHeight)
					vans.push_back(i);
			}*/
			int lenv=vans.size();
			/*for(i=0;i<lenv;i++){
				for(int j=1;j<=n;j++)
					vis[j]=false;
				DFS(vans[i],1);
			}*/


			maxHeight = -1;
			for(int j=1;j<=n;j++)
				vis[j]=false;
			DFS(tmpMaxId, 1);


			int tmpMax=maxHeight;
			vans.clear();
			for(i=1;i<=n;i++){
				maxHeight=0;
				for(int j=1;j<=n;j++)
					vis[j]=false;
				DFS(i,1);
				if(maxHeight==tmpMax){
					vans.push_back(i);
				}
			}


			lenv=vans.size();
			for(int i=0;i<lenv;i++)
				cout<<vans[i]<<endl;
		}
	}//end while
	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、付费专栏及课程。

余额充值