HDU 5917 Instability——稳定子图

版权声明:欢迎大家转载,转载请注明出处 https://blog.csdn.net/hao_zong_yin/article/details/82707703

大于等于6个点的图都是稳定子图,其余暴力特判就可以

5层循环也就2e6的复杂度

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 60;
const int mod = 1e9 + 7;
int g[maxn][maxn];
int T, n, m;
ll mpow(ll x, ll y) {
    ll ans = 1;
    while (y) {
        if (y & 1) ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}
bool ok(int i, int j, int k) {
    int sum = g[i][j] + g[i][k] + g[j][k];
    if (sum == 0 || sum == 3) return true;
    return false;
}
bool ok(int i, int j, int k, int t) {
    if (ok(i, j, k) || ok(i, j, t) || ok(i, k, t) || ok(j, k, t)) return true;
    return false;
}
bool ok(int i, int j, int k, int t, int x) {
    if (ok(i, j, k, t) || ok(i, j, k, x) || ok(i, j, t, x) || ok(i, k, t, x) || ok(j, k, t, x)) return true;
    return false;
}
int main() {
    scanf("%d", &T);
    for (int ks = 1; ks <= T; ks++) {
        scanf("%d %d", &n, &m);
        memset(g, 0, sizeof(g));
        for (int i = 1; i <= m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u][v] = g[v][u] = 1;
        }
        ll ans = mpow(2, n);
        ans = (ans - 1 + mod) % mod;
        ans = (ans - n + mod) % mod;
        ans = (ans - n*(n-1)/2 + mod) % mod;
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                for (int k = j + 1; k <= n; k++) {
                    if (!ok(i, j, k)) ans = (ans - 1 + mod) % mod;
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                for (int k = j + 1; k <= n; k++) {
                    for (int t = k + 1; t <= n; t++) {
                        if (!ok(i, j, k, t)) ans = (ans - 1 + mod) % mod;
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                for (int k = j + 1; k <= n; k++) {
                    for (int t = k + 1; t <= n; t++) {
                        for (int x = t + 1; x <= n; x++) {
                            if (!ok(i, j, k, t, x)) ans = (ans - 1 + mod) % mod;
                        }
                    }
                }
            }
        }
        printf("Case #%d: %lld\n", ks, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hao_zong_yin/article/details/82707703
hdu