【PAT - 甲级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​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

题目大意:

一个连通的非循环图可以看作是一个树。树的高度取决于所选的根。现在你应该找到根,结果是最高的树。这样的根叫做最深的根。对于每个测试用例,在一行中打印每个最深的根。如果这样的根不是惟一的,则按其数字的递增顺序打印它们。如果给定的图形不是树,则打印Error: K components,其中K是图中连图分量的数量。

解题报告:

  因为时限是两秒,直接枚举每一个点当根节点就可以。但是这题其实可以优化,只枚举叶子节点就可以。甚至可以用树的直径去乱搞一波,时间复杂度会更优。但这题时限很宽,所以没必要优化。

AC代码: 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,f[MAX],rt[MAX],dep[MAX];
vector<int> vv[MAX],ans;
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
int mx;
void dfs(int u,int fa) {dep[u] = dep[fa] + 1;mx = max(mx,dep[u]);int up = vv[u].size();for(int i = 0; i<up; i++) {int v = vv[u][i];if(v == fa) continue;dfs(v,u);}
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) f[i] = i;for(int u,v,i = 1; i<=n-1; i++) cin>>u>>v,vv[u].pb(v),vv[v].pb(u),merge(u,v);int cnt = 0;for(int i = 1; i<=n; i++) {if(f[i] == i) cnt++;}if(cnt != 1) {printf("Error: %d components\n",cnt);return 0 ;}for(int root = 1; root<=n; root++) {mx = 0;dfs(root,0);rt[root] = mx;}mx = 0;for(int i = 1; i<=n; i++) {if(rt[i] > mx) {mx = rt[i];ans.clear();ans.pb(i);}else if(rt[i] == mx) ans.pb(i);} sort(ans.begin(),ans.end());for(auto x : ans) {printf("%d\n",x);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)

题干&#xff1a; A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic numbers can be pair…

银行卡密码的加密、MAC计算

简介 在银行、银联、第三方支付等金融系统中&#xff0c;对银行卡密码等信息的加解密&#xff0c;对交易数据的加解密无处不在&#xff0c;在商场刷卡消费的POS机&#xff0c;在ATM机器取款等都需要对数据加密以保护数据安全&#xff0c;不被窃取。 本文主要对POS机的安全处理…

【PAT甲级 - 1028】List Sorting (25分)(模拟,排序)

题干&#xff1a; Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​) and C, where…

传统POS/终端/银联POS简介

point of sale&#xff1b; POS能够接受银行卡信息&#xff0c;具有通讯功能&#xff0c;并接受柜员的指令而完成金融交易信息和有关信息交换的设备。 找政策

【Python学习】 - - 链表推导式[ 2*x for x in X ]、匿名函数、并行迭代

列表推导式[x for x in range(n)] 问题&#xff1a;请计算出1~9间的整数的平方 常规方法 for i in range(1,10):print(i*i) 链表推导式&#xff1a; print([x*x for x in range(1,10)]) 匿名函数方法&#xff1a; 匿名函数语法形式&#xff1a; lambda [arg1, arg2, arg3,…

C#多线程和线程池

.Net的公用语言运行时&#xff08;Common Language Runtime&#xff0c;CLR&#xff09;能区分两种不同类型的线程&#xff1a;前台线程和后台线程。这两者的区别就是&#xff1a;应用程序必须运行完所有的前台线程才可以退出&#xff1b;而对于后台线程&#xff0c;应用程序则…

国家密码算法SM4(国密算法)介绍

国密是国家密码局认定的国产密码算法。而与之对应的&#xff0c;现在被广泛使用des、3des等算法是国外人发明&#xff0c;我们称为国际算法。 加密算法采用国家密码算法SM4&#xff0c;密钥长度为16字节&#xff0c;加密算法详见附录A。 主要有SM1&#xff0c;SM2&#xff0…

【最小费用可行流模板】

可能再也用不到了吧&#xff0c;今天整理电脑文件看到的&#xff0c;作为图论选手&#xff0c;留个纪念&#xff0c; //原图&#xff1a; 对于pi&#xff0c;拆点xi,yi s->S,[m,m],0 S->xi,[0,inf],0 yi->t,[0,inf],0 xi->yi,[vi,vi],0 对于有航线的pi和pj&#x…

ANSI X9.9 MAC算法介绍

