POJ 3321 Apple Tree

Description

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?

Input

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

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2



這道題主要是需要用dfs對樹遍歷一次,重新定義

每個節點的新下標,和此節點的子節點個數是多少,

然後用線段樹或者樹狀數組都可以。

不過注意在POJ用vector會TLE。

#include <stdio.h>
#include <string.h>
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
const int maxn = 100005;
int head[maxn << 1], next[maxn << 1], to[maxn << 1];
int vis[maxn], len[maxn], p;
int a[maxn << 4], pos[maxn], cnt, sub[maxn];
//只能用數組建鄰接表,vector TLE
void init ( int n )
{
    memset ( vis, 0, sizeof ( vis ) );
}
void dfs ( int u )
{
    vis[u] = 1;
    pos[u] = ++ cnt;
    for ( int i = head[u]; i; i = next[i] )
    {
        int v = to[i];
        if ( vis[v] == 0 )
            dfs ( v );
    }
    len[u] = cnt;
}
void add ( int u, int v )
{
    p ++;
    to[p]  = v;
    next[p] = head[u];
    head[u] = p;
}
void print ( int a[], int n )
{
    for ( int i = 1; i <= n; i ++ )
        printf ( "%d ", a[i] );
    printf ( "\n" );
}
void Build ( int l, int r, int rt )
{
    a[rt] = r-l+1;
    if ( l == r )
        return ;
    int m = ( l+r ) >> 1;
    Build ( lson );
    Build ( rson );
}
void PushUP ( int rt )
{
    a[rt] = a[rt << 1]+a[rt << 1 | 1];
}
void update ( int q, int l, int r, int rt )
{
    if ( l == r )
    {
        a[rt] = ! a[rt];
        return ;
    }
    int m = ( l+r ) >> 1;
    if ( q <= m )
        update ( q, lson );
    else
        update ( q, rson );
    PushUP ( rt );
}
int query ( int L, int R, int l, int r, int rt )
{
    if ( L <= l && r <= R )
        return a[rt];
    int m = ( l+r ) >> 1, ret = 0;
    if ( L <= m )
        ret += query ( L, R, lson );
    if ( R > m )
        ret += query ( L, R, rson );
    return ret;
}
int main ( )
{
    int n, u, v, m, x;
    char op[2];
    scanf ( "%d", &n );
    init ( n );
    for ( int i = 1; i < n; i ++ )
    {
        scanf ( "%d%d", &u, &v );
        add ( u, v );
        add ( v, u );   //添加了兩次,所以數組翻一倍
    }
    dfs ( 1 );  //最核心部分 映射新下標起點和終點
    Build ( 1, n, 1 );
    scanf ( "%d", &m );
    while ( m -- )
    {
        scanf ( "%s%d", op, &x );
        if ( op[0] == 'C' )
            update ( pos[x], 1, n, 1 );
        else
            printf ( "%d\n", query ( pos[x], len[x], 1, n, 1 ) );
    }
    return 0;
}

樹狀數組:

#include <stdio.h>
#include <string.h>
const int maxn = 100005;
int head[maxn << 1], next[maxn << 1], to[maxn << 1], p;
//因爲加了兩條邊所以應該是maxn*2
int vis[maxn], len[maxn], n;
int a[maxn], pos[maxn], cnt, tag[maxn];
void init ( int n )
{
    memset ( vis, 0, sizeof ( vis ) );
    memset ( head, -1, sizeof ( head ) );
}
void add ( int u, int v )
{
    p ++;
    to[p] = v;
    next[p] = head[u];
    head[u] = p;
}
void dfs ( int u )
{
    vis[u] = 1;
    pos[u] = ++ cnt;    //重新定義節點的下標
    for ( int i = head[u]; ~ i; i = next[i] )
    {
        if ( vis[ to[i] ] == 0 )
            dfs ( to[i] );
    }
    len[u] = cnt;   //子節點的最大新下標
}
void print ( int a[], int n )
{
    for ( int i = 1; i <= n; i ++ )
        printf ( "%d ", a[i] );
    printf ( "\n" );
}
int lowbit ( int x )
{
    return x & -x;
}
void ad ( int x, int val )
{
    while ( x <= n )
    {
        a[x] += val;
        x += lowbit ( x );
    }
}
int sum ( int x )
{
    int ret = 0;
    while ( x > 0 )
    {
        ret += a[x];
        x -= lowbit ( x );
    }
    return ret;
}
int main ( )
{
    int u, v, m, x;
    char op[2];
    scanf ( "%d", &n );
    init ( n );
    for ( int i = 1; i < n; i ++ )
    {
        scanf ( "%d%d", &u, &v );
        add ( u, v );
        add ( v, u );
    }
    dfs ( 1 );
    for ( int i = 1; i <= n; i ++ )
        ad ( i, 1 );
    scanf ( "%d", &m );
    while ( m -- )
    {
        scanf ( "%s%d", op, &x );
        if ( op[0] == 'C' )
        {
            if ( tag[x] )
                ad ( pos[x], 1 );
            else
                ad ( pos[x], -1 );
            tag[x] = ! tag[x];
        }
        else
        {
            printf ( "%d\n", sum ( len[x] )-sum ( pos[x]-1 ) );
            //sum(r)-sum(l-1)即爲[l,r]區間值
        }
    }
    return 0;
}


發佈了20 篇原創文章 · 獲贊 21 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章