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;
    }
    
  • 相关阅读:
    Tomcat系列教材 (一)- 教程
    反射机制系列教材 (四)- 调用方法
    反射机制系列教材 (五)- 有什么用
    反射机制系列教材 (三)- 访问属性
    【算法竞赛进阶指南】車的放置(行列模型二分图最大匹配+匈牙利算法)
    【算法竞赛进阶指南】棋盘覆盖(二分图最大匹配)
    【算法竞赛进阶指南】关押罪犯(二分+染色法判断二分图)
    数值计算实验三——拉格朗日插值和牛顿插值
    LDUOJ——2020级C语言测试1(顺序选择)
    codeforces859——C. Pie Rules(思维+DP)
  • 原文地址:https://www.cnblogs.com/yyroom/p/2966487.html
Copyright © 2011-2022 走看看