码迷,mamicode.com
首页 > 其他好文 > 详细

[ZOJ3316]:Game

时间:2018-04-05 22:06:59      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:https   并且   一个人   最大   void   tchar   ble   code   inline   

题面

vjudge

Sol

有一个棋盘,棋盘上有一些棋子,两个人轮流拿棋,第一个人可以随意拿,以后每一个人拿走的棋子与上一个人拿走的棋子的曼哈顿距离不得超过L,无法拿棋的人输,问后手能否胜利


首先距离小于等于\(L\)的连双向边
肯定是在每个连通块玩,并且这些连通块每个都有完美匹配后手才能赢

所以跑一般图最大匹配就好了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
const int _(400);
typedef long long ll;

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, first[_], cnt, x[_], y[_], L, size[_], num;
int col[_], vis[_], match[_], pre[_], fa[_], ri[_], tim[_], idx;
struct Edge{
    int to, next;
} edge[_ * _];
queue <int> Q;

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL int Dis(RG int i, RG int j){
    return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}

IL void Dfs(RG int u){
    ++size[num], col[u] = num;
    for(RG int e = first[u]; e != -1; e = edge[e].next)
        if(!col[edge[e].to]) Dfs(edge[e].to);
}

IL int Find(RG int x){
    return fa[x] == x ? x : fa[x] = Find(fa[x]);
}

IL int LCA(RG int x, RG int y){
    ++idx, x = Find(x), y = Find(y);
    while(tim[x] != idx){
        tim[x] = idx;
        x = Find(pre[match[x]]);
        if(y) swap(x, y);
    }
    return x;
}

IL void Blossom(RG int x, RG int y, RG int p){
    while(Find(x) != p){
        pre[x] = y, y = match[x];
        if(vis[y] == 2) vis[y] = 1, Q.push(y);
        if(Find(x) == x) fa[x] = p;
        if(Find(y) == y) fa[y] = p;
        x = pre[y];
    }
}

IL int Aug(RG int x){
    while(!Q.empty()) Q.pop(); idx = 0;
    for(RG int i = 1; i <= n; ++i) vis[i] = tim[i] = pre[i] = 0, fa[i] = i;
    Q.push(x), vis[x] = 1;
    while(!Q.empty()){
        RG int u = Q.front(); Q.pop();
        for(RG int e = first[u]; e != -1; e = edge[e].next){
            RG int v = edge[e].to;
            if(Find(u) == Find(v) || vis[v] == 2) continue;
            if(!vis[v]){
                vis[v] = 2, pre[v] = u;
                if(!match[v]){
                    for(RG int p = v, lst; p; p = lst)
                        lst = match[pre[p]], match[pre[p]] = p, match[p] = pre[p];
                    return 1;
                }
                vis[match[v]] = 1, Q.push(match[v]);
            }
            else{
                RG int p = LCA(u, v);
                Blossom(u, v, p), Blossom(v, u, p);
            }
        }
    }
    return 0;
}

IL int Check(){
    for(RG int i = 1; i <= num; ++i) if(size[i] & 1) return 0;
    for(RG int i = 1; i <= n; ++i) if(!match[i]) ri[col[i]] += Aug(i);
    for(RG int i = 1; i <= num; ++i) if((ri[i] << 1) != size[i]) return 0;
    return 1;
}

int main(RG int argc, RG char* argv[]){
    while(scanf("%d", &n) != EOF){
        cnt = num = 0;
        for(RG int i = 1; i <= n; ++i) first[i] = -1, col[i] = match[i] = ri[i] = size[i] = 0;
        for(RG int i = 1; i <= n; ++i) x[i] = Input(), y[i] = Input();
        L = Input();
        for(RG int i = 1; i < n; ++i)
            for(RG int j = i + 1; j <= n; ++j)
                if(Dis(i, j) <= L) Add(i, j), Add(j, i);
        for(RG int i = 1; i <= n; ++i) if(!col[i]) ++num, Dfs(i);
        Check() ? puts("YES") : puts("NO");
    }
    return 0;
}

[ZOJ3316]:Game

标签:https   并且   一个人   最大   void   tchar   ble   code   inline   

原文地址:https://www.cnblogs.com/cjoieryl/p/8724340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!