【PAT Advanced Level】1004. Counting Leaves (30)
来源:互联网 发布:创业网络平台建设 编辑:程序博客网 时间:2024/05/01 02:53
利用广度优先搜索,找出每层的叶子节点的个数。
#include <iostream>#include <vector>#include <queue>#include <fstream>using namespace std;vector<vector<int>> tree;vector<int> ans;void BFS(int s){queue<pair<int, int>> q;q.push(make_pair(s, 0));int cur_step = 0;int cnt = 0;while (!q.empty()){int id = q.front().first;int step = q.front().second;q.pop();if(step > cur_step){cur_step = step;ans.push_back(cnt);cnt = 0;}if(tree[id].size() == 0) cnt++;for(int i = 0; i < tree[id].size(); i++)q.push(make_pair(tree[id][i], step + 1));}ans.push_back(cnt);return;}int main(){//fstream cin("a.txt");int N, M;cin>>N>>M;tree.resize(N + 1);for(int i = 0; i < M; i++){int id, num;cin>>id>>num;while (num--){int tmp;cin>>tmp;tree[id].push_back(tmp);}}BFS(1);for(int i = 0; i < ans.size(); i++){if(i == 0)cout<<ans[i];elsecout<<" "<<ans[i];}cout<<endl;}
- 【PAT Advanced Level】1004. Counting Leaves (30)
- PAT (Advanced Level) 1004. Counting Leaves (30)
- 【PAT】【Advanced Level】1004. Counting Leaves (30)
- 浙大 PAT Advanced level 1004. Counting Leaves
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- [PAT (Advanced Level) ]1004. Counting Leaves解题文档
- PAT (Advanced) 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT Advanced Level 1004:Counting Leaves
- PAT (Advanced Level) Practise 1004 Counting Leaves
- PAT (Advanced Level) 1004. Counting Leaves (30) 层序遍历,两个queue辅助
- UVa 127 - "Accordian" Patience
- 一道百度面试题
- 在linux下使用mysql的基础操作(二)
- ACM STEP 1.2.7 Lowest Bit
- 第四章总结(上)
- 【PAT Advanced Level】1004. Counting Leaves (30)
- Linux就是这个范儿之第一次亲密接触(3)
- WEB开发总结(持续更新。。。)
- 14条建议,使你的IT职业生涯更上一层楼
- php学习笔记 1
- 每天一小步——C语言中嵌入正则表达式
- (贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)
- 继承机制
- Pat1034