吊灯(divide)题目描述
Alice家里有一盏很大的吊灯。所谓吊灯,就是由很多个灯泡组成。只有一个灯泡是挂在天花板上的,剩下的灯泡都是挂在其他的灯泡上的。也就是说,整个吊灯实际上类似于一棵树。其中编号为1的灯泡是挂在天花板上的,剩下的灯泡都是挂在编号小于自己的灯泡上的。
现在,Alice想要办一场派对,她想改造一下这盏吊灯,将灯泡换成不同的颜色。她希望相同颜色的灯泡都是相连的,并且每一种颜色的灯泡个数都是相同的。
Alice希望你能告诉她,总共有哪些方案呢?
Alice是一个贪心的孩子,如果她发现方案不够多,或者太多了,就会很不高兴,于是她会尝试调整。对于编号为x(x≠1)的灯泡,如果原来是挂在编号为f[x]的灯泡上,那么Alice会把第x个灯泡挂到第 ( f[x] + 19940105 ) mod (x-1) + 1 个灯泡上。
由于九在古汉语中表示极大的数,于是,Alice决定只调整9次。对于原始状态和每一次调整过的状态,Alice希望你依次告诉她每种状态下有哪些方案。
输入说明
第一行一个整数n,表示灯泡的数量。
接下来一行,有n-1个整数Ui,第i个数字表示第i+1个灯泡挂在了Ui个的下面。保证编号为1的灯泡是挂在天花板上的。数字之间用逗号‘,’隔开且最后一个数字后面没有逗号。
输出说明
对于10种状态下的方案,需要按照顺序依次输出。
对于每一种状态,需要先输出单独的一行,表示状态编号,如样例所示。
之后若干行,每行1个整数,表示划分方案中每种颜色的灯泡个数。
按升序输出。
样例输入
6
1,2,3,4,5
样例输出
Case #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
数据范围
对于20%的数据,n<=310^3。
对于40%的数据,n<=510^4。
对于50%的数据,n<=110^5。
对于60%的数据,n<=310^5。
对于70%的数据,n<=710^5。
对于100%的数据,n<=1.210^6。
子树能自己解决自己解决,否则加入根,复杂度较高,打表发现随机数据答案很可能是1,n
正解:http://www.cnblogs.com/gengchen/p/6579442.html
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
void read(int &x) {
char c; bool flag = 0;
while((c=getchar()) < '0' || c>'9') flag |= (c=='-');
x=c-'0';while((c=getchar())>='0'&&c<='9') x=x*10+c-'0';
if(flag) x=-x;
}
const int N = 1300000;
int st[N*2],top;
struct E{
int to,next;
E(int to=0,int next=0):to(to),next(next){}
}g[N*2];
int fr[N],tot;
void Add(int from,int to) {
g[++tot] = E(to,fr[from]);
fr[from] = tot;
}
int n,f[N],dp[N];
vector<int> vt;
void init() {
int t = sqrt(n);
for (int i = 2; i <= t; i++)
if(n%i == 0) {
vt.push_back(i);
if(i != n/i) vt.push_back(n/i);
}
sort(vt.begin(),vt.end());
}
void adjust() {
static int ff[N];
for (int i = 2; i <= n; i++) ff[i] = (f[i]+19940105)%(i-1)+1;
memset(fr,0,sizeof(fr[0])*(n+3)); tot = 0;
for (int i = 2; i <= n; i++) {
f[i] = ff[i];
Add(f[i],i);
}
}
int nd;
bool bfs() {
for (int t = n; t; t--) {
dp[t] = 1;
for (int i = fr[t]; i; i = g[i].next) {
if(dp[g[i].to] < nd) {
dp[t] += dp[g[i].to];
if(dp[t] > nd) return 0;
}
}
}
return dp[1]==nd;
}
void slove() {
printf("1\n");
for (int i = 0; i < vt.size(); i++) {
nd = vt[i];
if(bfs()) printf("%d\n",vt[i]);
}
if(n!=1) printf("%d\n",n);
}
void hehe() {
printf("Case #1:\n");
slove();
for (int i = 2; i <= 10; i++) {
printf("Case #%d:\n",i);
printf("1\n%d\n",n);
}
}
int main() {
//freopen("sdoi12_divide.in","r",stdin);freopen("sdoi12_divide.out","w",stdout);
read(n); init();
for (int i = 2; i <= n; i++) {
read(f[i]);
Add(f[i],i);
}
if(n >= 30000) {hehe();return 0;}
for (int i = 1; i <= 10; i++) {
printf("Case #%d:\n",i);
slove();
if(i != 10) adjust();
}
return 0;
}