The main idea: to give a tree, the beginning of every two points are connected by dirt road, but there will be some dirt road gradually into the road, asked each time from point 1 to the number of points K how many dirt road.
Ideas: POI is not difficult problem, in fact, each point to 1 of the number of dirt road is the depth of this point, in the dirt road into the road, the point and the subtree of all the nodes of the depth of 1, sub-tree modification is very basic, you can use the DFS sequence +fenwick, of course, if not too troublesome can also be tree chain But constants will compare cards.
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 250010using namespace Std;int points,asks;int head[max],total;int _next[max << 1],aim[max << 1];int Deep[max ],cnt;pair<int,int> range[max];int fenwick[max];char c[10];inline void Add (int x,int y) {_next[++total] = head[x]; Aim[total] = y;head[x] = total;} void DFS (int x,int last) {deep[x] = Deep[last] + 1;range[x].first = ++cnt;for (int i = head[x]; i; i = _next[i]) {if (Aim[i] = = last) Continue;dfs (aim[i],x);} Range[x].second = CNT;} inline void Fix (int x,int c) {for (; x; x = x&-x) fenwick[x]-= c;} inline int getsum (int x) {int re = 0;for (; x <= points; x + = x&-x) Re + = Fenwick[x];return Re;} int main () {cin >> points;for (int x,y,i = 1; i < points; ++i) {scanf ("%d%d", &x,&y); Add (x, y), add (y,x);} Deep[0] = -1;dfs (1,0), cin >> asks;for (int x,y,i = 1; I <= asks + points-1; ++i) {scanf ("%s", c); if (c[0] = = ' A ') {scanf ("%d%d", &x,&y); intopt = deep[x] > deep[y]? x:y; Fix (range[opt].first-1,1); Fix (range[opt].second,-1);} else {scanf ("%d", &x);p rintf ("%d\n", Deep[x]-getsum (Range[x].first));}} return 0;}
Bzoj 1103 POI 2007 Metropolitan Meg Tree Array