POJ-3321 Apple Tree---Tree array + DFS order

Topic link:

https://vjudge.net/problem/POJ-3321

Topic meaning:

The general idea of ​​the title is to give you a tree. Initially, there is an apple on each node. There are two operations: modify (that is, modify a certain node. When modifying, the apple of this node goes from having to nothing, or from nothing to nothing. Yes) and query (query how many apples there are in a node's subtree).

Problem solving ideas:

Each node of the tree is numbered and numbered according to the pre-order traversal, so that an interval can be processed, so that the interval is all subtrees of point i, and only the interval sum is required when summing.

Black is the original number, blue is the number obtained by preorder traversal

Super pit point:

Using vector<int>G[maxn] here will time out

Just use vector<vector<int> > G(maxn)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<string>
  6 #include<cmath>
  7 #include<set>
  8 #include<queue>
  9 #include<map>
 10 #include<stack>
 11 #include<vector>
 12 #include<list>
 13 #include<deque>
 14 #include<sstream>
 15 #include<cctype>
 16 #define REP(i,n) for(int i = 0; i < (n); i++)
 17 #define FOR(i, s, t) for(int i = (s); i < (t); i++)
 18 #define MEM(a, x) memset(a, x, sizeof(a));
 19 using namespace std;
 20 typedef long long ll;
 21 typedef unsigned long long ull;
 22 const int maxn = 1e5 + 10;
 23 const double eps = 1e-10;
 24 const int INF = 1 << 30;
 25 const int dir[4][ 2 ] = { 1 , 0 , 0 , 1 , 0 ,- 1 ,- 1 , 0 };
 26  const  double pi = 3.1415926535898 ;
 27  int T, n, m, cases, tot;
 28  int tree[maxn] , lef[maxn], rig[maxn], s[maxn]; /// The s array records whether the node has a tree 
29 vector<vector< int > > a(maxn);
 30  void dfs( int x)
 31  // / dfs Numbers tree nodes into left and right nodes,
 32 /// The left node is itself, the right node is the maximum number of reachable nodes,
 33  /// After querying the subtree of node x, you can directly query the interval of [le[x], rig[x]] and 
34  {
 35      lef[x] = tot;
 36      for ( int i = 0 ; i < a[x].size(); i++ )
 37      {
 38          tot++ ;
 39          dfs(a[x][i]);
 40      }
 41      rig [x] = tot;
 42  }
 43  int lowbit( int x)
 44  {
 45      return x&(- x);
 46  }
 47 int sum(int x)
 48 {
 49     int ret = 0;
 50     while(x > 0)
 51     {
 52         ret += tree[x];
 53         x -= lowbit(x);
 54     }
 55     return ret;
 56 }
 57 int add(int x, int d)
 58 {
 59     while(x <= n)
 60     {
 61         tree[x] += d;
 62         x += lowbit(x);
 63     }
 64 }
 65 int main()
 66 {
 67     int x, y;
 68     char c[5];
 69     while(scanf("%d", &n) != EOF)
 70     {
 71         MEM(lef, 0);
 72         MEM(rig, 0);
 73         MEM(s, 0);
 74         MEM(tree, 0);
 75         for(int i = 0; i < maxn; i++)a[i].clear();
 76         for(int i = 1; i < n; i++)
 77         {
 78             scanf("%d%d", &x, &y);
 79             a[x].push_back(y);
 80         }
 81         tot = 1;
 82         dfs(1);
 83         for(int i = 1; i <= n; i++)
 84         {
 85             s[i] = 1;
 86             add(i, 1);
 87         }
 88         scanf("%d", &m);
 89         while(m--)
 90         {
 91             scanf("%s%d", c, &x);
 92             if(c[0] == 'Q')
 93             {
 94                 cout << sum(rig[x]) - sum(lef[x] - 1 ) << endl;
 95              }
 96              else  if (c[ 0 ] == ' C ' )
 97              {
 98                  if (s[x])add(lef[x], - 1 );
 99                  else add(lef[x ], 1 );
 100                  s[x] = !s[x]; /// Update whether there is an apple at this point 
101              }
 102          }
 103      }
 104      return  0 ;
 105 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861837&siteId=291194637