1004. Counting Leaves (30)

来源:互联网 发布:mac装win10后启动黑屏 编辑:程序博客网 时间:2024/06/07 23:22

PAT 1004 用一个 BFS 搞定 哈哈



#include<iostream>

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn=105;
const int INF=1000000000;
struct E{
    int from,to;
    E(int from,int to):
        from(from),to(to){}
    bool operator <(const E &rhs) const{
        return this->from<rhs.from;
    }
};
vector<E> edges;
vector<int> G[maxn];
int deep[maxn];
int deep_zero[maxn];
void init()
{
    for(int i=0;i<maxn;i++)
    {
        G[i].clear();
        deep[i]=INF;
        deep_zero[i]=0;
    }
    edges.clear();
    deep[1]=1;
}
void addEdge(int from,int to)
{
    edges.push_back(E(from,to));
    G[from].push_back(edges.size()-1);
}
int main()
{
#ifdef LOCAL
    freopen("data.in","r",stdin);
#endif
    int n,m,root,nums,child;
    init();
    scanf("%d%d",&n,&m);
    for(int j=1;j<=m;j++)
    {
    scanf("%d%d",&root,&nums);
    for(int i=1;i<=nums;i++)
    {
            scanf("%d",&child);
            addEdge(root,child);
    }
    }
    queue<int> nodes;
    nodes.push(1);
     deep[1]=1;
    int max_deep=1;
    while(!nodes.empty())
    {
        int from=nodes.front(); nodes.pop();
        for(int i=0;i<G[from].size();i++)
        {
            int to=edges[G[from][i]].to;
            deep[to]=deep[from]+1;
            nodes.push(to);
            if(deep[to]>max_deep)
                max_deep=deep[to];
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(G[i].size()==0)
           deep_zero[deep[i]]+=1;
    }
    for(int i=1;i<=max_deep;i++)
    {
        cout<<deep_zero[i];
        if(i!=max_deep)
            cout<<" ";
    }
    cout<<endl;
    return 0;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 贫僧还俗了 逆流香江时代 我于教坊司中度长生 木叶,我成了大筒木辉夜的小号 长生妖道 大随圣人:从天牢自救开始 重生妖禽,加载了山海经游戏模板 虫族在晶壁系世界 诡雾临城,从眼镜蛇开始进化 重生之学霸男神 华娱,我真不想当明星 我的女友是双胞胎 赛亚人只是路过而已 全民转职,谁敢说剑客是垃圾职业 人在秦时,三无剑客 水浒之换了青天 轴承曝光我成首席科学家 诸神的轻语 超神:每天一个新马甲 逢魔时刻,从满级悟性开始 大主宰之冰封万里 从布斯巴顿到霍格沃茨 大乾镇魔人 足球之我喝口水都能变强 我被七个妖女囚禁了 DC人间之神 绝世唐门之太阳 混在港综世界当枭雄 龙族:蓝染魂穿路明非 超级垂钓养殖系统 stm32移植xcp协议 xcp协议 程女士 Java在一个5个数的数组中,输出逆序数(在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的 在一个5个数的数组中,输出逆序数 http://www.aqdlt.com/ Android应用逆向——分析反编译代码之大神器 公众号菜单 xcodeCommandPhaseScriptExecutionfailedwithanonzero xcodeCommandPhaseScriptExecutionfailedwithanonzero xcode升级