Counting Offspring
Time limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3054 Accepted Submission (s): 1031
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 all 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 Input15 107 410 1414 214 139 119 0Sample 153 0 0 0 0 1 6 0 3 1 0 0 0 2 0Authorbnug Ongsource multi-university Training Contest 5-host by BNU test instructions: How many of the sub-trees in a tree with a root node have fewer than pity Dorado ordinal DFS sequences find the timestamp of each node with a tree-like array directly We can maintain this range.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cctype>5#include <cmath>6#include <cstring>7#include <map>8#include <Set>9#include <queue>Ten#include <vector> One#include <algorithm> A#include <string> - #definell Long Long - #defineEPS 1e-10 the #defineLL unsigned long Long - using namespacestd; - Const intinf=0x3f3f3f3f; - Const intn=200000+Ten; + Const intmod=1e9+7; - intHead[n]; + intTot,time; A intL[n],r[n]; at structnode{ - intTo,next; -}edge[n<<1]; - inta[n*5]; - voidinit () { -memset (head,-1,sizeof(head)); intot=0; -Time=0; to } + voidAddintUintv) { -edge[tot].to=v; theedge[tot].next=Head[u]; *head[u]=tot++; $ }Panax Notoginseng voidDFS (intXintFA) { -l[x]=++Time ; the for(inti=head[x];i!=-1; i=Edge[i].next) { + intv=edge[i].to; A if(V==FA)Continue; the DFS (v,x); + } -r[x]=Time ; $ $ } - intLowbit (intx) { - returnx& (-x); the } - voidUpdateintXinty) {Wuyi while(x<=Time ) { thea[x]=a[x]+y; -x=x+lowbit (x); Wu } - } About intGetsum (intx) { $ intans=0; - while(x>0){ - //cout<<2<<endl; -ans=ans+A[x]; Ax=x-lowbit (x); + } the returnans; - } $ intMain () { the intn,p; the while(SCANF ("%d%d", &n,&p)! =EOF) { the if(n==0&&p==0) Break; the init (); -Memset (A,0,sizeof(a)); in for(intI=1; i<n;i++){ the intu,v; thescanf"%d%d",&u,&v); About Add (u,v); the Add (v,u); the } theDFS (p,-1); + for(intI=1; i<=n;i++){ - intAns=getsum (R[i])-getsum (l[i]-1); the if(i!=n) cout<<ans<<" ";Bayi Else{ thecout<<ans<<Endl; the } -Update (L[i],1); - } the } the}
HDU 3887 counting offspring (Dfs order)