zoukankan      html  css  js  c++  java
  • nyist 231 Apple Tree

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

    The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

    The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

    输入
    The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
    The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
    The next line contains an integer M (M ≤ 100,000).
    The following M lines each contain a message which is either
    "C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
    or
    "Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
    Note the tree is full of apples at the beginning
    输出
    For every inquiry, output the correspond answer per line.
    样例输入
    3
    1 2
    1 3
    3
    Q 1
    C 2
    Q 1
    样例输出
    3
    2


    代码:

    #include<iostream>
    #include <cstdio>
    
    using namespace std;
    
    #define N 100005
    
    typedef struct node
    {
        int sum ;//和值
        int data;
        int l;//左孩子
        int r;//右孩子
        int f;//父地址
        node()
        {
            sum = 1;
            l = 0;
            data = 1;
            r = 0;
            f = 0;
        }
    
    }node;
    
    node a[N];
    
    
    void add(int x,int y)
    {
        a[x].sum += a[y].data;
        if(a[x].l)
            a[x].r = y;
        else
            a[x].l = y;
    
            a[y].f = x;
            int w = a[x].f;
        while(w)
        {
            a[w].sum += 1;
            w = a[w].f;
        }
    
    }
    
    void inset(int x)
    {
        if(a[x].data)
        {
            a[x].data = 0;
            a[x].sum += -1;
    
            int y = x;
    
            while(y)
            {
                y = a[y].f;
                a[y].sum += -1;
            }
        }
    
        else
        {
            a[x].data = 1;
            a[x].sum += 1;
    
            int y = x;
    
            while(y)
            {
                y = a[y].f;
                a[y].sum += 1;
            }
        }
    }
    
    int query(int x)
    {
    
        return a[x].sum;
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        int i = 0;
        for(i = 1; i < n; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
    
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            getchar();
            char ch;
            scanf("%c",&ch);
            int kk =0;
            scanf("%d",&kk);
            if(ch == 'Q')
            {
                printf("%d\n",query(kk));
            }
            else
            inset(kk);
        }
        return 0;
    }
    
  • 相关阅读:
    Pytest学习笔记(二) 用例执行规则
    Pytest学习笔记(一) 环境安装及入门
    Jmeter(十四)取样器之JDBC Request
    Jmeter(十三)阶梯式压测
    Jmeter(十二)常用插件
    Fsync理解
    PostgreSQL checkpoint_completion_target及脏数据刷盘过程说明
    PostgreSQL synchronous_commit参数确认,以及流复制的思考
    性能测试之nmon对linux服务器的监控(转)
    Postgresql的pgcrypto模块(转)
  • 原文地址:https://www.cnblogs.com/yyroom/p/2966487.html
Copyright © 2011-2022 走看看