• 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
    ", 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
    ", K * (K-1) / 2 * ans % mod);
    }
    
    int main() {
        int T = getint();
        while(T--) sol();
        return 0;
    }
    View Code
  • 相关阅读:
    Javascript之in操作符的用法
    auguements实参对象的数组化
    jQuery多库共存问题解决方法
    JS框架设计之命名空间设计一种子模块
    WebStorm 快键键
    Sington单例模式(创建型模式)
    Asp.Net Cache缓存技术学习
    跨域
    webuploader跨域上传
    BuiltWith
  • 原文地址:https://www.cnblogs.com/galaxies/p/uoj308.html
Copyright © 2020-2023  润新知