
2 0 4 4 0 3 0 1 24 1 0 86 24 86 0
YES NO
题目大意:
给你b[i][j],问你a[i]是否冲突?
解题思路:
对于a[i]的每一位根据 d[i][j] 进行2at
解题代码:
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=510;
struct edge{
int u,v,next;
edge(int u0=0,int v0=0){
u=u0,v=v0;
}
}e[maxn*maxn*4];
int head[maxn*2],cnt,N;
int dfn[maxn*2],low[maxn*2],color[maxn*2],index,nc;
bool mark[maxn*2];
vector <int> vec;
void adde(int u,int v){
e[cnt]=edge(u,v),e[cnt].next=head[u],head[u]=cnt++;
}
void init(){
vec.clear();
index=nc=cnt=0;
for(int i=0;i<=2*N;i++){
dfn[i]=0;
color[i]=head[i]=-1;
mark[i]=false;
}
}
void tarjan(int s){
dfn[s]=low[s]=++index;
mark[s]=true;
vec.push_back(s);
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(!dfn[d]){
tarjan(d);
low[s]=min(low[s],low[d]);
}else if(mark[d]){
low[s]=min(low[s],dfn[d]);
}
}
if(low[s]==dfn[s]){
int d;
nc++;
do{
d=vec.back();
vec.pop_back();
color[d]=nc;
mark[d]=false;
}while(s!=d);
}
}
bool sat2(){
for(int i=0;i<2*N;i++){
if(!dfn[i]) tarjan(i);
}
for(int i=0;i<N;i++){
if(color[i]==color[i+N]) return false;
}
return true;
}
int n,b[maxn][maxn];
void build(int x){
N=n;//N =sum point
init();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if( i%2==1 && j%2==1 ){//|
if( b[i][j]&(1<<x) ){
adde(i,j+n);
adde(j,i+n);
}else{
adde(i,j);
adde(j,i);
}
}else if( i%2==0 && j%2==0 ){//&
if( b[i][j]&(1<<x) ){
adde(i+n,j+n);
adde(j+n,i+n);
}else{
adde(i+n,j);
adde(j+n,i);
}
}else{//^
if( b[i][j]&(1<<x) ){
adde(i,j+n);
adde(i+n,j);
adde(j,i+n);
adde(j+n,i);
}else{
adde(i,j);
adde(j,i);
adde(i+n,j+n);
adde(j+n,i+n);
}
}
}
}
}
void input(){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
bool solve(){
for(int i=0;i<n;i++){
if(b[i][i]!=0) return false;
for(int j=i+1;j<n;j++){
if(b[i][j]!=b[j][i]) return false;
}
}
for(int i=0;i<=30;i++){
build(i);
if(!sat2()) return false;
}
return true;
}
int main(){
while(scanf("%d",&n)!=EOF){
input();
if(solve()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
原文:http://blog.csdn.net/a1061747415/article/details/39855061