POJ - 3321 Apple Tree
来源:互联网 发布:手机淘宝网卖家中心 编辑:程序博客网 时间:2024/05/08 14:10
1.题面
http://poj.org/problem?id=3321
2.题意
给你一个指定树根的树,每个节点上有一个为0或1的值,提供两种操作,1.是改变某个节点的值,在0,1之间进行转化,2.是询问某个节点子树中所有节点值之和。
3.思路
基本思想是一个子树的dfs时间戳在dfs序中是连接的。
这道题错了几发,因为没考虑到边的数量是点的两倍,没有开够节点。
4.代码
/***************************************************************** > File Name: Cpp_Acm.cpp > Author: Uncle_Sugar > Mail: uncle_sugar@qq.com > Created Time: 2016年07月18日 星期一 11时56分56秒*****************************************************************/# include <cstdio># include <cstring># include <cctype># include <cmath># include <cstdlib># include <climits># include <iostream># include <iomanip># include <set># include <map># include <vector># include <stack># include <queue># include <algorithm>using namespace std;const int debug = 1;const int size = 100000 + 100; const int INF = INT_MAX>>1;typedef long long ll;int n,m,tmp;struct edge{int to,nxt;}e[2*size];int e_tot;int head[size];void AddEdge(int u,int v){e[e_tot].to = v;e[e_tot].nxt = head[u];head[u] = e_tot++;e[e_tot].to = u;e[e_tot].nxt = head[v];head[v] = e_tot++;}int dfsn[size],sz[size];int dfs_clock;void dfs(int fa,int n){dfsn[n] = ++dfs_clock;for (int i=head[n];i!=-1;i=e[i].nxt){if (e[i].to!=fa){dfs(n,e[i].to);sz[n] += sz[e[i].to];}} sz[n]++;}int BIT[size];inline int lowbit(int x){return (x - (x&(x-1)));}void Add(int p,int v){while (p<=n){BIT[p] += v;p += lowbit(p);}}int Sum(int p){int ret = 0;while (p>0){ret += BIT[p];p -= lowbit(p);}return ret;}int rd[size];int main(){int i,j;char cmd[10];while (scanf("%d",&n)!=EOF){if (n>100000)while (1);dfs_clock = 0;e_tot = 0;memset(sz,0,sizeof(sz));memset(head,-1,sizeof(head));memset(BIT,0,sizeof(BIT));for (i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);AddEdge(a,b);}dfs(-1,1);for (i=1;i<=n;i++){Add(dfsn[i],1);rd[i] = 1;}scanf("%d",&m);while (m--){scanf("%s%d",cmd,&tmp);if (cmd[0]=='C'){if (rd[tmp]==1){rd[tmp] -= 1;Add(dfsn[tmp],-1);}else {rd[tmp]+= 1;Add(dfsn[tmp],1);}}else {int k = Sum(dfsn[tmp]+sz[tmp]-1) - Sum(dfsn[tmp]-1);printf("%d\n",k);}}}return 0;}
0 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
- 手把手教你做蓝牙聊天应用(六)-界面优化
- ThinkPad 备忘录
- Beyond Compare修改比较文件时间戳的方法
- Android调用系统软件打开文件(包括apk文件)
- 2016年团体程序设计天梯赛-决赛 L2-1红色警报
- POJ - 3321 Apple Tree
- 直接内存和非直接内存
- update-rc.d的具体用法
- 最新基于高德地图的android进阶开发(3)GPS地图定位
- AngularJS ng-model 指令
- 梯度下降原理及Python实现
- MyEclipse文件查找技巧
- ubuntu下安装 ipython notebook
- 利用zabbix API进行管理