POJ 3321 Apple Tree
2016-01-23 18:45
323 查看
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
Sample Output
这道题主要是需要用dfs对树遍历一次,重新定义
每个节点的新下标,和此节点的子节点个数是多少,
然后用线段树或者树状数组都可以。
不过注意在POJ用vector会TLE。
树状数组:
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; }
相关文章推荐
- [iOS]URL编码和解码
- 高通android开发摘要
- 高通android开发摘要
- Android ActionBar的图标和标题
- iOS swift 字符属性NSAttributedString描述
- Android SDK 在线更新镜像服务器资源
- iOS 模拟器不弹出软键盘的解决办法
- Ubuntu14.04搭建android开发环境
- 【我所认知的BIOS】系列blog整理 1.23.2016.zip
- 报 Unable to resolve target 'android-19' 问题
- 【cocos2d-x制作别踩白块儿】第一期:游戏介绍
- 50个Android开发技巧(11 为文字加入特效)
- Core Animation1-简介
- Android 6.0 请求拨打电话error
- appcan更新
- Android API Level与sdk版本对照表
- iOS block在不同界面的回调传值
- Objective-c开发教程--MRC和ARC混编
- 最全的PHP开发Android应用程序
- Windows平台下Android应用抓包挖掘漏洞方法