POJ 3321 Apple Trie

Source: Internet
Author: User

/* POJ 3321 Apple Trie The key to this problem is how to build a tree into a one-dimensional array using a tree-like array to solve problems!   Can be done with DFS (), we after a deep search of a node, the number of nodes passed is the number of subtree of that node, so we use Start[i] array to record I node in one-dimensional array starting position, and End[i] is the record I node all children Node the last child node in the array position, then end[i]-start[i]+1, is the I node (including itself) and the number of all its child nodes. Number is set up, then the last is to apply a tree-like array template to solve! */#include <iostream> #include <vector> #include <cstring> #include <cstdio> #define N    100005using namespace Std;class node{public:int K;    Node *next;    Node () {next=null;    }};node Trie[n];//trie[i] record is all I node child nodes composed of the head of the list 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)//traverse all child nodes of the cur node {++cnt;    DFS (P-&GT;K); end[cur]=cnt;//the CNT value obtained after deep search is the cur node's last child's position in a one-dimensional array}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;}

/*
This problem uses two-dimensional number to build a diagram can also be too, but the size of the array is really elusive ....
*/
#include <iostream> #include <vector> #include <cstring> #include <cstdio> #define N 100005using 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;}

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.