UVA 10615 Rooks(?)
来源:互联网 发布:皇甫圣华淘宝 编辑:程序博客网 时间:2024/05/29 02:47
这题是我碰到的二分图最难,到现在都还没明白怎么回事,先存着吧。
#include<iostream>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn=100+5;int n,maxcolor;int color[maxn][maxn],match[maxn],inrow[maxn],incol[maxn];char map[maxn][maxn];bool used[maxn];vector<int> G[maxn];void init(){ memset(inrow,0,sizeof(inrow)); memset(incol,0,sizeof(incol)); maxcolor=0; scanf("%d",&n); for(int i=1;i<=n;i++){ G[i].clear(); scanf("%s",map[i]+1); for(int j=1;j<=n;j++){ if(map[i][j]=='*'){ inrow[i]++;incol[j]++; G[i].push_back(j); } } } for(int i=1;i<=n;i++) maxcolor=max(maxcolor,max(inrow[i],incol[i])); for(int i=1;i<=n;i++) if(inrow[i]<maxcolor) for(int j=1;j<=n&&inrow[i]<maxcolor;j++){ while(inrow[i]<maxcolor&&incol[j]<maxcolor){ inrow[i]++; incol[j]++; G[i].push_back(j); } } }bool find(int i){ for(int j=0;j<G[i].size();j++){ int v=G[i][j]; if(!used[v]) { used[v]=true; if(match[v]==-1||find(match[v])) { match[v]=i; return true; } } } return false;}void solve(){ memset(color,0,sizeof(color)); int cnt=0; while(cnt<maxcolor) { ++cnt; memset(match,-1,sizeof(match)); for(int i=1;i<=n;i++){ memset(used,0,sizeof(used)); find(i); } for(int i=1;i<=n;i++){ int row=match[i]; if(map[row][i]=='*') color[row][i]=cnt; for(int j=0;j<G[row].size();j++){ if(G[row][j]==i){ G[row].erase(G[row].begin()+j); break; } } } } printf("%d\n",maxcolor); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf("%s%d",j==1?"":" ",color[i][j]); } printf("\n"); }}int main(){ int t; scanf("%d",&t); while(t--) { init(); solve(); } return 0;}
0 0
- Uva-10615-Rooks
- UVA 10615 Rooks(?)
- uva 10615 - Rooks(完美匹配)
- [UVA 10615]Rooks[二分图匹配]
- Uva 10615 Rooks(二分图匹配)
- uva 11134 Fabled Rooks
- Uva-11134-Fabled Rooks
- uva 11134 Fabled Rooks
- uva 11134 - Fabled Rooks
- UVA 11134 - Fabled Rooks
- UVa:11134 Fabled Rooks
- uva 11134 Fabled Rooks
- UVa 11134 - Fabled Rooks
- UVa 11134 Fabled Rooks
- UVA 11134 - Fabled Rooks
- Uva - 11134 - Fabled Rooks
- UVA - 11134 Fabled Rooks
- UVA 11134 Fabled Rooks
- JVM定位占用cpu过高堆栈信息(Linux)
- proc 文件系统 介绍
- POJ 1847 Tram
- PHP filemanager resource
- VR_Unity的基本Shader的剔除、深度测试、Alpha测试以及基本雾效
- UVA 10615 Rooks(?)
- JavaScript Math对象
- js == 与 === 的区别
- [总结]音视频基础知识·二
- go-hbase的Scan模型源码分析
- 页面对json返回自动result:<pre style="word-wrap: break-word; white-space: pre-wrap;">{"flag":"true"}</pre>问题
- 有关 LayoutInflater.from(mActivity).inflate(R.layout.board, null);
- 【JQuery】可以编辑的表格实例
- System.Runtime.InteropServices.COMException (0x80010105): 服务器出现意外情况 问题的解决