【bzoj4057】[Cerc2012]Kingdoms

  闷声大爆搜就好。
  状压判重。
  时间 O(n2n)

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define shl(x) (1 << (x))

inline int rd() {
    char c = getchar();
    while (!isdigit(c) && c != '-') c = getchar();
    int x = 0 , f = 1;
    if (c == '-') f = -1 ; else x = c - '0';
    while (isdigit(c = getchar())) x = x * 10 + c - '0';
    return x * f;
}

int n , a[23][23] , c[23] , vis[1 << 21] , ans , all;

void input() {
    n = rd();
    rep (i , 1 , n) c[i] = 0;
    rep (i , 1 , n) rep (j , 1 , n) a[i][j] = rd() , c[i] += a[i][j];
}

void dfs(int s , int t) {
    if (vis[s]) return;
    vis[s] = 1;
    if (t == 1)
        { ans |= s ; return ; }
    rep (i , 1 , n)
        if ((shl(i - 1) & s) && c[i] > 0) {
            rep (j , 1 , n) c[j] -= a[j][i];
            dfs(s ^ shl(i - 1) , t - 1);
            rep (j , 1 , n) c[j] += a[j][i];
        }
}

void solve() {
    ans = 0;
    all = shl(n) - 1;
    rep (i , 0 , all) vis[i] = 0;
    dfs(all , n);
    bool p = 0;
    rep (i , 1 , n) if (shl(i - 1) & ans) {
        if (p) putchar(' ');
        else p = 1;
        printf("%d" , i);
    }
    if (!p) putchar('0');
    //puts("");
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.txt" , "r" , stdin);
    #endif
    per (T , rd() , 1) {
        input();
        solve();
        puts("");
    }
    return 0;
}

你可能感兴趣的:(【bzoj4057】[Cerc2012]Kingdoms)