ZOJ3316:Game

来源:互联网 发布:河南vb振动电机 编辑:程序博客网 时间:2024/06/15 14:30
ZOJ Problem Set - 3316
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 <= x,y <= 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

Author: LIN, Yue
Source: The 10th Zhejiang University Programming Contest
一道看似博弈论的题,其实就是模板题而已。

分析:
平面上N个点,两个人轮流取点,而且规定当前取的点和上一个取的点的曼哈顿距离要小于L,所以,点可以看成两两成对消去。
这也就转成了一般图匹配,如果存在完美匹配,那么后手的人总可以取完,如果不存在,那么先手的人可以拿一个孤立点,这样第二个人要么没有匹配点,要么只能拆到一个匹配对,但这样又造成了孤立点。

网上有个人拿二分图匹配过了....看来数据很弱.....
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#define MAXN 450
using namespace std;

int x[MAXN], y[MAXN];

struct Graph
{
    bool mat[MAXN + 1][MAXN + 1];
    int n;

    bool inque[MAXN + 1];
    int que[MAXN], head, tail;

    int match[MAXN + 1], father[MAXN + 1], base[MAXN + 1];

    void init(int _n)
    {
        n = _n;
        for (int i = 1; i <= n; ++i)
        {
            match[i] = 0;
            for (int j = 1; j <= n; ++j)
                mat[i][j] = false;
        }
    }

    int pop()
    {
        return que[head++];
    }

    void push(int x)
    {
        que[tail++] = x;
        inque[x] = true;
    }

    void add_edge(int a, int b)
    {
        mat[a][b] = mat[b][a] = true;
    }

    int inpath[MAXN + 1];
    static int pcnt;

    int find_ancestor(int u, int v)
    {
        ++pcnt;
        while (u)
        {
            u = base[u];
            inpath[u] = pcnt;
            u = father[match[u]];
        }

        while (true)
        {
            v = base[v];
            if (inpath[v] == pcnt)
                return v;
            v = father[match[v]];
        }
    }

    int inblossom[MAXN + 1];
    static int bcnt;

    void reset(int u, int anc)
    {
        while (u != anc)
        {
            int v = match[u];
            inblossom[base[v]] = bcnt;
            inblossom[base[u]] = bcnt;
            v = father[v];
            if (base[v] != anc) father[v] = match[u];
            u = v;
        }
    }

    void contract(int u, int v)
    {
        int anc = find_ancestor(u, v);
        ++bcnt;
        reset(u, anc);
        reset(v, anc);
        if (base[u] != anc) father[u] = v;
        if (base[v] != anc) father[v] = u;
        for (int i = 1; i <= n; ++i)
            if (inblossom[base[i]] == bcnt)
            {
                base[i] = anc;
                if (!inque[i]) push(i);
            }
    }

    int find_augment(int start)
    {
        for (int i = 1; i <= n; ++i)
        {
            father[i] = 0;
            inque[i] = false;
            base[i] = i;
        }
        head = 0, tail = 0, push(start);
        while (head < tail)
        {
            int u = pop();
            for (int v = 1; v <= n; ++v)
                if (mat[u][v] && base[v] != base[u] && match[v] != u)
                {
                    if (v == start || (match[v] && father[match[v]]))
                        contract(u, v);
                    else
                    {
                        if (father[v] == 0)
                        {
                            if (match[v])
                            {
                                push(match[v]);
                                father[v] = u;
                            }
                            else
                            {
                                father[v] = u;
                                return v;
                            }
                        }
                    }
                }
        }
        return 0;
    }

    void augment(int finish)
    {
        int u = finish, v, w;
        while (u)
        {
            v = father[u];
            w = match[v];
            match[u] = v;
            match[v] = u;
            u = w;
        }
    }

    int graph_max_match()
    {
        int ans = 0;
        for (int i = 1; i <= n; ++i)
            if (match[i] == 0)
            {
                int finish = find_augment(i);
                if (finish)
                {
                    augment(finish);
                    ans += 2;
                }
            }
        return ans;
    }
} g;

int Graph :: bcnt = 0, Graph :: pcnt = 0;

int dis(int i, int j, int l)
{
    int d;
    d = abs(x[i] - x[j]) + abs(y[i] - y[j]);
    if (d <= l) return 1;
    else return 0;
}

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        int l;
        g.init(n);
        for (int i = 1; i <= n; ++i)
            scanf("%d%d", &x[i], &y[i]);
        scanf("%d", &l);
        for (int i = 1; i < n; ++i)
            for (int j = i + 1; j <= n; ++j)
            {
                if (dis(i, j, l)) g.add_edge(i, j);
            }
        int sum;
        sum = g.graph_max_match();
        if (sum == n) puts("YES");
        else puts("NO");
    }
    return 0;
}