極度建議初學2-SAT別寫這題,會被這題搞亂
//============================================================================
// Name : Manhattan.cpp
// Date : 2013/5/3 上午11:44:22
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
#ifdef ONLINE_JUDGE
#define ll "%lld"
#else
#define ll "%I64d"
#endif
typedef unsigned int uint;
typedef long long int Int;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 200
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
vector<int> f,mz[M];
vector<int> rmz[M];
bool vis[M];
int s,a,m;
int scc[M];
int n;
int num;
void addcl(int x,int y){
mz[x^1].PB(y);
rmz[y].PB(x^1);
mz[y^1].PB(x);
rmz[x].PB(y^1);
}
void dfs1(int x){
vis[x]=1;
FOR(it,mz[x]){
int v=(*it);
if(!vis[v]){
dfs1(v);
}
}
f.PB(x);
}
void dfs2(int x){
scc[x]=n;
vis[x]=1;
FOR(it,rmz[x]){
int v=(*it);
if(!vis[v]){
dfs2(v);
}
}
}
void solve(){
f.clear();
n=0;
num=(s+a)*2;
Set(vis,0);
for(int i=0;i<num;i++){
if(!vis[i])dfs1(i);
}
Set(vis,0);
for(int i=f.size()-1;i>=0;i--){
if(!vis[f[i]]){
dfs2(f[i]);
n++;
}
}
for(int i=0;i<num;i++){
if(scc[i]==scc[i^1]){
printf("No\n");
return ;
}
}printf("Yes\n");
}
int main() {
ios_base::sync_with_stdio(0);
int test;
scanf("%d",&test);
while(test--){
scanf("%d%d%d",&s,&a,&m);
for(int i=0;i<m;i++){
int s1,a1,s2,a2;
scanf("%d%d%d%d",&s1,&a1,&s2,&a2);
s1--,a1--,s2--,a2--;
s1*=2,a1=2*s+2*a1,s2*=2,a2=2*s+2*a2;
if(a1>a2){
s1^=1;
s2^=1;
}
if(s1>s2){
a1^=1;
a2^=1;
}
if(a1==a2&&s1==s2)continue;
if(a1==a2){
mz[a1^1].PB(a1);
rmz[a1].PB(a1^1);
}else if(s1==s2){
mz[s1^1].PB(s1);
rmz[s1].PB(s1^1);
}else{
addcl(s1,s2);
addcl(s1,a1);
addcl(a2,s2);
addcl(a2,a1);
}
}
solve();
for(int i=0;i<num;i++){
mz[i].clear();
rmz[i].clear();
}
}
}
留言
張貼留言