Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Fight club is an organization where you release your pressure and emotion by fighting the other club members. The fight begins like this: n people standing in a circle. Then two adjacent guys are chosen to fight. The winner will stay and the loser will be sent to the hospital (there is no tie in the fighting). The cruel fight continues until there is only one left.
It is somewhat strange to see that when two guys fight each other, the result is always definite, and you know the result of every pair of fighters. Though they enjoy fighting, they still want to be the winner. You are to tell each of them, if the fight is arranged properly, can he be the winner.
Input
First line contains integer T (T<=5), the number of test cases.
For each test case, the first line contains n (1<=n<=40), the number of people. Following is the description of a n*n matrix A. Following n lines each contains n ‘0’ or ‘1’, separated by a single blank. The ith number in jth line indicates the fighting result of ith and jth fighter. If it is ‘1’, then the ith fighter will always beat the jth fighter, otherwise he will always lose. You can safely assume that aij and aji are different and aii=0 for every distinct i and j.
The people are numbered counterclockwise.
Output
For each test case, output n lines. If the ith fighter can be the survivor, output 1, otherwise output 0.
Output a blank line after each test case.
Sample Input
2
2
0 1
0 0
3
0 1 1
0 0 1
0 0 0
Sample Output
1
0
1
0
0
zsdx的oj 没法交
题意就是n个人成一个环 然后每个人如果要决斗会和他右边的人决斗 n会和1决斗 问最后谁有可能获胜
我们把环状拆成一个 2*n的链然后定义一个dp[l][r] 代表 l 能不能遇见r
那么我们输出答案只要检验 dp[i][i+n]
然后我们得枚举中间k
如果 dp[l][k] && dp[k][r]并且 &&(mp[l][k]||mp[r][k]) 就代表能遇到
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 85;
int dp[MAX_N][MAX_N],mp[MAX_N][MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
for(int i = 1;i<=n;++i)
for(int j = 1;j<=n;++j)
scanf("%d",&mp[i][j]);
memset(dp,0,sizeof(dp));
for(int i = 0;i<(n*2);i++) dp[i][i+1] = 1;
for(int i = 1;i<=n;++i) mp[0][i] = mp[n][i],mp[i][0] = mp[i][n];
for(int l = 2;l<=n;++l)
{
for(int i = 0;i+l<2*n;++i)
{
int j = i + l;
for(int k = i+1;k<j;++k)
{
if(dp[i][k]&&dp[k][j]&&(mp[i%n][k%n]||mp[j%n][k%n]))
{
dp[i][j] = 1;
break;
}
}
}
}
for(int i = 1;i<=n;++i)
if(dp[i][i+n]) puts("1");
else puts("0");
printf("\n");
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}