1336:【例3-1】找树根和孩子

简介: 1336:【例3-1】找树根和孩子

时间限制: 1000 ms         内存限制: 65536 KB

【题目描述】

给定一棵树,输出树的根root,孩子最多的结点max以及他的孩子。

【输入】

第一行:n(结点个数≤100),m(边数≤200)。

以下m行:每行两个结点x和y,表示y是x的孩子(x,y≤1000)。

【输出】

第一行:树根:root;

第二行:孩子最多的结点max;

第三行:max的孩子(按编号由小到输出)。

【输入样例】

8 7

4 1

4 2

1 3

1 5

2 6

2 7

2 8

【输出样例】

4

2

6 7 8

1. #include <iostream>
2. #include <cstdio>
3. using namespace std;
4. int tree[110],n,m;
5. int main()
6. {
7.  int x,y,root,maxn=0,maxp;
8.  cin>>n>>m;
9.  for(int i=1;i<=m;i++){
10.     cin>>x>>y;
11.     tree[y]=x;
12.   }
13.   for(int i=1;i<=n;i++){
14.     if(tree[i]==0){
15.       root=i;
16.       break;
17.     }
18.   }
19.   for(int i=1;i<=n;i++){
20.     int cnt=0;
21.     for(int j=1;j<=n;j++)
22.       if(tree[j]==i) cnt++;
23.     if(cnt>maxn){
24.       maxn=cnt;maxp=i;
25.     }
26.   }
27.   cout<<root<<endl;
28.   cout<<maxp<<endl;
29.   for(int i=1;i<=n;i++)
30.     if(tree[i]==maxp)
31.       cout<<i<<" "; 
32. return 0;
33. }


相关文章
|
安全 网络安全 开发工具
git 提示:fatal: Could not read from remote repository.
git 提示:fatal: Could not read from remote repository.
687 0
git 提示:fatal: Could not read from remote repository.
|
11月前
|
开发框架 .NET
1337:【例3-2】单词查找树
1337:【例3-2】单词查找树
133 0
|
11月前
1338:【例3-3】医院设置
1338:【例3-3】医院设置
|
11月前
1360:奇怪的电梯(lift)
1360:奇怪的电梯(lift)
|
11月前
1270:【例9.14】混合背包
1270:【例9.14】混合背包
|
Linux 数据安全/隐私保护 Windows
更换(Pypi)pip源到国内镜像
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.
221340 2
|
机器学习/深度学习 设计模式 Rust
Rust机器学习之tch-rs
整体上tch-rs的使用思路和PyTorch是一致的,因为本身tch-rs就是PyTorch的C++库`libtorch`的绑定。如果你熟练使用PyTorch,那么用tch-rs上手会非常快。关键是用tch-rs能够带给你更快的速度,这在大规模项目中是一个巨大的优势。
959 0
Rust机器学习之tch-rs
|
1月前
【C/PTA】选择结构专项练习(额外)
【C/PTA】选择结构专项练习(额外)
56 0
|
11月前
1335:【例2-4】连通块
1335:【例2-4】连通块
|
8月前
|
搜索推荐 虚拟化 Windows
VMware17 下载以及 配置虚拟机 一条龙全教程
VMware17 下载以及 配置虚拟机 一条龙全教程
565 0

热门文章

最新文章