1004. Counting Leaves (30)[bfs]
来源:互联网 发布:车票软件下载 编辑:程序博客网 时间:2024/05/24 05:49
1. 原题:https://www.patest.cn/contests/pat-a-practise/1004
2. 思路:
题意就是输出树的每层叶子节点数。
无需构建树,通过DFS或者BFS遍历就好。
这里采用bfs。
无需构建树,通过DFS或者BFS遍历就好。
这里采用bfs。
已AC。
3. 源码:
#include <iostream>#include <vector>#include <queue>using namespace std;int N, M;vector< vector<int> > T;void bfs();int main(){freopen("in.txt", "r", stdin);scanf("%d %d", &N, &M);T.resize(N + 1);for (int i = 0; i < M; i++){int parent, k;scanf("%d %d", &parent, &k);T[parent].resize(k);for (int j = 0; j < k; j++){scanf("%d", &T[parent][j]);}}bfs();return 0;}void bfs(){queue<int> Q;Q.push(1);int current, cnt;int last = 1;cnt = 0;while (!Q.empty()){int node = Q.front();Q.pop();if (T[node].empty()){cnt++;}else{for (int i = 0; i < T[node].size(); i++){Q.push(T[node][i]);current = T[node][i];}}if (last == node){if (node != 1)printf(" ");printf("%d", cnt);cnt = 0;last = current;}}printf("\n");return;}
阅读全文
0 0
- 1004. Counting Leaves (30)[bfs]
- 1004. Counting Leaves (30) PAT+BFS
- PAT 1004. Counting Leaves (30)(BFS)
- 1004. Counting Leaves (30)(*****dfs/bfs)
- 1004. Counting Leaves (30) 树的遍历 DFS BFS
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 20170731~20170802
- Codeforces Round #427 (Div. 2)【solved:4 / 6】
- 题目33——蛇形填数
- DB2中NULL字段的显示问题
- java做项目的顺序
- 1004. Counting Leaves (30)[bfs]
- handle message Looper源码解析
- C language manual
- 2017年8月7日 星期一
- caffe 学习教程
- 打印宏
- hadoop2.x完全分布式环境搭建
- gan train real_test
- Java包装类与基本类型进行比较的坑