@description@

给定一个每个点出度都为 1 的有向连通图以及 m 种颜色。求本质不同的染色方案数。

原题传送门。

@solution@

其实就是基环树。

考虑一棵有根树的本质不同染色方案:先递归处理出子树答案,通过树哈希把它长得一样的子树放在一起。

对于长得相同的子树,假如有 x 个,这子树的染色方案为 f[x],则它们的贡献为 C(f[x] + x - 1, x)。一个不难理解的组合问题。

注意那个组合数直接暴力算就好了,因为 ∑x = n。

再考虑环,可以使用 burnside 引理解决,是个经典的模型。

不过注意环旋转过后对应位置的树哈希值必须相等,否则这个置换是不合法的,不能算入最后的置换群大小中。

为了保证这一点,可以先求出所有可行循环节大小。转成 border 用 kmp 求即可。

总时间复杂度 O(nlogn)。

@accepted code@

#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std; typedef unsigned long long ull; const int MAXN = 100000;
const int MOD = int(1E9) + 7;
const int HASHMOD = 19260817; int pow_mod(int b, int p) {
int ret = 1;
for(int i=p;i;i>>=1,b=1LL*b*b%MOD)
if( i & 1 ) ret = 1LL*ret*b%MOD;
return ret;
} struct dhash{
ull h1; int h2; dhash() : h1(0), h2(0) {}
dhash(ull _h1, int _h2) : h1(_h1), h2(_h2) {}
friend dhash operator + (const dhash &a, const dhash &b) {
return dhash(a.h1 + b.h1, (a.h2 + b.h2 >= HASHMOD ? a.h2 + b.h2 - HASHMOD : a.h2 + b.h2));
}
friend dhash operator - (const dhash &a, const dhash &b) {
return dhash(a.h1 - b.h1, (a.h2 - b.h2 < 0 ? a.h2 - b.h2 + HASHMOD : a.h2 - b.h2));
}
friend dhash operator * (const dhash &a, const dhash &b) {
return dhash(a.h1 * b.h1, 1LL * a.h2 * b.h2 % HASHMOD);
}
friend bool operator < (const dhash &a, const dhash &b) {
return (a.h1 == b.h1 ? a.h2 < b.h2 : a.h1 < b.h1);
}
friend bool operator == (const dhash &a, const dhash &b) {
return ((!(a < b)) && (!(b < a)));
}
friend bool operator != (const dhash &a, const dhash &b) {
return a < b || b < a;
}
}; int f[MAXN + 5], n, m;
bool tag[MAXN + 5], vis[MAXN + 5];
int get(int x) {
vis[x] = true;
if( vis[f[x]] ) {
tag[x] = true;
return f[x];
}
int ret = get(f[x]);
if( ret != -1 )
tag[x] = true;
return (ret == x ? -1 : ret);
} dhash h[MAXN + 5], sed[MAXN + 5], pw[2*MAXN + 5];
int dp[MAXN + 5], iv[MAXN + 5], siz[MAXN + 5];
vector<int>v[MAXN + 5];
int comb(int n, int m) {
n = ((n + m) % MOD + MOD - 1) % MOD;
int ret = 1;
for(int i=1;i<=m;i++)
ret = 1LL*ret*(n-i+1)%MOD*iv[i]%MOD;
return ret;
}
pair<dhash, int>tmp[MAXN + 5];
void dfs(int x) {
siz[x] = 1, h[x] = dhash(1, 1);
for(int i=0;i<(int)v[x].size();i++) {
int to = v[x][i];
if( tag[to] ) continue;
dfs(to); siz[x] += siz[to];
h[x] = h[x] + sed[siz[to]] * h[to];
}
int tot = 0;
for(int i=0;i<(int)v[x].size();i++) {
int to = v[x][i];
if( tag[to] ) continue;
tmp[++tot] = make_pair(h[to], dp[to]);
}
sort(tmp + 1, tmp + tot + 1), dp[x] = m;
for(int i=1;i<=tot;i++) {
int j = i;
while( j < tot && tmp[i].first == tmp[j + 1].first )
j++;
dp[x] = 1LL*dp[x]*comb(tmp[i].second, j - i + 1)%MOD;
i = j;
}
}
int b[MAXN + 5], c[MAXN + 5], cnt;
int con[MAXN + 5], tot;
int gcd(int x, int y) {
return (y == 0 ? x : gcd(y, x % y));
}
int fail[MAXN + 5], prod[MAXN + 5];
int solve1() {
prod[0] = c[0];
for(int i=1;i<cnt;i++)
prod[i] = 1LL*prod[i - 1]*c[i]%MOD;
fail[0] = -1, fail[1] = 0;
for(int i=2;i<=cnt;i++) {
int j = fail[i - 1];
while( j != -1 && b[j] != b[i-1] )
j = fail[j];
fail[i] = j + 1;
}
for(int i=1;i<=cnt;i++) con[i] = 0;
for(int p=cnt;fail[p]!=-1;p=fail[p])
con[cnt - fail[p]] = prod[cnt - fail[p] - 1];
/*
for(int i=1;i<=cnt;i++) {
if( cnt % i == 0 ) {
bool flag = true;
for(int j=i;j<cnt;j++)
if( b[j-i] != b[j] ) {
flag = false;
break;
}
if( flag ) {
con[i] = 1;
for(int j=0;j<i;j++)
con[i] = 1LL*con[i]*c[j]%MOD;
}
else con[i] = 0;
}
}
*/
int ans = 0; tot = 0;
for(int i=0;i<cnt;i++) {
int x = gcd(i, cnt);
if( con[x] ) ans = (ans + con[x]) % MOD, tot++;
}
return ans;
}
int solve() {
int ret = solve1();
return 1LL*ret*pow_mod(tot, MOD-2)%MOD;
}
int a[MAXN + 5], dcnt; dhash d[MAXN + 5];
int read() {
int x = 0, ch = getchar();
while( ch > '9' || ch < '0' ) ch = getchar();
while( '0' <= ch && ch <= '9' ) x = 10*x + ch - '0', ch = getchar();
return x;
}
void work() {
n = read(), m = read();
for(int i=1;i<=n;i++) v[i].clear(), tag[i] = vis[i] = false;
for(int i=1;i<=n;i++) v[f[i] = read()].push_back(i);
get(1);
for(int i=1;i<=n;i++)
if( tag[i] ) dfs(i);
cnt = 0;
for(int i=1;i<=n;i++)
if( tag[i] ) {
int p = i;
do {
a[cnt++] = p;
p = f[p];
}while( p != i );
break;
}
dcnt = 0;
for(int i=0;i<cnt;i++) d[++dcnt] = h[a[i]];
sort(d + 1, d + dcnt + 1), dcnt = unique(d + 1, d + dcnt + 1) - d - 1;
for(int i=0;i<cnt;i++) b[i] = lower_bound(d + 1, d + dcnt + 1, h[a[i]]) - d, c[i] = dp[a[i]];
printf("%d\n", solve());
}
ull get_rand() {
ull p = rand() << 16 | rand();
ull q = rand() << 16 | rand();
return p << 32 | q;
}
void init() {
for(int i=0;i<=MAXN;i++) {
ull p = get_rand();
sed[i] = dhash(p, int(p % HASHMOD));
}
iv[1] = 1;
for(int i=2;i<=MAXN;i++)
iv[i] = MOD - 1LL*(MOD/i)*iv[MOD%i]%MOD;
pw[0] = dhash(1, 1), pw[1] = dhash(1313131, 1313131);
for(int i=2;i<=2*MAXN;i++)
pw[i] = pw[i-1] * pw[1];
}
int main() {
srand(20041112), init();
int T; scanf("%d", &T);
while( T-- ) work();
}

