Time Limit:10 Sec Memory limit:128 MB submit:72 solved:46 description Alice has a big chandelier at home. The so-called chandelier is made up of a lot of light bulbs. Only one bulb is hanging from the ceiling, and the rest of the bulbs are hanging on the other bulbs. In other words, the entire chandelier is actually similar to a tree. The lamp numbered 1 is hung on the ceiling, and the rest of the bulbs are hung on the lamp with a number smaller than their own. Now that Alice wants to have a party, she wants to change the chandelier and change the bulbs into different colors. She wants the same color bulbs to be connected, and the number of bulbs in each color is the same. Alice would like you to tell her, what are the total options? Alice is a greedy child, and if she finds out that the program is not enough, or too much, she will be very unhappy, so she will try to adjust. For a lamp numbered X (x≠1), if it was hung on a light bulb numbered f[x], Alice would hook the X bulb to the (F[x] + 19940105) mod (x-1) + 1 bulbs. because nine in ancient Chinese expressed the enormous number, so, Alice decided to adjust only 9 times. For both the original state and each adjusted state, Alice wants you to tell her in turn what scenarios are in each state. input the first line an integer n, indicating the number of bulbs. on the next line, there is n-1 integer UI, and the first number indicates that the i+1 bulb is hanging underneath the UI. A lamp with a guaranteed number of 1 is hung on the ceiling. The numbers are separated by a comma ', ' and the last digit is not followed by a comma. output for scenarios in 10 states, they need to be sequentially output in order. For each state, you need to output a separate line that represents the status number, as shown in the sample. After a few lines, 1 integers per line representing the number of bulbs for each color in the scheme. output in ascending order. sample Input6
1,2,3,4,5
Sample OutputCase #1:
1
2
3
6
Case #2:
1
2
6
Case #3:
1
3
6
Case #4:
1
3
6
Case #5:
1
3
6
Case #6:
1
2
6
Case #7:
1
2
3
6
Case #8:
1
6
Case #9:
1
2
6
Case #10:
1
3
6
HINT
For 100% of data, n<=1.2*106.
Source
Brain-hole problems.
See the range of data, inner nervous. Look at the time limit 10s, hey, write it casually!
The rebuilding of a tree is a need for violence to maintain, not to be avoided.
After the tree is rebuilt, the total number of nodes for each node's subtree is counted. If the bulb of each color has k, then n%k==0 must be satisfied, and the number of nodes equal to the number of K is n/k.
Statistical tree size, the building of Dfs is a waste of time, because the father number of each node must be smaller than itself, you can upload from number n to number 1 update size.
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=1200010;9 intRead () {Ten intx=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the intN; - intA[mxn],cnt=0; - intFA[MXN],NUM[MXN]; - intC[MXN]; + //void DFS (int u,int fa) { - //} + voidReset () { A for(intI=2; i<=n;i++){ atFa[i]= (fa[i]+19940105)% (I-1)+1; - } - return; - } - voidinit1 () { - intm=sqrt (n); in for(intI=1; i<=m;i++){ - if(n%i==0){ toa[++cnt]=i; + if(i*i!=n) a[++cnt]=n/i; - } the } * return; $ }Panax Notoginseng voidinit () { -Memset (c,0,sizeofc); the for(intI=1; i<=n;i++) num[i]=1; + return; A } the voidsolve () { + init (); - inti,j; $ for(i=n;i>=1; i--) $num[fa[i]]+=Num[i]; - for(i=1; i<=n;i++) c[num[i]]++; - for(i=1; i<=cnt;i++){ the intX=a[i];//x Bulbs per color - intres=0;Wuyi for(j=x;j<=n;j+=x) { theres+=C[j]; - } Wu if(n/x==res) printf ("%d\n", x); - } About return; $ } - intMain () { -n=read (); - init1 (); A inti,j; + for(i=2; i<=n;i++) fa[i]=read (); theprintf"Case #1: \ n"); - solve (); $ for(i=1; i<=9; i++){ theprintf"Case #%d:\n", i+1); the reset (); the solve (); the } - return 0; in}
Bzoj3004 pendant lamp