zoukankan      html  css  js  c++  java
  • [ZOJ3316]:Game

    题面

    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;
    }
    
  • 相关阅读:
    CentOS 6.3用ssh无密码登陆其它主机
    堆排序算法以及JAVA实现
    TaskTracker发送Heartbeat以及接受HeartbeatResponse
    Linux常用命令总结
    用eclipse将mapreduce程序打成jar包并在命令行执行
    jquery自定义验证方法
    solaris中几个网络经典命令小结
    DWR的简单总结[转]
    java 编码问题 及转换
    DWR学习详解【转】
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8724340.html
Copyright © 2011-2022 走看看