@details@

貌似有点小卡常,不知道是不是我常数太大。

@hdu - 5822@ color的更多相关文章

  1. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. hdu 1199 Color the Ball

    http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...

  5. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. HDU 1556.Color the ball-差分数组-备忘

    备忘. 差分数组: 区间更新查询有很多方法,线段树.树状数组等都可以.如果为离线查询,就可以考虑使用差分数组. 假设对于区间[l,r]的每个数都加1,我们用一个数组a来记录,a[l]+=1;a[r+1 ...

  7. 线段树(求单结点) hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. hdu 1556 Color the ball(区间更新,单点求值)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

随机推荐

  1. Golang源码学习:使用gdb调试探究Golang函数调用栈结构

    本文所使用的golang为1.14,gdb为8.1. 一直以来对于函数调用都仅限于函数调用栈这个概念上,但对于其中的详细结构却了解不多.所以用gdb调试一个简单的例子,一探究竟. 函数调用栈的结构(以 ...

  2. web自动化之文件上传操作

    #!/usr/bin/python3 # -*- coding: utf-8 -*- #Author: xiaojian #Time: 2018/11/16 20:49 import win32gui ...

  3. mybatis是怎样炼成的

    前言 一些个人感受:不管分析什么源码,如果我们能摸索出作者的心路历程,跟着他的脚步一步一步往前走,这样才能接近事实的真相,也能更平滑更有趣的学习到知识.跟福尔摩斯探案一样,作者都经历了些什么,为什么他 ...

  4. Java高级特性之集合

    Java集合框架 一.Java集合框架概述 1.数组与集合的区别: 1)数组长度不可变化而且无法保存具有映射关系的数据:集合类用于保存数量不确定的数据,以及保存具有映射关系的数据. 2)数组元素既可以 ...

  5. 引入mybatis-plus报 Invalid bound statement错误怎么办,动动手指改一个地方就行

    错误 Mybatis-Plus (简称MP) 是mybatis的一个增强工具,在mybatis的基础上只做增强不做改变,简化了开发效率.其实就是帮我们封装了一些简单的curd方法,可以直接调用,不必再 ...

  6. javascript中日期的最简单格式化

    // 假设要转换的日期数据来源是date(一个timestamp) let date = Date.now() // 1574141546000 let strDate = (new Date(dat ...

  7. [优文翻译]003.你应避免的移动开发APP的5个细节(5 Things to Avoid while Developing Your Next Mobile App)

    导读:本文是从<5 Things to Avoid while Developing Your Next Mobile App>这篇文章翻译而来 智能手机的普及带动了大批移动应用的诞生,这 ...

  8. SpringBoot工程创建的三种方式

    一. 通过IDEA的spring Initializer创建 1. 打开创建项目面板 File->New->Project->Spring Initializr 2. 填写Maven ...

  9. 使用Burpsuite对手机抓包的配置

    之前使用dSploit的时候就一直在想怎么对手机进行抓包分析,前两天使用了Burpsuite神器,发现通过简单的配置就可以抓手机app的数据包了,进而分析手机app的流量. 配置环境: 1.win7下 ...

  10. 一篇文章讲透Dijkstra最短路径算法

    Dijkstra是典型最短路径算法,计算一个起始节点到路径中其他所有节点的最短路径的算法和思想.在一些专业课程中如数据结构,图论,运筹学等都有介绍.其思想是一种基础的求最短路径的算法,通过基础思想的变 ...