PAT 1021 Deepest Root

news/2024/7/20 22:20:17 标签: 深度优先, 算法, c++

个人学习记录,代码难免不尽人意。
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 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 <cstdio>
#include<set>
#include<string>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=10100;
const int INF=1000000000;
vector<int> G[maxn];
bool visit[maxn]={false};
int height=0;
void dfs(int n){
	visit[n]=true;
	for(int i=0;i<G[n].size();i++){
		if(!visit[G[n][i]]){
			dfs(G[n][i]);
		}
	}
	return;
}
int res=0;
void dfsheight(int n,int height){
	visit[n]=true;
	if(height>res) res=height;
	for(int i=0;i<G[n].size();i++){
		if(!visit[G[n][i]]){
			dfsheight(G[n][i],height+1);
		}
	}
	return;
}
int main(){
  int n;
  scanf("%d",&n);
  for(int i=0;i<n-1;i++){
  	int a,b;
  	scanf("%d %d",&a,&b);
	G[a].push_back(b);
	G[b].push_back(a); 
  }
  int block=0;
  for(int i=1;i<=n;i++){
    if(!visit[i]){
    	dfs(i);
    	block++;
	}
  }
  if(block>1){
  	printf("Error: %d components",block);
  	return 0;
  }
  
  vector<int> v;
  int max=-1;
  for(int i=1;i<=n;i++){

  fill(visit+1,visit+1+n,false);
  	  res=0;
  	  	
  	  dfsheight(i,0);
  	  if(max<res){
  	  	max=res;
  	  	v.clear();
  	  	v.push_back(i);
		}
	   else if(max==res){//这个地方手误写成了max=res卡了好久
	   	v.push_back(i);
	   }
  }
  for(int i=0;i<v.size();i++){
  	printf("%d\n",v[i]);
  }

}

这道题还蛮有意思的,注意题目要求n<=10000,因此不能用邻接矩阵来做必须用邻接表。
其次,题目说给了n-1条边,因此我们可以排除连通块为1且有回路的情况,也就是说可以不用考虑回路。具体做法是先判断连通块是否为1,是则结束,不是则进入下一步dfs处理
各位一定要注意写if条件的时候等于是“==”,我有好几次都只写了一个=导致检查了半天错误,大家切记。


http://www.niftyadmin.cn/n/4947344.html

相关文章

mysql 查询报错 1267 - Illegal mix of collations

mysql 查询报错 1267 - Illegal mix of collations 详细报错: 1267 - Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for 主要的原因其实就是两张表的字符集不一样改一下就行了。 注: 改了表还是报错的话,那就是表内的字段没有…

Mongodb 更新集合的方法到底有几种 (中) ?

更新方法 Mongodb 使用以下几种方法来更新文档 &#xff0c; Mongodb V5.0 使用 mongosh 客户端&#xff1a; db.collection.updateOne(, , ) db.collection.updateMany(, , ) db.collection.replaceOne(, , ) db.collection.findOneAndReplace(, , ) db.collection.findO…

安装IIS服务

什么是IIS服务 IIS&#xff08;Internet Information Services&#xff09;是微软公司开发的一款用于托管和管理Web应用程序的服务软件。IIS服务是一种在Windows操作系统上运行的Web服务器&#xff0c;它能够处理HTTP&#xff08;Hypertext Transfer Protocol&#xff09;和HTT…

神经网络基础-神经网络补充概念-54-softmax回归

概念 Softmax回归&#xff08;Softmax Regression&#xff09;是一种用于多分类任务的机器学习算法&#xff0c;特别是在神经网络中常用于输出层来进行分类。它是Logistic回归在多分类问题上的推广。 原理 Softmax回归的主要思想是将原始的线性分数&#xff08;得分&#xf…

相对于多进程,你真的知道为什么要使用多线程吗(C/C++多线程编程)

目录 前言 线程VS进程 POSIX线程库的使用 线程创建 线程等待 线程分离 线程状态 可结合态线程实例 分离态线程实例 线程退出 线程的同步与互斥 同步互斥的概念 互斥锁&#xff08;互斥&#xff09; 互斥锁的使用步骤 总结说明 信号量 信号量的使用步骤 条件变…

神经网络基础-神经网络补充概念-58-端到端的深度学习

概念 端到端深度学习&#xff08;End-to-End Deep Learning&#xff09;是指将整个问题的解决过程从输入到输出都交由深度神经网络来完成&#xff0c;无需手工设计复杂的特征提取、预处理或后处理步骤。这种方法的核心思想是通过神经网络自动地学习适合任务的特征表示和映射&a…

python中的lstm:介绍和基本使用方法

python中的lstm&#xff1a;介绍和基本使用方法 未使用插件 LSTM&#xff08;Long Short-Term Memory&#xff09;是一种循环神经网络&#xff08;RNN&#xff09;的变体&#xff0c;专门用于处理序列数据。LSTM 可以记忆序列中的长期依赖关系&#xff0c;这使得它非常适合于各…

自动驾驶——车辆动力学模型

/*lat_controller.cpp*/ namespace apollo { namespace control {using apollo::common::ErrorCode;//故障码 using apollo::common::Status;//状态码 using apollo::common::TrajectoryPoint;//轨迹点 using apollo::common::VehicleStateProvider;//车辆状态信息 using Matri…