PAT1004. Counting Leaves bfs
来源:互联网 发布:个性签名 知乎 编辑:程序博客网 时间:2024/06/14 11:22
这个题目的意思是给出这棵树的节点数和非叶节点,统计各层的叶节点数。
这道题目还是蛮简单的,虽然是给出一棵树,但是并不需要完整的建树,只需要知道每个节点有没有孩子和该节点的层数即可。
代码如下
#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<cstring>using namespace std;vector<int> tree[120];int levelnum[120]; //节点层数int ans[120];//结果 每层的非叶节点数量// 字符串转整数int chattoInt(char a[2]){int b =(a[0]-'0')*10;int c = a[1]-'0';return b+c;}void solver(){queue<int> q;q.push(1);levelnum[1] = 1;int level=0;memset(ans,0,sizeof(ans));while(!q.empty()){int t = q.front();q.pop();if(levelnum[t]>level)level = levelnum[t];int cou = tree[t].size();if(cou==0){ans[level]++;}else{for(int i=0;i<tree[t].size();i++){q.push(tree[t][i]);levelnum[tree[t][i]] = levelnum[t]+1;}}}}int main(){int m,n;cin>>n>>m;for(int i=0;i<m;i++){char id[2];int k;cin>>id[0]>>id[1]>>k;int id1 = chattoInt(id);for(int j=0;j<k;j++){char id2[2];cin>>id2[0]>>id2[1];int id3 = chattoInt(id2);tree[id1].push_back(id3);}}solver();int sum=0;cout<<ans[1];for(int i=2;i<=n;i++) //最后好几点没过就是因为输出结构没控制好{if(sum!=n-m){cout<<" "<<ans[i];sum+=ans[i];}}return 0;}
0 0
- PAT1004. Counting Leaves bfs
- 【PAT1004】Counting Leaves
- PAT1004. Counting Leaves (30)
- pat1004 Counting Leaves
- [搜索]PAT1004 Counting Leaves
- PAT1004. Counting Leaves
- 【PAT1004】Counting Leaves
- pat1004 Counting Leaves
- PAT1004 Counting Leaves
- 1004. Counting Leaves (30)[bfs]
- 1004. Counting Leaves (30) PAT+BFS
- PAT 1004. Counting Leaves (30)(BFS)
- 1004. Counting Leaves (30)(*****dfs/bfs)
- Counting Leaves
- Counting Leaves
- 1004. Counting Leaves (30) 树的遍历 DFS BFS
- PAT 1004 Counting Leaves(BFS+模拟树)
- pat 1004 Counting Leaves
- 扩展欧几里德求逆元
- 地址中含有汉字等其他字符时 的处理方法
- 数据库连接池DBPool分析(四):mysql连接池 mysql_connection_pool
- linux grep命令详解
- 基于MSP430G2系列实现的步进电机控制
- PAT1004. Counting Leaves bfs
- mysql命令行
- 利用addChildViewController管理子视图控制器<2>
- 堆,优先队列
- android studio gradle 签名
- 计算一个数的二进制位中1的个数的方法总结
- Android中关于颜色的使用
- java学习笔记--类与接口
- 数据库连接池DBPool分析(五):Makefile