(intermediate) 二分图(边着色) UVA 10615 Rooks

Problem G
Rooks
Input: Standard Input

Output:Standard Output

Time Limit:8 Seconds 

 

Given a chessboardNxN, on which the rooks are placed. You have tocolor those rooks in a minimal number of colors in that way – no horizontal and vertical line contains two rooks of the same color.

 

Input

First line of the input file contains an integerS(0<S<10) that indicates how many sets of inputs are there. The description of each set is given below:

 

The first line of each input set contains numberN (0≤N≤100).

The next N lines contain a chessboard (arrayNxN), where an empty cell is marked as ‘.’, and a cell that contains a rook is marked as ‘*’(there are not blanks between the symbols in a line).

 

Output

The description of output for each test case is given below:

 

The first line of the output for each test case contains numberM- the minimal number of colors. The next N lines contain a chessboard, where an empty cell is marked as ‘0’, and a cell that contains a rook is marked as ‘K’, whereK is a color of the rook. There can be more than correct solution  any valid solution will be accepted.

 

Sample Input                             Output for Sample Input

2
2
*.
**
4
*.*.
*.*.
***.
..**
2
2 0
1 2

4

1 0 2 0

3 0 1 0

2 1 3 0

0 0 4 1

 

 

 

Problem source: Russiansummer training camp 2000.

Problem author: Maxim Babenko

Problem translation: Dmytro Chernysh

 

 题意:给出一个方格图,里面有一些是“*”,你要为他们染色,使得每行每列不会有同样的颜色,并且颜色要最少。


思路:这题给跪了。。。建图的话是按行号和列号分别为两个集合,如果该行该列有“*”,就连一条线,我就只能想到这里了。。。。然后颜色的最少数目为这个图里面最大的度数。。。不会证明,不要问我。然后做法就是为这个二分图补充一些边使得每个点的度数等于最大度数。然后开始匹配,匹配一次,就把边给删掉。如果匹配出的行列有”*“,就标颜色到输出答案中,否则不用管。


代码:

#include<iostream>
#include<cstdio>
#include<ctime>
#include<string.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 105;
int n , rook;
int board[maxn][maxn];
char buffer[maxn][maxn];
int link[maxn];
bool T[maxn];
vector<int> G[maxn];
int deg[maxn];

void input()
{
	memset(buffer,0,sizeof(buffer));
	scanf("%d",&n);
	rook = 0;
	memset(deg,0,sizeof(deg));
	for (int i = 0 ; i < n ; ++i)
	{
		G[i].clear();
		scanf("%s",buffer[i]);
		int cnt = 0;
		for (int j = 0 ; j < n ; ++j) {
			if (buffer[i][j]=='*') {
				++cnt;
				G[i].push_back(j);
				++deg[j];
			}
		}
		rook = max(rook,cnt);
	}
	for (int i = 0 ; i < n ; ++i)
	{
		int cnt = 0;
		for (int j = n-1 ; j >= 0 ; --j)
			if (buffer[j][i]=='*') ++cnt;
		rook = max(rook,cnt);
	}

	for (int i = 0 ; i < n ; ++i) if (G[i].size()<rook) {
		for (int j = 0 ; j < n && G[i].size()<rook ; ++j) if (deg[j]<rook) {
			while (deg[j]<rook && G[i].size()<rook) {
				G[i].push_back(j);
				++deg[j];
			}
		}
	}
}

void output()
{
	for (int i = 0 ; i < n ; ++i)
	{
		printf("%d",board[i][0]);
		for (int j = 1 ; j < n ; ++j) printf(" %d",board[i][j]);
		printf("\n");
	}
}

bool dfs(int x)
{
	for (int i = 0 ; i < G[x].size() ; ++i) if (!T[G[x][i]])
	{
		int y = G[x][i];
		T[y] = true;
		if (link[y]==-1 || dfs(link[y]))
		{
			link[y] = x;
			return true;
		}
	}
	return false;
}

void maxmatch()
{
	memset(link,-1,sizeof(link));
	for (int i = 0 ; i < n ; ++i) {
		memset(T,0,sizeof(T));
		dfs(i);
	}
}

void solve()
{
	printf("%d\n",rook);
	memset(board,0,sizeof(board));
	int color = 0;
	while (color < rook) {
		++color;
		maxmatch();
		for (int i = 0 ; i < n ; ++i) {
			int r = link[i] , c = i;
			if (buffer[r][c]=='*') board[r][c] = color;
			for (int j = 0 ; j < G[r].size() ; ++j) if (G[r][j]==c) {
				G[r].erase(G[r].begin()+j);
				break;
			}
		}
	}
	output();
}

int main()
{
	int T; cin>>T;
	while (T--) {
		input();
		solve();
	}
}



我的天,二分图的题目好难想,不管中等题还是简单题直接就跪了,有些题目说了是二分图都想不到怎么用二分图做。。。太弱了。建模能力还有待提高,这种能力是非常重要的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值