POJ 3321 Apple Tree
来源:互联网 发布:网络侮辱罪 编辑:程序博客网 时间:2024/05/17 02:33
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 toN 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 forku 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 forkx, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
这道题主要是需要用dfs对树遍历一次,重新定义
每个节点的新下标,和此节点的子节点个数是多少,
然后用线段树或者树状数组都可以。
不过注意在POJ用vector会TLE。
#include <stdio.h>#include <string.h>#define lson l, m, rt << 1#define rson m+1, r, rt << 1 | 1const 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 TLEvoid 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*2int 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;}
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- Poj 3321 Apple Tree
- poj 3321 apple tree
- poj 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ--3321--Apple Tree
- POJ-3321-Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- Theano安装
- 复现内存泄露的工具
- 求任意一颗二叉树的繁茂度
- Android Developer : Window Backgrounds & UI Speed
- 扩展卡尔曼滤波的原理及应用
- POJ 3321 Apple Tree
- Linux下的I/O重定向
- OpenStack中国联盟公开课20160105参会笔记
- LibGDX教程——重建Flappy Bird——(6) 碰撞检测及细节处理
- 高通android开发摘要
- linux GPRS模块拨号上网
- Android自带模拟器的安装
- BZOJ 3339: Rmq Problem|莫队算法
- 机器学习实战笔记——微软小冰的读心术与决策树