zoj 3316 Game(带花树+完美匹配)

来源:互联网 发布:php分页详解 编辑:程序博客网 时间:2024/06/05 11:41

Game

Time Limit: 1 Second      Memory Limit: 32768 KB

Fire and Lam are addicted to the game of Go recently. Go is one of the oldest board games. It is rich in strategy despite its simple rules. The game is played by two players who alternately place black and white stones on the vacant intersections of a grid of 19*19 lines. Once placed on the board, stones cannot be moved elsewhere, unless they are surrounded and captured by the opponent's stones. The object of the game is to control (surround) a larger portion of the board than the opponent.

Fire thinks it is too easy for him to beat Lam. So he thinks out a new game to play on the board. There are some stones on the board, and we don't need to care about the stones' color in this new game. Fire and Lam take turns to remove one of the stones still on the board. But the Manhattan distance between the removed stone and the opponent's last removed stone must not be greater than L. And the one who can't remove any stone loses the game.

The Manhattan distance between (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

To show the performance of grace, Fire lets Lam play first. In the beginning of the game, Lam can choose to remove any stone on the board.

Fire and Lam are clever, so they both use the best strategy to play this game. Now, Fire wants to know whether he can make sure to win the game.

Input

There are multiple cases (no more than 30).

In each case, the first line is a positive integer n (n <= 361) which indicates the number of stones left on the board. Following are n lines, each contains a pair of integers x and y (0 <= xy <= 18), which indicate a stone's location. All pairs are distinct. The last line is an integer L (1 <= L <= 36).

There is a blank line between cases.

Ouput

If Fire can win the game, output "YES"; otherwise, just output "NO".

Sample Input

20 22 0220 22 04

Sample Output

NOYES

取棋子游戏 判断后取的是否能赢

当每个连通块中 都是完美匹配时  后取的才能赢

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 380#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char ch;    int a = 0;    while((ch = getchar()) == ' ' | ch == '\n');    a += ch - '0';    while((ch = getchar()) != ' ' && ch != '\n')    {        a *= 10;        a += ch - '0';    }    return a;}void Print(int a)    //输出外挂{     if(a>9)         Print(a/10);     putchar(a%10+'0');}deque<int> Q;//g[i][j]存放关系图:i,j是否有边 match[i]存放i所匹配的点bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN],inpath[MAXN];int match[MAXN],pre[MAXN],base[MAXN];int n,m,mmg;vector<int> res;struct node{    int u,v;}point[MAXN];//找公共祖先int findancestor(int u,int v){    MEM(inpath,0);    while(1)    {        u=base[u];        inpath[u]=1;        if(match[u]==-1)  break;        u=pre[match[u]];    }    while(1)    {        v=base[v];        if(inpath[v])  return v;        v=pre[match[v]];    }}//压缩花void reset(int u,int anc){    while(u!=anc)    {        int v=match[u];        inblossom[base[u]]=1;        inblossom[base[v]]=1;        v=pre[v];        if(base[v]!=anc)  pre[v]=match[u];        u=v;    }}void contract(int u,int v,int n){    int anc=findancestor(u,v);    MEM(inblossom,0);    reset(u,anc); reset(v,anc);    if(base[u]!=anc)  pre[u]=v;    if(base[v]!=anc)  pre[v]=u;    for(int i=1;i<=n;i++)    {        if(inblossom[base[i]])        {            base[i]=anc;            if(!inque[i])            {                Q.push_back(i);                inque[i]=1;            }        }    }}bool bfs(int S,int n){    for(int i=0;i<=n;i++)    {        pre[i]=-1; inque[i]=0; base[i]=i;    }    Q.clear(); Q.push_back(S); inque[S]=1;    while(!Q.empty())    {        int u=Q.front(); Q.pop_front();        for(int v=1;v<=n;v++)        {            if(g[u][v]&&base[v]!=base[u]&&match[u]!=v)            {                if(v==S||(match[v]!=-1&&pre[match[v]]!=-1)) contract(u,v,n);                else if(pre[v]==-1)                {                    pre[v]=u;                    if(match[v]!=-1)                    {                        Q.push_back(match[v]);                        inque[match[v]]=1;                    }                    else                    {                        u=v;                        while(u!=-1)                        {                            v=pre[u];                            int w=match[v];                            match[u]=v;                            match[v]=u;                            u=w;                        }                        return 1;                    }                }            }        }    }    return 0;}int belong[MAXN],sz,num[MAXN],link[MAXN];void dfs(int u){    belong[u]=sz; num[sz]++;    for(int i=1;i<=n;i++)    {        if(g[u][i]&&!belong[i])            dfs(i);    }}bool solve(){    MEM(match,-1);    for(int i=1;i<=n;i++)        if(match[i]==-1)  bfs(i,n);    for(int i=1;i<=n;i++)        if(match[i]!=-1)            link[belong[i]]++;    for(int i=1;i<=sz;i++)    {        if(num[i]!=link[i])            return 0;    }    return 1;}int x[MAXN],y[MAXN];int main(){//    fread;    while(scanf("%d",&n)!=EOF)    {        MEM(g,0);        for(int i=1;i<=n;i++)        {            scanf("%d%d",&x[i],&y[i]);        }        int l;        scanf("%d",&l);        MEM(g,0);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if(i==j)  continue;                int len=abs(x[i]-x[j])+abs(y[i]-y[j]);                if(len<=l) g[i][j]=g[j][i]=1;            }        }        MEM(belong,0); MEM(num,0); MEM(link,0);        sz=0;        for(int i=1;i<=n;i++)        {            if(!belong[i])            {                sz++;                dfs(i);            }        }        int res=solve();        if(res)            puts("YES");        else puts("NO");    }    return 0;}





0 0