BZOJ 4057: [Cerc2012]Kingdoms
状压DP
#include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],stack[25]; int main(){ int T; scanf("%d",&T); while (T--){ int n; scanf("%d",&n); for (int i=0; i<n; i++) for (int j=0; j<n; j++) scanf("%d",&A[i][j]); for (int i=0; i<(1<<n); i++) F[i]=0; F[(1<<n)-1]=1; for (int now=(1<<n)-1; now; now--) if (F[now]){ int top=0; for (int i=0; i<n; i++) if (now&(1<<i)) stack[++top]=i; for (int i=1; i<=top; i++){ int Sum=0; for (int j=1; j<=top; j++) Sum+=A[stack[j]][stack[i]]; if (Sum<0) F[now-(1<<stack[i])]=1; } } int f=0; for (int i=0; i<n; i++) if (F[1<<i]) { f++; printf("%d ",i+1); } if (!f) printf("%d",f); printf("\n"); } return 0; }