浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
来源:互联网 发布:sql 编辑器 编辑:程序博客网 时间:2024/05/12 15:49
#include <iostream> #include <cstdio>#include <map>#include <vector>using namespace std;/*采用深度搜索方法,获取每一层的叶子节点,用map存储数据,省去了建立树的麻烦,此方法更为简便*/void dfs(map<int, vector<int> > &tree, map<int,int> &res, int id, int level){if(tree.count(id) == 0){//如果没有子节点,相应的叶子节点+1 ++res[level];return;}for(int i = 0; i < tree[id].size(); ++i){//循环访问每一个子树dfs(tree, res, tree[id][i], level+1);}}int main(){int n,m;map<int, vector<int> > tree;//用map按照结点与其子节点存取树map<int, int> res;scanf("%d %d",&n,&m);for(int i = 0; i < m; ++i){//读取数据int id, k;scanf("%d %d", &id, &k);while(k-- > 0){int tmp;scanf("%d",&tmp);tree[id].push_back(tmp);}}dfs(tree, res, 1, 1);//深度搜索获得结果map<int,int>::iterator it = --res.end();int maxLevel = (--res.end())->first;//获取最大层数for(int i = 1; i <= maxLevel; ++i){//输出结果if(i == 1){cout<<res[i];}else{cout<<" "<<res[i];}}return 0; }
0 0
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 浙大 PAT Advanced level 1004. Counting Leaves
- 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) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004 Counting Leaves
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- 【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 (30) 解题报告
- 浙大PAT (Advanced Level) Practise 1006
- 浙大PAT (Advanced Level) Practise 1007
- [PAT (Advanced Level) ]1004. Counting Leaves解题文档
- PAT (Advanced) 1004. Counting Leaves (30)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- 5个步骤,将 storyboard 从 iphone 版转变为 ipad 版
- UVA - 10534 Wavio Sequence
- hadoop命令行运行wordcount程序 web端监控不到作业状态
- ACM计算几何题目推荐(转)
- 【FFT】 HDOJ 4609 3-idiots
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 动态规划 钢条分割 矩阵链乘 最长公共子序列 最优二叉树
- 安装Hadoop2.3.0注意事项
- swift
- Radar Installation
- 开源重塑软件开发
- Java——PriorityQueue
- hud1151 动态规划 最大的公共子序列
- 包和访问权限(三)