zoukankan      html  css  js  c++  java
  • 树的遍历 | 1004 广义树的层序遍历

    刚接触pat的时候就做过这个题。当时被难得要死,连题都看不懂,一脸蒙蔽。

    时过境迁,今天一看这题就笑了,太简单了。对着电脑一顿操作,就AC了。

    AC代码:

    #include <stdio.h>
    #include <memory.h>
    #include <math.h>
    #include <string>
    #include <vector>
    #include <set>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    
    #define I scanf
    #define OL puts
    #define O printf
    #define F(a,b,c) for(a=b;a<c;a++)
    #define FF(a,b) for(a=0;a<b;a++)
    #define FG(a,b) for(a=b-1;a>=0;a--)
    #define LEN 1010
    #define MAX (1<<30)-1
    #define V vector<int>
    
    using namespace std;
    
    typedef struct Node{
        int id;
        vector<int> l;    //leaves
    }Node;
    
    Node nd[LEN];
    
    int main(){
    //    freopen("1004.txt","r",stdin);
        int n,m,k,t,i,id;
        I("%d%d",&n,&m);
        while(m--){
            I("%d%d",&id,&k);
            while(k--){
                I("%d",&t);
                nd[id].l.push_back(t);
            }
        }
        queue<int> q;
        q.push(1);
        vector<int> ans;
        while(!q.empty()){
            int sz=q.size();
            int cnt=0;
            while(sz--){
                t=q.front();
                q.pop();
                if(nd[t].l.size()){
                    FF(i,nd[t].l.size()) q.push(nd[t].l[i]);
                }else
                    cnt++;    
            }
            ans.push_back(cnt);
        }
        FF(i,ans.size()){
            O("%d",ans[i]);
            if(i!=ans.size()-1) O(" ");
        }
        return 0;
    }
  • 相关阅读:
    hdu 5101 Select
    hdu 5100 Chessboard
    cf B. I.O.U.
    cf C. Inna and Dima
    cf B. Inna and Nine
    cf C. Counting Kangaroos is Fun
    Radar Installation 贪心
    spfa模板
    Sequence
    棋盘问题
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8550996.html
Copyright © 2011-2022 走看看