http://acm.hdu.edu.cn/showproblem.php?pid=4421
2 0 4 4 0 3 0 1 24 1 0 86 24 86 0
YES NO
#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<queue> #include<stack> using namespace std; int n,m,i,j,k; const int N=2022; vector<int> g[N]; stack<int> s; int f[N],dfn[N],low[N],b[N][N]; int curr,cnt; int instack[N]; void init() { cnt=curr=0; while(!s.empty())s.pop(); for(i=0;i<N;i++)g[i].clear(); memset(dfn,0,sizeof dfn); memset(low,0,sizeof low); memset(instack,0,sizeof instack); memset(f,0,sizeof f); } void dfs(int x) { dfn[x]=low[x]=++curr; s.push(x); instack[x]=1; for(int i=0;i<g[x].size();i++) { int v=g[x][i]; if(!dfn[v]) { dfs(v); low[x]=min(low[x],low[v]); }else{ if(instack[v]) low[x]=min(low[x],dfn[v]); } } if(low[x]==dfn[x]) { ++cnt;int v; do{ v=s.top(); s.pop(); instack[v]=0; f[v]=cnt; }while(v!=x); } } #define pb push_back void build() { for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(i!=j)if(b[i][j]&1){ if(i%2==1&&j%2==1){ g[j+n].pb(i);g[i+n].pb(j); }else if(i%2==0&&j%2==0) { g[i].pb(j);g[j].pb(i); }else{ g[i].pb(j+n);g[j+n].pb(i); g[i+n].pb(j);g[j].pb(i+n); } } else{ if(i%2==1&&j%2==1){ g[i+n].pb(j+n);g[j+n].pb(i+n); }else if(i%2==0&&j%2==0){ g[i].pb(j+n);g[j].pb(i+n); }else{ g[i].pb(j);g[j].pb(i); g[i+n].pb(j+n);g[j+n].pb(i+n); } } } bool check() { for(int i=0;i<2*n;i++) if(!dfn[i])dfs(i); for(int i=0;i<n;i++) if(f[i]==f[i+n]) return 0; return 1; } bool solve() { for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(b[i][j]!=b[j][i])return 0; } if(b[i][i])return 0; } for(int k=0;k<32;k++) { init(); build(); if(!check())return 0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) b[i][j]>>=1; } return 1; } int main() { while(cin>>n) { for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); if(solve())puts("YES"); else puts("NO"); } }