【PAT】【Advanced Level】1004. Counting Leaves (30)
2017-07-11 19:39
543 查看
1004. Counting Leaves (30)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end
of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.
Sample Input
2 1 01 1 02
Sample Output
0 1
原题链接:
https://www.patest.cn/contests/pat-a-practise/1004
思路:
map匹配节点在数组中的下标,类似于索引功能
先读入数据,添加父子关系
然后DFS,确定层数并统计各层目标点个数
最后输出
CODE:
#include<iostream> #include<cstring> #include<string> #include<map> #include<vector> #define N 101 using namespace std; typedef struct no { string id; string son ; int ns; }; map<string,int> pos; int m,n; no p ; int mf=0; int outp ; void dfs(string r,int fl) { mf=max(mf,fl); if (p[pos[r]].ns==0) { outp[fl]++; return ; } for (int i=0;i<p[pos[r]].ns;i++) { dfs(p[pos[r]].son[i],fl+1); } return; } int main() { cin>>n>>m; int num=1; for (int i=0;i<m;i++) { string a; cin>>a; int t; if (pos[a]==0) { p[num].id=a; p[num].ns=0; pos[a]=num; t=num; num++; } else { t=pos[a]; } int k; cin>>k; for (int j=0;j<k;j++) { string ss; cin>>ss; p[t].son[p[t].ns]=ss; p[t].ns++; if (pos[ss]==0) { p[num].id=ss; p[num].ns=0; pos[ss]=num; num++; } } } memset(outp,0,sizeof(outp)); dfs("01",0); for (int i=0;i<mf;i++) cout<<outp[i]<<" "; cout<<outp[mf]; return 0; }
相关文章推荐
- PAT (Advanced Level) 1030. Travel Plan (30)
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30) 中序遍历往BST中填数据
- PAT (Advanced Level) Practise 1107 Social Clusters (30)
- PAT (Advanced Level) Practise 1045 Favorite Color Stripe (30)
- PAT (Advanced Level) Practise 1026. Table Tennis (30)
- PAT (Advanced Level) Practise 1014. Waiting in Line (30)
- 【PAT】【Advanced Level】1045. Favorite Color Stripe (30)
- 1026. Table Tennis (30)【排序+模拟+逻辑复杂】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1072. Gas Station (30)
- 【PAT】【Advanced Level】1057. Stack (30)
- PAT(Advanced Level)1111. Online Map (30)
- PAT (Advanced Level) Practise 1057 Stack (30)
- PAT (Advanced Level) Practise 1095 Cars on Campus (30)
- PAT (Advanced Level) 1022. Digital Library (30) 数字图书馆 map
- PAT (Advanced Level) 1049. Counting Ones (30) 1到N中1出现的次数
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- PAT (Advanced Level) Practise 1099 Build A Binary Search Tree (30)
- PAT (Advanced Level) 1076. Forwards on Weibo (30)
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1049 Counting Ones (30)