PAT 1021 Deepest Root 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 called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​410​^4104) 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

题意:给出一个有N个节点,N-1条边的图,若该图满足树结构,则输出使得树深度最大的根节点,若不满足,则输出联通域个数。

解题过程:

  1. 如果给出的图不满足树结构则要求输出联通域个数,所以首先应该使用dfs求出图的连通域个数,若只有一个联通域则可断定该图满足树结构,此处不用再判断图中是否含有环,因为本题只有N-1条边,如果一个图中只有一个联通域,且含有环则图中的边的个数应大于等于N。
  2. 如果1中判断满足树结构,则用dfs求出以每个节点为根节点时树的深度,最后输出深度最大的点的标号即可

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 10004; 
vector<vector<int> > G(maxn);
int mark[maxn];
int parent[maxn];
int visited[maxn];
int N;
void dfs1(int u)
{
	mark[u] = 1;
	for(int j = 0;j < G[u].size(); j++)
	{   
		if(mark[G[u][j]] == 0)
			dfs1(G[u][j]);	
	} 
}

int dfs2(int u)
{
	mark[u] = 1;
	int ans =  1;
	for(int i = 0;i < G[u].size(); i++)
	{
		if(mark[G[u][i]] == 0)
			ans = max(ans,1+dfs2(G[u][i]));
	}
	return ans;
}

int getComponents()
{
	memset(mark,0,sizeof(mark));
	int ans = 0;
	for(int i = 1;i <= N; i++)
	{
		if(mark[i] == 0)
		{
			dfs1(i);
			ans++;
		}
	}
	return ans;
}

int main(int argc, char const *argv[])
{
	int u,v;
	cin >> N;
	for(int i = 0;i < N-1; i++)
	{
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	
	int parts = getComponents();
	if(parts != 1)                       //如果含有多个联通域
		cout << "Error: " << parts << " components" << endl;
	else
	{
		int depth[N+1],max_depth = -1;  // 分别用于记录以i为根节点的深度  最大深度 
		for(int i = 1;i <= N; i++) 
		{
			memset(mark,0,sizeof(mark));
			depth[i] = dfs2(i);
			if(max_depth < depth[i])
				max_depth = depth[i];
		}
		for(int i = 1;i <= N; i++)
		{
			if(depth[i] == max_depth)
				cout << i << endl;
		}
	}
		
	return 0;
}
【从高压输电线的架空地线中汲取电能】一个25千瓦受控电源从735千伏线路的架空地线中汲取电能的SimPowerSystems模型(Simulink仿真实现)内容概要:本文介绍了一个基于SimPowerSystems的Simulink仿真模型,用于模拟从735千伏高压输电线的架空地线中汲取25千瓦电能的受控电源系统。该模型聚焦于高压输电线路中架空地线的能量回收技术,通过仿真手段实现对电能采集过程的建模与控制策略验证,体现了电力系统中新型能源获取方式的技术可行性与工程应用潜力。文中还提及该资源属于一系列电力系统仿真研究的一部分,涵盖微电网、储能优化、碳流追踪、鲁棒调度等多个前沿方向,配套提供Matlab/Simulink代码及网盘资料链接,便于科研人员复现与拓展研究。; 适合人群:具备电力系统基础知识、熟悉Matlab/Simulink仿真环境,从事电力工程、能源回收或智能电网相关研究的科研人员及研究生;有一定编程与建模仿真经验的高年级本科生或工程技术人员。; 使用场景及目标:①研究高压输电线路中架空地线的能量回收机制与建模方法;②掌握基于Simulink的电力系统仿真技术,特别是受控电源与电网交互的动态特性分析;③为开展能源 harvesting、分布式供能、电力电子变换器控制等相关课题提供参考模型与技术支撑; 阅读建议:建议结合提供的仿真模型文件进行实操演练,重点理解系统结构设计、参数设置与控制逻辑实现;同时可延伸学习文档中提到的其他电力系统优化与仿真案例,以拓宽研究视野和技术积累。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值