4401: Count of blocksDescription
Little y recently heard from the classmate of a very high-quality data structure of B-block tree. It is said that this data structure can maintain all kinds of information on the tree in the time of sqrt (N), and it is very efficient. Of course, the boring little Y has no interest in this kind of thing, but is very curious about the operation of the tree. He thought, if you can divide a tree into several pieces, so that the number of points in each block is the same how beautiful ah! Little y wants to know that there are several ways to make a tree beautiful. Small y will draw a tree every time, but because of the speed of the hand too fast, sometimes small y-painted trees will be unusually large, so that small y feel very distressed. But little y really wanted to know the answer, so he found you, a genius programmer, to help him finish the job.
Input
The first line is a positive integer n, which represents the total number of nodes in the tree, followed by the N-1 line, where two digits x, y for each row are connected to the node numbered Y. The node number range is 1-n and the number 22 is different.
Output
One line, an integer ans, that represents the number of scenarios that are being asked.
Sample Input6
1 2
2 3
2 4
4 5
5 6Sample Output3HINT
100% of the data meets n<=1000000.
The idea is a great problem.
The first consideration is that the size of the block must be an approximate amount of n, which is obvious.
You can then find a point that, if it can be a root of a block, the size of the subtree produced by this point must be a multiple of the block size
Then constructs the tree, the Sieve method, the size is the number I multiples the point calculates, this is the block quantity, in carries on the judgment.
#include <stdio.h>#include<iostream>using namespacestd;Const intn=1000005;intN,i,j,x,y,ans,f[n],p[n];inttot,head[n],next[n<<1],to[n<<1];voidAddintXinty) {Tot++; To[tot]=y; Next[tot]=Head[x]; HEAD[X]=tot;}voidDfsintXintpre) {F[x]=1; for(inti=head[x];i!=-1; i=Next[i])if(to[i]!=pre) {DFS (TO[I],X); F[X]+=F[to[i]]; } P[f[x]]++;}intMain () {scanf ("%d",&N); for(i=1; i<=n;i++) Head[i]=-1; for(i=1; i<n;i++) {scanf ("%d%d",&x,&y); Add (x, y); Add (y,x); } DFS (1,0); for(i=1; i<=n;i++) { for(j=i<<1; j<=n;j+=i) p[i]+=P[j]; if(i*p[i]==n) ans++; } cout<<ans; return 0;}
Bzoj 4401: Count of Blocks