zoukankan      html  css  js  c++  java
  • CodeForces

    题目:

    给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实。

    思路:

    dfs直接搜索统计这棵树的每一层上有多少个果实就可以了。如果是奇数个ans++,偶数个不作处理。

    代码:

    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <iomanip>
    #define MAX 1000000000
    #define inf 0x3f3f3f3f
    #define FRE() freopen("in.txt","r",stdin)
    
    using namespace std;
    typedef long long ll;
    const int maxn = 100005;
    int n,x,ans=0;
    int deep[maxn];
    vector<int> mp[maxn];
    
    void init()
    {
        memset(deep,0,sizeof(deep));
        for(int i=0; i<maxn; i++)
        {
            mp[i].clear();
        }
    }
    
    void dfs(int cur,int level)
    {
        deep[level]++;
        for(int i=0; i<mp[cur].size(); i++)
        {
            dfs(mp[cur][i],level+1);
        }
        return;
    }
    
    int main()
    {
        init();
        scanf("%d",&n);
        for(int i=2; i<=n; i++)
        {
            scanf("%d",&x);
            mp[x].push_back(i);
        }
        dfs(1,1);
        int ans = 0;
        for(int i=1; i<=n; i++)
        {
            if(deep[i]%2)
                ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    知识点整理
    NGINX 内存池有感
    NGINX怎样处理惊群的
    NGINX 定时器
    制作linux内核安装包
    ES6变量的解构赋值
    jquery uploadify上传插件用法心得
    【转贴】J2EE中的13种技术规范
    【转帖】Servlet 3.0 新特性详解
    汉诺塔问题的一个C#实现
  • 原文地址:https://www.cnblogs.com/sykline/p/10539226.html
Copyright © 2011-2022 走看看