前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >找树根和孩子

找树根和孩子

作者头像
attack
发布2018-04-12 15:41:39
4920
发布2018-04-12 15:41:39
举报

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

【问题描述】

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

【输入格式】

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

    以下m行;每行两个结点x和y,

    表示y是x的孩子(x,y<=1000)。

【输出格式】

  第一行:树根:root。   

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

    第三行:max的孩子。

简单方法

代码语言:javascript
复制
 1 #include<iostream>
 2 using namespace std;
 3 int tree[101];
 4 int maxn=0;
 5 int maxnroot;
 6 int main()
 7 {
 8     int n,m;
 9     int root=-1;
10     cin>>n>>m;
11     for(int i=1;i<=m;i++)
12     {
13         int x,y;
14         cin>>x>>y;
15         tree[y]=x;
16     }
17     for(int i=1;i<=n;i++)
18     {
19         if(tree[i]==0)
20         {
21             root=i;
22         }
23     }
24     for(int i=1;i<=n;i++)
25     {
26         int sum=0;
27         for(int j=1;j<=n;j++)
28         {
29             if(tree[j]==i)
30             {
31                 sum++;
32             }
33         }
34         if(sum>maxn)
35         {
36             maxn=sum;
37             maxnroot=i;
38         }
39     }
40     cout<<root<<endl;
41     cout<<maxnroot<<endl;
42     for(int i=1;i<=n;i++)
43     {
44         if(tree[i]==maxnroot)
45         {
46             cout<<i<<" ";
47         }
48     }
49     return 0;
50 }
代码语言:javascript
复制
父亲孩子表示法

 1 #include<iostream>
 2 using namespace std;
 3 int tree[101];
 4 int maxn=0;
 5 int maxnroot;
 6 int main()
 7 {
 8     int n,m;
 9     int root=-1;
10     cin>>n>>m;
11     for(int i=1;i<=m;i++)
12     {
13         int x,y;
14         cin>>x>>y;
15         tree[y]=x;
16     }
17     for(int i=1;i<=n;i++)
18     {
19         if(tree[i]==0)
20         {
21             root=i;
22         }
23     }
24     for(int i=1;i<=n;i++)
25     {
26         int sum=0;
27         for(int j=1;j<=n;j++)
28         {
29             if(tree[j]==i)
30             {
31                 sum++;
32             }
33         }
34         if(sum>maxn)
35         {
36             maxn=sum;
37             maxnroot=i;
38         }
39     }
40     cout<<root<<endl;
41     cout<<maxnroot<<endl;
42     for(int i=1;i<=n;i++)
43     {
44         if(tree[i]==maxnroot)
45         {
46             cout<<i<<" ";
47         }
48     }
49     return 0;
50 }

父亲孩子表示法

代码语言:javascript
复制
 1 #include<iostream>
 2 using namespace std;
 3 struct node
 4 {
 5     int parent;
 6     int child[101];
 7     int hzsl;
 8     int date;
 9 }tree[101];
10 int main()
11 {
12     int n,m;
13     cin>>n>>m;
14     for(int i=1;i<=m;i++)
15     {
16         int x,y;
17         cin>>x>>y;
18         tree[x].child[tree[x].hzsl]=y;
19         tree[x].hzsl++;
20         tree[y].parent=x;
21     }
22     for(int i=1;i<=n;i++)
23     {
24         if(tree[i].parent==0)
25         {
26             cout<<i<<endl;
27         }
28     }
29     int maxn=0;
30     int maxnroot=0;
31     for(int i=1;i<=n;i++)
32     {
33         if(tree[i].hzsl>maxn)
34         {
35             maxn=tree[i].hzsl;
36             maxnroot=i;
37         }
38     }
39     cout<<maxnroot;
40     for(int i=0;i<tree[maxnroot].hzsl;i++)
41     {
42         cout<<tree[maxnroot].child[i]<<" ";
43     }
44     return 0;
45 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档