当前位置:网站首页>-找树根2-
-找树根2-
2022-08-03 11:42:00 【-JMY-】
题目描述
一棵树有n个结点,已知树上所有的父子结点关系,请问该树的根是几号结点,哪个结点的子结点最多,该结点有哪些子结点。
输入
第一行,有1个整数n代表结点数量(0<n≤100)
接下来若干行;每行两个结点x和y,表示y是x的孩子(1≤x,y≤1000)
请注意:树上结点的编号不一定是连续的
输出
第一行输出树根的编号。
第二行输出孩子最多的结点编号(如果有多个结点的子结点都是最多的,则输出编号最大的那个)。
第三行输出第二行求出的孩子最多的结点,有哪些孩子,按照编号从小到大,输出这些孩子的编号,用空格隔开。
样例输入
5 4 1 4 2 1 3 1 5
样例输出
4 4 1 2
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,a[1005],b[1005],s[1005],k[1005],l[1005],maxn,ip;
int f(int x){
if(s[x]==0)
return x;
return f(s[x]);
}
int main(){
cin>>n;
for(int i=0;i<n-1;i++){
cin>>a[i]>>b[i];
s[b[i]]=a[i];
k[a[i]]++;
}
cout<<f(b[0])<<'\n';
for(int i=0;i<n-1;i++){
if(k[maxn]<k[a[i]]||(k[maxn]<=k[a[i]]&&maxn<=a[i]))
maxn=a[i];
}
cout<<maxn<<'\n';
for(int i=0;i<n-1;i++){
if(s[b[i]]==maxn){
l[ip]=b[i];
ip++;
}
}
sort(l,l+ip);
for(int i=0;i<ip;i++)
cout<<l[i]<<' ';
return 0;
}
边栏推荐
- Generate interface documentation online
- LyScript 实现对内存堆栈扫描
- Take you understand the principle of CDN technology
- LeetCode——1161. 最大层内元素和
- 数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
- LyScript implements memory stack scanning
- 优维低代码:Provider 构件
- RICON:NER SOTA 又来!
- Matlab学习12-图像处理之图像增强
- Simple implementation of a high-performance clone of Redis using .NET (1)
猜你喜欢
随机推荐
bash while循环和until循环
Cookie和Session使用
RTP协议分析
2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
Cookie and Session usage
opencv学习—VideoCapture 类基础知识「建议收藏」
Traceback (most recent call last): File
学习软件测试需要掌握哪些知识点呢?
增加WebView对localStorage的支持
C language advanced article: memory function
thymeleaf中的日期格式转化
bash case用法
后台图库上传功能
劝退背后。
XDR平台架构与关键技术解析
智能日报脚本
fastposter v2.9.0 programmer must-have poster generator
mysql advanced (twenty-four) method summary of defense against SQL injection
899. 有序队列 : 最小表示法模板题
码率vs.分辨率,哪一个更重要?