poj 3321 Apple Trie

/*
  poj 3321 Apple Trie
  这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!
  可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目
  所以我们利用start[i]数组来记录 i 节点在一维数组的起始位置, 而end[i]则是记录i节点所有孩子
  节点最后一个孩子节点在数组的位置,那么end[i]-start[i]+1,就是 i 节点(包括自身)和其所有孩子节点的
  数目。数组建好了,那么最后就是套用树状数组模板进行求解了!
*/
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
class node
{
public :
    int k;
    node *next;
    node()
    {
    	next=NULL;
    }
};

node trie[N];
//trie[i]记录的是所有是 i 节点 孩子节点组成的链表的头部
int C[N], num[N];
int start[N], end[N];
int cnt, n;

void dfs(int cur)
{
    start[cur]=cnt;
    if(trie[cur].next==NULL)
    {
    	end[cur]=cnt;
        return;
    }
    for(node *p=trie[cur].next; p!=NULL; p=p->next)//遍历cur节点的所有孩子节点
    {
    	++cnt;
    	dfs(p->k);
    }
    end[cur]=cnt;//深搜之后得到的cnt值就是cur节点最后一个孩子在一维数组中的位置
}

int lowbit(int x)
{
   return x&(-x);
}

void init(int p, int k)
{
   int i;
   num[p]=k;
   for(i=p-lowbit(p)+1; i<=p; ++i)
      C[p]+=num[i];
}

int getSum(int p)
{
    int s=0;
    while(p>0)
    {
    	s+=C[p];
    	p-=lowbit(p);
    }
    return s;
}

void update(int p, int k)
{
    while(p<=n)
    {
    	C[p]+=k;
    	p+=lowbit(p);
    }
}

int main()
{
   int i, u, v, m;
   char ch[2];
   int f;
   while(scanf("%d", &n)!=EOF)
   {
      cnt=1;
      memset(C, 0, sizeof(C));
      for(i=1; i<n; ++i)
      {
      	  scanf("%d%d", &u, &v);
      	  node *p=new node();
      	  p->k=v;
      	  p->next=trie[u].next;
      	  trie[u].next=p;
      }
      dfs(1);
      for(i=1; i<=n; ++i)
         init(i, 1);
      scanf("%d", &m);
      while(m--)
      {
	 scanf("%s%d", ch, &f);
	 if(ch[0]==‘C‘)
	 {
	     if(num[f]==1)
	       {
	          update(start[f], -1);
	          num[f]=0;
	       }
	     else
	       {
	       	  update(start[f], 1);
	       	  num[f]=1;
	       }
	 }
	 else
	    printf("%d\n", getSum(end[f])-getSum(start[f]-1));
      }
   }
   return 0;
}
/*   这道题利用二维数组建图也可以过,不过数组的大小还真是难以捉摸....*/#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
int node[N][100];
int C[N], num[N];
int start[N], end[N];
int cnt, n;

void dfs(int cur)
{
    int sz=node[cur][0];
    start[cur]=cnt;
    if(sz==0)
    {
    	end[cur]=cnt;
        return;
    }
    for(int i=1; i<=sz; ++i)
    {
    	++cnt;
    	dfs(node[cur][i]);
    }
    end[cur]=cnt;
}

int lowbit(int x)
{
   return x&(-x);
}

void init(int p, int k)
{
   int i;
   num[p]=k;
   for(i=p-lowbit(p)+1; i<=p; ++i)
      C[p]+=num[i];
}

int getSum(int p)
{
    int s=0;
    while(p>0)
    {
    	s+=C[p];
    	p-=lowbit(p);
    }
    return s;
}

void update(int p, int k)
{
    while(p<=n)
    {
    	C[p]+=k;
    	p+=lowbit(p);
    }
}

int main()
{
   int i, u, v, m;
   char ch[2];
   int f;
   while(scanf("%d", &n)!=EOF)
   {
      cnt=1;
      for(i=1; i<=n; ++i)
         node[i][0]=0;
      memset(C, 0, sizeof(C));
      for(i=1; i<n; ++i)
      {
      	  scanf("%d%d", &u, &v);
      	  node[u][++node[u][0]]=v;
      }
      dfs(1);
      for(i=1; i<=n; ++i)
         init(i, 1);
      scanf("%d", &m);
      while(m--)
      {
	 scanf("%s%d", ch, &f);
	 if(ch[0]==‘C‘)
	 {
	     if(num[f]==1)
	       {
	          update(start[f], -1);
	          num[f]=0;
	       }
	     else
	       {
	       	  update(start[f], 1);
	       	  num[f]=1;
	       }
	 }
	 else
	    printf("%d\n", getSum(end[f])-getSum(start[f]-1));
      }
   }
   return 0;
}

  

  

poj 3321 Apple Trie,布布扣,bubuko.com

时间: 2024-10-12 03:08:59

poj 3321 Apple Trie的相关文章

poj 3321:Apple Tree(树状数组,提高题)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 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

POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

POJ - 3321 Apple Tree Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 muc

【树状数组】POJ 3321 Apple Tree

/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

POJ 3321 Apple Tree 【树形结构转变为线性结构+线段树OR树状数组】

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21587   Accepted: 6551 POJ 3321链接: 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

POJ 3321 Apple Tree 【树状数组+建树】

题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34812 Accepted: 10469 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka like

POJ 3321 Apple Tree (树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21191   Accepted: 6436 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

POJ 3321 Apple Tree

题目链接:http://poj.org/problem?id=3321 解题思路:dfs加时间戳然后简单树状数组单点更新区间查询即可. 代码: 1 const int maxn = 1e5 + 5; 2 struct Edge{ 3 int to, next; 4 }; 5 Edge edges[maxn]; 6 int head[maxn], tot; 7 int st[maxn], ed[maxn], cnt; 8 int bit[maxn], status[maxn], n, m; 9 1

#5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 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