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 分)
73 0
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
94 0
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
116 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
76 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
83 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
94 0
|
测试技术
PAT (Advanced Level) Practice - 1029 Median(25 分)
PAT (Advanced Level) Practice - 1029 Median(25 分)
98 0
PAT (Advanced Level) Practice - 1030 Travel Plan(30 分)
PAT (Advanced Level) Practice - 1030 Travel Plan(30 分)
82 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
95 0
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
83 0