染色+特殊的背包DP
//============================================================================
// Name : The Joys of Farming.cpp
// Date : 2013/4/27 下午12:20:56
// 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 2010
#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);
}
int b,c,a;
int mz[M][M];
int mzsize[M];
int used[M];
int q[M*M],front,rear;
int white,black,no;
int coinw[M];
int coinb[M];
int dp[M];
int next[M];
void bfs(int x){
front=rear=0;
q[front++]=x;
used[x]=0;
while(front>rear){
int u=q[rear++];
int nowcolor=used[u];
if(nowcolor)black++;
else white++;
for(int i=0;i<mzsize[u];i++){
int v=mz[u][i];
if(used[v]==nowcolor){
no=1;
return;
}else if(used[v]==-1){
used[v]=1-nowcolor;
q[front++]=v;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
int test;
scanf("%d",&test);
while(test--){
no=0;
Set(mz,0);
Set(mzsize,0);
Set(used,-1);
Set(coinw,0);
Set(coinb,0);
Set(dp,0);
scanf("%d%d%d",&b,&c,&a);
for(int i=0;i<a;i++){
int x,y;
scanf("%d%d",&x,&y);
mz[x][mzsize[x]++]=y;
mz[y][mzsize[y]++]=x;
}
int k=0;
for(int i=1;i<=b+c;i++){
if(used[i]==-1){
white=black=0;
bfs(i);
coinw[k]=white;
coinb[k++]=black;
// debug("%d %d\n",coinw[k-1],coinb[k-1]);
}
}
if(no){
printf("no\n");
continue;
}
Set(next,0);
dp[0]=1;
for(int i=0;i<k;i++){
Set(next,0);
for(int j=b;j>=min(coinb[i],coinw[i]);j--){
if(j>=coinw[i]){
next[j]|=dp[j-coinw[i]];
// printf("dp[%d]=dp[%d]\n",j,j-coinw[i]);
}
if(j>=coinb[i]){
next[j]|=dp[j-coinb[i]];
// printf("dp[%d]=dp[%d]\n",j,j-coinb[i]);
}
}
for(int i=0;i<=b;i++)dp[i]=next[i];
}
// for(int i=0;i<=b;i++){
// printf("%d ",dp[i]);
// }Pln();
if(dp[b])printf("yes\n");
else printf("no\n");
}
}
留言
張貼留言