zoj 3316 Game(带花树+完美匹配)
2015-04-01 21:59
316 查看
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
Sample Output
取棋子游戏 判断后取的是否能赢
当每个连通块中 都是完美匹配时 后取的才能赢
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
2 0 2 2 0 2 2 0 2 2 0 4
Sample Output
NO YES
取棋子游戏 判断后取的是否能赢
当每个连通块中 都是完美匹配时 后取的才能赢
#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; }
相关文章推荐
- ZOJ 3316 Game 一般图最大匹配带花树
- ZOJ 3316 Game (带花树算法)
- zoj 3316 Game (一般图匹配带花树)
- ZOJ 3316 Game 一般图最大匹配带花树
- 带花树(一般图最大匹配)详解 ZOJ 3316
- ZOJ 3316 Game 一般图匹配
- zoj 3316 Game 一般图最大匹配+博弈 有N个棋子在棋盘上,2个人轮流拿走一个棋子,第一步可以拿任意一个,而之后每一步必须拿上一步拿走的棋子曼哈顿长度L以内的棋子,问,后手是否能赢
- zoj 2050 -Flip Game题解
- zoj 3329 One Person Game 概率dp
- ZOJ 3593 One Person Game(扩展欧几里得求最小步数)
- ZOJ 1926 Guessing Game
- ZOJ 3329 One Person Game
- ZOJ 3015 Collision Ball Game
- zoj 3329 One Person Game (概率DP )
- ZOJ 3329 One Person Game 概率dp 处理环
- ZOJ 3329 One Person Game [概率DP]
- ZOJ 3329 One Person Game(概率dp)
- ZOJ 3329 One Person Game(玄学)
- zoj 1926 || poj 2328 Guessing Game(水~)
- A Game Between Alice and Bob(zoj 3529)