ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
2015-05-17 01:07
435 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836
因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设(i,j)的位置是U,
以U为边,连接点i和点j+n,也即连接行点和列点,最大匹配为n则必定有解,否则必定无解
因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设(i,j)的位置是U,
以U为边,连接点i和点j+n,也即连接行点和列点,最大匹配为n则必定有解,否则必定无解
#include <cstdio> #include <iostream> #include <cstring> #include <cctype> #define clr(x,y) memset(x, y, sizeof x) #include <cmath> using namespace std; const int maxn=6e2+6; const int maxm=maxn*maxn*2; int first[maxn]; struct edge{ int nxt,t,f; }e[maxm]; void addedge(int f,int t,int ind){ e[ind].nxt=first[f]; e[ind].t=t; e[ind].f=f; first[f]=ind; } int n; char maz[maxn][maxn]; bool vis[maxn]; int match[maxn]; bool dfs(int f){ vis[f]=true; for(int p=first[f];p!=-1;p=e[p].nxt){ int t=e[p].t; int mch=match[t]; if(mch==-1||(!vis[mch]&&dfs(mch))){ match[t]=f; match[f]=t; return true; } } // printf("dfs %d no\n",f); return false; } int findmatch(){ int ans=0; for(int i=0;i<n;i++){ if(match[i]==-1){ clr(vis,0); if(dfs(i))ans++; } } return ans; } void init(){ clr(first,-1); clr(match,-1); } int main(){ //freopen("input.txt","r",stdin); while(scanf("%d",&n)==1){ init(); int en=0; for(int i=0;i<n;i++)scanf("%s",maz[i]); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(maz[i][j]=='U'){ addedge(i,j+n,en++); addedge(j+n,i,en++); } } } int ans=findmatch(); if(ans==n){ puts("YES"); } else { puts("NO"); } } return 0; }
相关文章推荐
- ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3
- ZOJ 1654--Place the Robots【二分匹配 && 经典建图】
- ZOJ 1654--Place the Robots【二分匹配 && 经典建图】
- zoj 3460 Missile 【二分 + 二分图匹配】 【经典建模】 【二分 + 最大流】
- ZOJ 3646 (匹配匈牙利..算法)
- ZOJ 1516--Uncle Tom's Inherited Land【二分图 && 求最大匹配数 && 经典建图】
- poj 2446 Chessboard(经典二分匹配)
- 【二分匹配】ZOJ 1654 Place the Robots
- ZOJ 2521 —— LED Display(二分匹配,最小路径覆盖)
- ZOJ 1016 Parencodings (括号匹配) 思路
- ZOJ 1654 (经典二分构图题)
- POJ 1469 ZOJ1140 二分匹配裸题
- ZOJ 3156 Taxi (二分匹配+二分查找)
- 【二分匹配】ZOJ 1654 Place the Robots
- 【二分匹配】zoj1654 Place the Robots
- zoj 1002 Fire Net (二分匹配)
- ZOJ-1654 Place the Robots 拆行拆列构图+二分匹配 Or 最大独立点集+TLE
- Poj 2446 Chessboard 【二分匹配+经典建图模型】
- hdu 1150 Machine Schedule (经典二分匹配)
- POJ3343&HDU2413Against Mammoths (二分匹配)经典