先做二分匹配,然后遍历每个点,如果删掉这个点求出的最大匹配和原先的最大匹配相等,那么这个点就是必败点,否则就是必胜点。。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1005
#define maxm 100005
#define eps 1e-10
#define mod 10000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
//head
int h[maxn], next[maxm], v[maxm];
int linked[maxn], vis[maxn], res[maxn];
int n1, n2, m, cnt, p;
void addedges(int a, int b)
{
next[cnt] = h[a], h[a] = cnt, v[cnt] = b, cnt++;
}
void init(void)
{
cnt = p = 0;
memset(h, -1, sizeof h);
memset(linked, -1, sizeof linked);
memset(res, -1, sizeof res);
}
bool find(int u)
{
for(int e = h[u]; ~e; e = next[e]) if(!vis[v[e]]) {
vis[v[e]] = 1;
if(linked[v[e]] == -1 || find(linked[v[e]])) {
if(v[e] == p) continue;
linked[v[e]] = u;
res[u] = v[e];
return true;
}
}
return false;
}
void read(void)
{
int a, b;
while(m--) {
scanf("%d%d", &a, &b);
addedges(a, b+n1);
addedges(b+n1, a);
}
}
void work(void)
{
for(int i = n1+1; i <= n1+n2; i++) {
memset(vis, 0, sizeof vis);
find(i);
}
for(int i = 1; i <= n1; i++) {
if(linked[i] == -1) printf("P");
else {
int t = linked[i];
linked[i] = res[t] = -1;
p = i;
memset(vis, 0, sizeof vis);
if(find(t)) printf("P");
else printf("N"), res[t] = i, linked[i] = t;
}
}
printf("\n");
memset(linked, -1, sizeof linked);
memset(res, -1, sizeof res);
for(int i = 1; i <= n1; i++) {
memset(vis, 0, sizeof vis);
find(i);
}
for(int i = n1+1; i <= n1+n2; i++) {
if(linked[i] == -1) printf("P");
else {
int t = linked[i];
linked[i] = res[t] = -1;
p = i;
memset(vis, 0, sizeof vis);
if(find(t)) printf("P");
else printf("N"), res[t] = i, linked[i] = t;
}
}
printf("\n");
}
int main(void)
{
while(scanf("%d%d%d", &n1, &n2, &m)!=EOF) {
init();
read();
work();
}
return 0;
}