1&#xff09;该算法只使用单倍长密钥&#xff0c;也就是8字节密钥&#xff1b; 2&#xff09;MAC数据按8字节分组&#xff0c;尾部以字节00补齐&#xff1b; 3&#xff09;用MAC密钥加密第一个8字节分组&#xff0c;加密结果与第二个8字节分组异或&#xff0c;然后再用MAC密…

C#.Net使用线程池(ThreadPool)与专用线程(Thread)

线程池(ThreadPool)使用起来很简单&#xff0c;但它有一些限制&#xff1a; 1. 线程池中所有线程都是后台线程&#xff0c;如果进程的所有前台线程都结束了&#xff0c;所有的后台线程就会停止。不能把入池的线程改为前台线 程。 2. 不能给入池的线程设置优先级或名称。 3. 对于…

【Python学习】 - sklearn学习 - 评估指标precision_score的参数说明

函数声明&#xff1a; precision_score(y_true, y_pred, labelsNone, pos_label1, averagebinary, sample_weightNone) 其中较为常用的参数解释如下&#xff1a; y_true&#xff1a;真实标签 y_pred&#xff1a;预测标签 average&#xff1a;评价值的平均值的计算方式。可…

ANSI X9.19 MAC算法介绍

(1) ANSI X9.19MAC算法只使用双倍长密钥&#xff0c;也就是16字节密钥&#xff1b; (2) MAC数据按8字节分组&#xff0c;表示为D0&#xff5e;Dn&#xff0c;如果Dn不足8字节时&#xff0c;尾部以字节00补齐&#xff1b; (3) 用MA…

GitHub.com上的那些东西你都知道什么意思吗?

GitHub初学入门者的图谱&#xff0c;介绍Github网站每个功能的意思 一、键盘快捷键 在GitHub中&#xff0c;很多页面都可以使用键盘快捷键。在各个页面按下“shift /”都可以打开键盘快捷键一览表&#xff0c;如下图&#xff1a; 快捷键 二、工具栏 工具栏 LOGO 点击GitHub…

【Python学习】 - sklearn学习 - 数据集分割方法 - 随机划分与K折交叉划分与StratifiedKFold与StratifiedShuffleSplit

一、随机划分 import numpy as np from sklearn import datasetsiris datasets.load_iris() X iris.data y iris.target# 1&#xff09;归一化前&#xff0c;将原始数据分割 from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test train_t…

CURLE_WRITE_ERROR

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) { return 0; //return size * nmemb; } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); res curl_easy_perform(curl); 如果应…

Azure 应用服务、虚拟机、Service Fabric 和云服务的比较

Azure 提供了几种托管网站的方式&#xff1a;Azure 应用服务、虚拟机、Service Fabric 和云服务。 本文可帮助你了解这几种方式&#xff0c;并针对 Web 应用程序做出正确的选择。 Azure 应用服务是大多数 Web 应用的最佳选择。 部署和管理都已集成到平台&#xff0c;站点可以快…

【Python学习】 - sklearn - PCA降维相关

1、PCA算法介绍 主成分分析&#xff08;Principal Components Analysis&#xff09;&#xff0c;简称PCA&#xff0c;是一种数据降维技术&#xff0c;用于数据预处理。一般我们获取的原始数据维度都很高&#xff0c;比如1000个特征&#xff0c;在这1000个特征中可能包含了很多…

【Python学习】 - sklearn - 用于生成数据的make_blobs模块

函数原型&#xff1a; sklearn.datasets.make_blobs(n_samples100, n_features2, centers3, cluster_std1.0, center_box(-10.0, 10.0), shuffleTrue, random_stateNone) 参数含义&#xff1a; n_samples: int, optional (default100) The total number of points equally di…

微服务架构及幂等性

微服务架构 微服务架构是一种架构概念&#xff0c;旨在通过将功能分解到各个离散的服务中以实现对解决方案的解耦。它的主要作用是将功能分解到离散的各个服务当中&#xff0c;从而降低系统的耦合性&#xff0c;并提供更加灵活的服务支持。 和 微服务 相对应的&#xff0c;这…

【Python学习】 - Matplotlib二维绘图 - plt.matshow()和plt.imshow()区别对比

给定一个8*8的数据&#xff0c;用两种方式分别进行输出。 xx np.zeros((8,8),dtype np.uint8) xx[0,0] 13im Image.fromarray(xx) plt.imshow(im)plt.matshow(xx) plt.show() 输出&#xff1a; 得出结论&#xff1a; 首先我不知道为啥两个窗口是不一样大的。 其次发现图…