当前位置:网站首页>-找树根-
-找树根-
2022-08-03 11:42:00 【-JMY-】
题目描述
一棵树有n个结点,已知树上所有的父子结点关系,请问该树的根是几号结点,哪个结点的子结点最多,该结点有哪些子结点。
输入
第一行,有1个整数n代表结点数量(0<n≤100)
接下来若干行;每行两个结点x和y,表示y是x的孩子(1≤x,y≤n)
输出
第一行输出树根的编号。
第二行输出孩子最多的结点编号(如果有多个结点的子结点都是最多的,则输出编号最大的那个)。
第三行输出第二行求出的孩子最多的结点,有哪些孩子,按照编号从小到大,输出这些孩子的编号,用空格隔开。
样例输入
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;
}
边栏推荐
- 【MySQL】数据库进阶之索引内容详解(上篇 索引分类与操作)
- LeetCode-1796. 字符串中第二大的数字
- LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
- Realize 2d characters move left and right while jumping
- Simple implementation of a high-performance clone of Redis using .NET (1)
- 最牛逼的集群监控系统,它始终位列第一!
- bash for循环
- Simple implementation of a high-performance clone of Redis using .NET (1)
- LyScript implements memory stack scanning
- The effects of the background and the Activiti
猜你喜欢
随机推荐
分享一款实用的太阳能充电电路(室内光照可用)
The effects of the background and the Activiti
Objective - C code analysis of the deep and shallow copy
小身材有大作用——光模块基础知识(一)
Matlab学习10-图像处理之傅里叶变换
After completing the interview and clearance collection of Alibaba, I successfully won the 15th Offer this year
智能日报脚本
viewstub 的详细用法_pageinfo用法
一文带你弄懂 CDN 技术的原理
从零开始Blazor Server(6)--基于策略的权限验证
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
LeetCode-1796. 字符串中第二大的数字
Dva.js 新手入门指南
MySQL之json数据操作
GET 和 POST 有什么区别?
基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
C - 为什么指针常常初始化为 NULL?
Lease recovery system based on PHP7.2+MySQL5.7
mysql advanced (twenty-four) method summary of defense against SQL injection








