uoj308 【UNR #2】UOJ拯救计划

传送门:http://uoj.ac/problem/308

【题解】

考虑枚举用了$i$所学校,那么贡献为${k \choose i} * cnt * i!$

意思是从$k$所选$i$所出来染色,$cnt$为固定颜色顺序的染色方案,$i!$为可以交换学校位置。

考虑当$i \geq 3$的时候,贡献含有模数因子6,所以模6为0,相当于没有贡献。

当$i = 1$,显然只有$m = 0$有贡献。

对于$m = 0$我们特判,答案显然是$K^n$。

剩下$i = 2$的情况,也就是我们要判断答案是不是一个二分图,如果不是二分图,显然答案为0。

考虑令$cnt' = cnt * i!$,那么二分图的一个连通块只要确定了1个点颜色,剩下确定下来了,所以$cnt' = 2^p$,其中$p$为连通块个数。

那么贡献为$2^{p-1} * k * (k-1)$,直接算就好了。

复杂度$O(T(n + m))$

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5 + 10, M = 4e5 + 10;
const int mod = 6;

inline int getint() {
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x<<3) + (x<<1) + ch - '0', ch = getchar();
    return x;
}

int n, m, K, head[N], nxt[M], to[M], tot = 0;
inline void add(int u, int v) {
    ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v;
}
inline void adde(int u, int v) {
    add(u, v), add(v, u);
}

inline int pwr(int a, int b) {
    int ret = 1;
    while(b) {
        if(b&1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

bool ok; int c[N];
inline void color(int x) {
    for (int i=head[x]; i; i=nxt[i]) {
        if(c[to[i]]) {
            if(c[x] == c[to[i]]) {
                ok = 0;
            }
            continue;
        }
        c[to[i]] = -c[x];
        color(to[i]);
    }
}


inline void sol() {
    n = getint(); m = getint(); K = getint(); tot = 0;
    for (int i=1; i<=n; ++i) head[i] = c[i] = 0;
    for (int i=1, u, v; i<=m; ++i) {
        u = getint(), v = getint();
        adde(u, v);
    }
    if(m == 0) {
        printf("%d\n", pwr(K, n));
        return ;
    }
    // bitgraph
    int ans = 1;
    ok = 1;
    for (int i=1; i<=n; ++i) 
        if(!c[i]) {
            c[i] = 1;
            color(i);
            ans <<= 1; ans %= mod;
            if(!ok) {
                puts("0");
                return ;
            }
        }
    printf("%d\n", K * (K-1) / 2 * ans % mod);
}

int main() {
    int T = getint();
    while(T--) sol();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/galaxies/p/uoj308.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值