hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图

来源:互联网 发布:ebscohost外文数据库 编辑:程序博客网 时间:2024/06/07 09:07

Question for the Leader

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 308    Accepted Submission(s): 59


Problem Description
JRY is the leader of a village. He has n lands, and there are n roads connecting them. There is at most one road connecting two lands and all lands are connected.

Now, JRY wants to divided the n lands into k disjoint sets of equal size, satisfying that one can move between any two lands belonging to the same set passing only through lands frome this set.

Furthermore, he wants to know how many k(1kn) he can choose.
 

Input
There are multiple testcases, the sum of n is less then 106.

For each test case, the first line contains one integer n(1n105).

The next line contains n integers, the i-th integer ai means that there is an edge between i and ai. It is guaranteed that the graph doesn't contain self loops and multiple edges.
 

Output
For each testcase print a single integer - the number of ways to choose the integer k.
 

Sample Input
62 3 4 5 6 162 4 2 3 4 3
 

Sample Output
43
Hint
Case 1 : k = 1,2,3,6Case 2: k = 1,3,6

题目:

把图划分为大小都为k的子图,子树内相互联通


题解直接黏贴解题报告了。

做法,求出基环。算出每个基环上的结点的子树的结点数,算出不在基环上的结点的子树的结点数


如果对于n % k == 0

算出不在基环上的结点就几个子树是k的倍数。得到F1

然后对于基环的点,任意点作为起点算出前缀和。对于每个前缀和%k。查看0-k-1中哪个数模数的到的次数最多。得到F2

判断F1+F2 == n/k








0 0