• cogs 2039. 树的统计


    ★★   输入文件:counttree.in   输出文件:counttree.out   简单对比
    时间限制:1 s   内存限制:128 MB

    【题目描述】

    关于树的统计问题有多种多样的版本,这里你需要解决一个比较简单的问题:对于一棵包含N个节点的有根树,将所有点从1到N编号后,对于每一个节点v,统计出以v为根的子树中有多少个点的编号比v小。

    【输入格式】

    输入第一行包含一个整数N,以下N行每行包含一个整数,其中第i行的整数表示编号为i的节点的父亲节点的编号,根的父亲节点编号为0。

    【输出格式】

    输出包含N行,其中第i行给出编号为i的节点的统计结果。

    【样例输入】

    3
    2
    3
    0
    

    【样例输出】

    0 1 2
    

    【提示】

    在此键入。

    【来源】

    20%的数据1<=n<=1000

    100%的数据1<=n<=100000

    爆搜AC:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<string>
     7 #include<queue>
     8 
     9 using namespace std;
    10 const int N=100010;
    11 
    12 int head[N];
    13 int now=1;
    14 int n;
    15 queue<int>q;
    16 int tot;
    17 
    18 struct node 
    19 {
    20     int u,v,nxt;
    21 } E[N];
    22 
    23 inline int read() 
    24 {
    25     int x=0,f=1;
    26     char c=getchar();
    27     while(c<'0'||c>'9') 
    28     {
    29         if(c=='-')f=-1;
    30         c=getchar();
    31     }
    32     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    33     return x*f;
    34 }
    35 
    36 inline void add(int u,int v) 
    37 {
    38     E[now].u=u;
    39     E[now].v=v;
    40     E[now].nxt=head[u];
    41     head[u]=now;
    42     now++;
    43 }
    44 
    45 inline void bfs(int x) 
    46 {
    47     tot=0;
    48     q.push(x);
    49     while(!q.empty()) 
    50     {
    51         int top=q.front();
    52         q.pop();
    53         if(top<x)tot++;
    54         for(int i=head[top]; i!=-1; i=E[i].nxt) 
    55         {
    56             int v=E[i].v;
    57             q.push(v);
    58         }
    59     }
    60     printf("%d
    ",tot);
    61 
    62 }
    63 
    64 int main() 
    65 {
    66     freopen("counttree.in","r",stdin);
    67     freopen("counttree.out","w",stdout);
    68 
    69     n=read();
    70 
    71     for(int i=1; i<=n; i++)
    72         head[i]=-1;
    73     for(int i=1; i<=n; i++) 
    74     {
    75         int u=read();
    76         add(u,i);
    77     }
    78 
    79     for(int i=1; i<=n; i++) 
    80         bfs(i);
    81 
    82     return 0;
    83 
    84 }

     DFS序的做法80分:

     1 #include<algorithm>
     2 #include<cstdio>
     3 #define maxn 100001
     4 
     5 using namespace std;
     6 
     7 struct Edge
     8 {
     9     int vi;
    10     int vj;
    11     int next;
    12 }edge[maxn * 4];
    13 
    14 int topedge = 0;int topx = 0;
    15 int dfsx[maxn * 5];int b[maxn];int e[maxn];//在dfs序中出现次序 0
    16 int head[maxn];
    17 int kai;int N;
    18 
    19 inline int read()
    20 {
    21     int x=0,f=1;
    22     char c=getchar();
    23     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    24     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    25     return x*f;
    26 }
    27 
    28 void push(int vi,int vj)
    29 {
    30     topedge ++;
    31     edge[topedge].vj = vj;
    32     edge[topedge].next = head[vi];
    33     head[vi] = topedge;
    34 }
    35 
    36 void dfs(int x)
    37 {
    38     dfsx[++topx] = x;
    39     b[x] = topx;
    40     for(int i = head[x];i != 0 ;i = edge[i].next)
    41     {
    42         dfs(edge[i].vj);
    43     }
    44     e[x] = topx;
    45 }
    46 
    47 int main()
    48 {
    49     freopen("counttree.in","r",stdin);
    50     freopen("counttree.out","w",stdout);
    51     N = read();
    52     for(int i = 1;i <= N;i ++)
    53     {
    54         int num = read();
    55         if(num == 0)
    56         {
    57           kai = i;
    58           continue;    
    59         }
    60         push(num,i);
    61     }
    62     dfs(kai);
    63     for(int i = 1;i <= N;i ++)
    64     {
    65         int answ = 0;
    66         for(int j = b[i] + 1;j <= e[i];j ++)
    67             if(dfsx[j] < i)
    68                 answ ++;
    69         printf("%d
    ",answ);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    2019前端面试系列——CSS面试题
    面试题——数组转树结构
    前端安全
    webpack入门——构建简易版vue-cli
    [] == ![],走进==隐式转换的世界
    Vue图片懒加载插件
    JS常用时间处理方法
    Vue中实现token验证
    VSCode基本配置
    打乱数组——shuffle
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6910091.html
Copyright © 2020-2023  润新知