PAT 1021 Deepest Root (25 分)(最后两个测试点错误)燚_1021 deepest root (25分)运行超时-程序员宅基地

技术标签: code  

A graph which is connected and acyclic can be considered a tree. The hight 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​4​​) 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
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
struct Node {
	int num;
	int floor;
};
vector<vector<int>>G; //图
vector<int>node;
int n;
set<int>s;
//并查集  findRoot() unions()
int findRoot(int i) {
	while (node[i] != i) {
		i = node[i];
	}
	return i;
}
void unions(int a,int b) {
	int aRoot = findRoot(a);
	int bRoot = findRoot(b);
	if (aRoot != bRoot)
		node[aRoot] = bRoot;
}
//第一次BFS找到所有叶子节点并加入集合s中
int findTreeRoot(vector<Node>nodes) {
	queue<Node>q;
	nodes[1].floor = 1;
	q.push(nodes[1]);
	int maxFloor = 1;
	while(!q.empty()){
		Node temp = q.front();
		q.pop();
		for (int i = 0;i < G[temp.num].size();i++) {
			if (nodes[G[temp.num][i]].floor == 0) {
				nodes[G[temp.num][i]].floor = temp.floor + 1;
				q.push(nodes[G[temp.num][i]]);
				maxFloor = temp.floor + 1;
			}
		}
	}
	int root = 1;
	for (int i = 1;i <= n;i++) {
		if (nodes[i].floor == maxFloor) {
			root = i;
			s.insert(i);
		}
	}
	return root;
}
//第二次BFS找到所有叶子节点加入集合s中
void deepestRoot(vector<Node>nodes,int root) {
	
	queue<Node>q;
	nodes[root].floor = 1;
	q.push(nodes[root]);
	int maxFloor = 1;
	while (!q.empty()) {
		Node temp = q.front();
		q.pop();
		for (int i = 0;i < G[temp.num].size();i++) {
            //如果该节点没有被访问过则更新该节点的信息
			if (nodes[G[temp.num][i]].floor == 0) {
				nodes[G[temp.num][i]].floor = temp.floor + 1;
				q.push(nodes[G[temp.num][i]]);
				maxFloor = temp.floor + 1;
			}
		}
	}
	//将最深的节点加入s中
	for (int i = 1;i <= n;i++) {
		if (nodes[i].floor == maxFloor) {
			s.insert(i);
		}
	}
    //输出结果
	for (auto i = s.begin();i != s.end();i++) {
		printf("%d\n", *i);
	}
	return;
}
int main() {
	cin >> n;
	G.resize(n + 1);
	node.resize(n + 1, 0);
	for (int i = 1;i <= n;i++)
		node[i] = i;
    //建立树
	for (int i = 1;i <= n - 1;i++) {
		int n1, n2;
		scanf("%d%d", &n1, &n2);
		G[n1].push_back(n2);
		G[n2].push_back(n1);
		unions(n1, n2);
	}
	int count = 0;//记录连通分量个数数
	for (int i = 1;i <= n;i++) {
		if (node[i] == i)
			count++;
	}
	if(count==1){
		vector<Node>ns(n + 1);
		for (int i = 1;i <= n;i++) {
			ns[i].num = i;
			ns[i].floor = 0;
		}
		int root = findTreeRoot(ns);
		deepestRoot(ns, root);
	}
	else {
		printf("Error: %d components", count);
	}
	

}

题目大意:给定一个图,先判断该图是否为树,如果是树则求出该树最深的叶子节点和根。

输入:第一行:N 该图的节点个数,从1~N;

           第二行到第N行:N-1条边,每条边的端点。

思路:1.用set来存储结果。

           2.先用并查集判断该图是否为树

           3.如果是树则以节点1为根节点先用BFS找到最深的所有叶子节点并存储在set中(用DFS也可以)。

           4.在以3中找到的叶子节点中的任意一个节点为根结点再用BFS找到最深的所有叶子节点存储在set中。

           5.输出set中的节点即为所求结果。

注意:思路中的第三点,是将最深的说有叶子节点存储在set中(不是任意一个)。

          例子:5

                     1   2

                      2   3

                      3   4

                      3    5

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_38090681/article/details/85017680

智能推荐

成功创业家的心理特征_刘强东情绪稳定性对他的影响-程序员宅基地

文章浏览阅读1.7k次。 美国对75位取得成功的创业家所做的研究,归纳出“企业家的心理特征”。它成为创业者的座右铭。  自信——他们普遍都有很强的自信心,有时有咄咄逼人的感觉。  急迫感——创业家通常很急切地想见到事物的成果,因此会给别人带来许多的压力。他们信仰“时间就是金钱”,不喜欢也不会把宝贵的时间浪费在无聊琐碎的事情上。  广泛的知识_刘强东情绪稳定性对他的影响

