Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27309 | Accepted: 8098 |
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
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
Source
题意:给出一棵树,每个节点一开始都有一个苹果,这个树不是二叉树,可以是多叉树,有两种操作
1.Q 查询这个节点下面有多少果子
2.C 改变,如果有果子,去掉,没有,就加上
分析:首先,根据给出的节点建图,再用dfs找出每个节点所对应的区间,再来个树状数组或者线段树
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *head[110000];
long long c[110000];
int kaishi[110000],jieshu[110000],time=0;
int v[110000];
void jiantu(int x,int y)//可以逆序建链表,感觉更快
{
struct node *r,*q,*p;
if(head[x]==NULL)
{
head[x]=(struct node *)malloc(sizeof(struct node));
head[x]->data=y;
head[x]->next=NULL;
}
else
{
r=head[x];
while(r->next!=NULL)
{
r=r->next;
}
q=r;
p=(struct node *)malloc(sizeof(struct node));
p->data=y;
q->next=p;
p->next=NULL;
}
}
void dfs(int x)//找出节点对应的区间
{
kaishi[x]=time;
struct node *p;
p=head[x];
while(p!=NULL)
{
time++;
dfs(p->data);
p=p->next;
}
jieshu[x]=time;
}
int lowbit(int k)
{
return k&(-k);
}
//void input(int x)
//{
// int y=lowbit(x);
// c[x]+=y;
//}
long long he(int x)
{
long long sum=0;
while(x>=1)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void change(int k,int x,int n)
{
while(k<=n)
{
c[k]+=x;
k+=lowbit(k);
}
}
int main()
{
int n,m,i,j;
int x,y;
while(~scanf("%d",&n))
{
time=1;
memset(c,0,sizeof(c));
for(i=1; i<=n; i++)
{
head[i]=NULL;
}
for(i=1; i<=n; i++)
{
change(i,1,n);
}
for(i=1; i<n; i++)
{
scanf("%d%d",&x,&y);
jiantu(x,y);
}
dfs(1);
// for(i=1; i<=n; i++)
// {
// struct node *p;
// p=head[i];
// while(p)
// {
// printf("%d ",p->data);
// p=p->next;
// }
// printf("\n");
// }
// for(int i=1;i<=n;i++)
// {
// printf("%d %d\n",kaishi[i],jieshu[i]);
// }
char s[10];
int g;
scanf("%d",&m);
memset(v,0,sizeof(v));
while(m--)
{
scanf("%s%d",s,&g);
if(s[0]=='Q')
{
long long a=he(jieshu[g]);
long long b=he(kaishi[g]-1);
printf("%lld\n",a-b);
}
if(s[0]=='C')
{
if(!v[g])
{
change(kaishi[g],-1,n);
v[g]=1;
}
else
{
change(kaishi[g],1,n);
v[g]=0;
}
}
}
}
return 0;
}