PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)

简介: PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)

题目链接:点击打开链接

题目大意:求每一层的叶子节点个数(从 根节点 (1th 层) 开始)。

解题思路:略。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
map<int,vector<int> > miv;
int rcd[110];
int len;
void dfs(int pid,int lvl)
{
    if(miv[pid].empty())
    {
        ++rcd[lvl];
        len=max(len,lvl);
        return;
    }
    for(vector<int>::iterator it=miv[pid].begin(); it!=miv[pid].end(); it++)
        dfs(*it,lvl+1);
}
int main()
{
    int n,m,pid,cid,k;
    while(~scanf("%d%d",&n,&m))
    {
        mem(rcd,0); miv.clear(); len=0;
        while(m--)
        {
            scanf("%d%d",&pid,&k);
            while(k--)
            {
                scanf("%d",&cid);
                miv[pid].push_back(cid);
            }
        }
        dfs(1,0);
        printf("%d",rcd[0]);
        for(int i=1;i<=len;i++)
            printf(" %d",rcd[i]);
        puts("");
    }
    return 0;
}
目录
相关文章
|
存储 算法
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
93 0
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
110 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
120 0
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
147 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
121 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
128 0
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
105 0
|
测试技术
PAT (Advanced Level) Practice - 1029 Median(25 分)
PAT (Advanced Level) Practice - 1029 Median(25 分)
120 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
106 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
121 0