Problem descriptionyou is given a tree, it's root is P, and the node was numbered from 1 to N. Now define F (i) as the number of nodes whose number are less than I in all the succeeding nodes of node I. Now we need to calculate F (i) for any possible I.
Inputmultiple cases (no more than), for each case:
The first line contains-integers n (0<n<=10^5) and p, representing this tree have n nodes, its root is P.
Following n-1 lines, each of which has a integers, representing an edge on this tree.
The input terminates with the zeros.
Outputfor each test case, Output n integer in one line representing F (1), F (2) ... f (n), separated by a space.
Sample Input
15 77 107 17 97 37 410 1414 214 139 119 66 56 83 153 120 0
Sample Output
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
Test instructions: Takes x as the subtree weight of the root node and. The problem: After obtaining the DFS sequence, it is simple summation question, bit/line segment tree can be. because the title is smaller than the current x, we want to update from the big start. Do not want to manually simulate please Gar
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring> #include < algorithm> #include <vector> #include <string> #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack> #include <bitset>using namespace std; #define REPF (I, A, b ) for (int i = A; I <= B; + + i) #define REP (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, x) Memse T (A, X, sizeof a) typedef long long ll;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=1e5+10 0;int l[maxn],r[maxn];int c[maxn*2+10];vector<int>v[maxn];int ans[maxn];int n,rt,dfn;void dfs (int u,int pre) {L[ U]=++DFN; for (int i=0;i<v[u].size (); i++) {int to=v[u][i]; if (to==pre) continue; DFS (TO,U); } R[U]=++DFN;} int lowbit (int x) {return x& (-X);} void Update (int x,int val) {while (X<=DFN) {c[x]+=val; X+=lowbit (x); }}int query (intx) {int sum=0; while (x>0) {sum+=c[x]; X-=lowbit (x); } return sum;} int main () {int x, y; while (~SCANF ("%d%d", &n,&rt), N+rt) {CLEAR (l,0); REP (i,n+1) v[i].clear (); for (int i=0;i<n-1;i++) {scanf ("%d%d", &x,&y); V[x].push_back (y); V[y].push_back (x); } dfn=0; DFS (RT,-1); CLEAR (c,0); for (int i=1;i<=dfn;i++) update (i,1); for (int i=n;i>=1;i--) {ans[i]= (query (r[i]-1)-query (L[i]))/2; Update (R[I],-1); Update (L[I],-1); } repf (I,1,n) printf (i==n? ") %d\n ":"%d ", ans[i]); } return 0;}
HDU 3887 counting offspring (Dfs ordering subtree weights and)