HDU 4421 Bit Maigc



http://acm.hdu.edu.cn/showproblem.php?pid=4421

Bit Magic

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1175    Accepted Submission(s): 320


Problem Description
Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my achievement would get teacher's attention.
The key function is the code showed below.
HDU 4421 Bit Maigc_第1张图片
There is no doubt that my teacher raised lots of interests in my work and was surprised to my talented programming skills. After deeply thinking, he came up with another problem: if we have the matrix table b[N][N] at first, can you check whether corresponding number table a[N] exists?
 

Input
There are multiple test cases.
For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 2  31 - 1)
 

Output
For each test case, output "YES" if corresponding number table a[N] exists; otherwise output "NO".
 

Sample Input
   
   
   
   
2 0 4 4 0 3 0 1 24 1 0 86 24 86 0
 

Sample Output
   
   
   
   
YES NO
 

Source
2012 Asia ChangChun Regional Contest

#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");
    }
}


你可能感兴趣的:(Algorithm,poj,DFS,CString,杭州)