Linux 编译OpenJDK_linux 怎么编译opk 文件-程序员宅基地

文章浏览阅读2k次。CentOS 6.5 i386 平台编译 OpenJDK8_linux 怎么编译opk 文件

蚁群优化算法在具有时间窗的车辆路径问题中的应用:MATLAB实现及详解-程序员宅基地

文章浏览阅读168次。通过这篇文章,我们了解了如何在MATLAB环境中使用蚁群优化算法解决具有时间窗的车辆路径问题。希望这为您提供了一个有价值的参考,帮助您更好地理解和应用这一算法。这篇文章详细描述了蚁群优化算法在具有时间窗的车辆路径问题中的应用,并给出了MATLAB的完整实现。考虑到篇幅与深入性,我们在一定程度上简化了部分内容。在实际应用中,可能还需要根据问题的具体情况进行相应的调整和优化。

V5VPN 下载地址-程序员宅基地

文章浏览阅读3.5k次,点赞12次,收藏16次。VPN客户端的使用说明

Nginx配置Http响应头安全策略_nginx content-security-policy-程序员宅基地

文章浏览阅读8.9k次,点赞12次,收藏41次。1. 防止跨站脚本攻击(XSS):通过设置`CSP(Content-Security-Policy)`,可以限制浏览器只加载和执行来自特定来源的脚本,从而防止恶意脚本注入和执行。2. 防止点击劫持攻击:通过设置`X-Frame-Options`响应头,可以防止网页被嵌入到其他网站的`iframe`中,避免用户在不知情的情况下触发恶意代码。3. 提高网站安全性:通过设置`STS(Strict-Transport-Security)`响应头,强制浏览器使用`HTTPS协议`与服务器通信,从而防止信息在传输过_nginx content-security-policy

centos搭建Prometheus基础_centos安装prometheus-程序员宅基地

文章浏览阅读730次,点赞16次,收藏10次。centos搭建prometheus基础_centos安装prometheus

随便推点

java/php/node.js/python基于微信小程序的图书管理系统【2024年毕设】-程序员宅基地

文章浏览阅读27次。管理端为管理员操作界面,主在包括首页、个人中心、会员管理、图书分类管理、图书信息管理、热门图书管理、图书入库管理、图书出库管理、在线借阅管理、续借信息管理、还书信息管理、增加积分管理、扣减积分管理、会员续期管理、系统管理等功能;springboot基于springboot的小型超市库存管理系统。springboot基于springboot的交互式教学微信小程序。

小程序如何实现定点跳转其他的小程序(京东、苏宁)的具体店铺或商品页面_京东小程序怎么跳转京东app-程序员宅基地

文章浏览阅读7.8k次,点赞3次,收藏8次。1.以跳转京东商城为例,首先要获取京东商城的Appid获取方式如下:点击更多资料可看到appid:wx91d27dbf599dff74,2.获取要跳转的小程序页面路径进入小程序后台当输入了微信号开启之后可以在京东小程序中复制路径,复制路径:pages/cate/cate.html3.小程序相关配置app.json 配置 "navigateToMiniProgramA..._京东小程序怎么跳转京东app

jquery 实现老虎机的跑动效果-程序员宅基地

文章浏览阅读4.6k次。老虎机的跑动效果需要自行引用jquery库 老虎机

微信公众号API Java实现:一键解锁微信开发新可能-程序员宅基地

文章浏览阅读262次,点赞4次,收藏3次。微信公众号API Java实现:一键解锁微信开发新可能项目地址:https://gitcode.com/caijianqing/weixinmp4java在数字化时代的今天,微信已经成为了企业与客户沟通的重要桥梁,而微信公众号平台更是企业进行营销和互动的一大利器。为了帮助开发者更高效、更便捷地利用微信公众号API,我们推荐一款由caijianqing开发的Java库——weixinmp4ja...

C#连接SQLite的...方法_app.config <dbproviderfactories> sqlite-程序员宅基地

文章浏览阅读1.3k次。1 SQLite简介  SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysq_app.config sqlite

如何利用 XMind 高效学习?_学习xmind软件体会-程序员宅基地

文章浏览阅读202次。一、合理分配每日的时间和精力。高效源于精力的有效管理。合理分配学习和娱乐的时间,在专注时集中注意力,在娱乐时尽情放松。XMind 是时间管理神器,把预习、复习、小组作业、社团等事项安排得明明白白。用 XMind 批量整理待办事项调整主题顺序合理安排优先级用快速样式划去完成的任务掌控时间,把握节奏,规划自己的充实生活,达成更多成就。二:高效记录课堂笔记,课上内容全掌握。除了做好预习准备外,高效上课很重要的一点在于,既要全神贯注 follow 老师的思路,还要聪明地记录笔记。用 XMin_学习xmind软件体会

推荐文章

热门文章

相关标签