Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥

求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子可以共存

阴阳离子都至少需要存在一种 那么可以枚举哪2种离子共存 假设枚举a b 然后找到所有的和a可以共存的阴离子(设为x集合)以及和b共存的阳离子(设为y集合)

现在只需求x集合中和y集合中最多有多少个离子可以共存 这个求最大独立集就行了 枚举所有的a b 取最大值

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 55;
int vis[maxn];
int y[maxn];
vector <int> G[maxn];
int n, m;

char a[maxn][maxn];
bool dfs(int u)
{
	for(int i = 0; i < G[u].size(); i++)
	{
		int v = G[u][i];
		if(vis[v])
			continue;
		vis[v] = true;
		if(y[v] == -1 || dfs(y[v]))
		{
			y[v] = u;
			return true;
		}
	}
	return false;
}
int match()
{
	int ans = 0;
	memset(y, -1, sizeof(y));
	for(int i = 0; i < n; i++)
	{
		memset(vis, 0, sizeof(vis));
		if(dfs(i))
			ans++;
	}
	return ans;
}
int main()
{
	int T;
	int cas = 1;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d %d", &n, &m);
		for(int i = 0; i < n; i++)
			scanf("%s", a[i]);
		int ans = 0;
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				if(a[i][j] == '1')
				{
					int sum1 = 0, sum2 = 0;
					for(int h = 0; h < m; h++)
						if(a[i][h] == '1')
							sum1++;
					for(int h = 0; h < n; h++)
						if(a[h][j] == '1')
							sum2++;
					if(ans >= sum1+sum2)
						continue;
					for(int h = 0; h < n; h++)
					{
						G[h].clear();
						if(a[h][j] == '1')
						{
							for(int k = 0; k < m; k++)
								if(a[i][k] == '1' && a[h][k] == '0')
									G[h].push_back(k);
						}
					}
					int res = match();
					if(sum1+sum2-res > ans)
						ans = sum1+sum2-res;
				}
		printf("Case %d: %d\n", cas++, ans);

	}
	return 0;
}
时间: 2024-10-24 14:34:38

Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集的相关文章

Light oj 1149--Factors and Multiples【二分匹配 &amp;&amp; 经典建图】

1149 - Factors and Multiples PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You will be given two sets of integers. Let's call them set A and set B. Set A contains n elements and set B contains m elements. You have to remo

hdu 4169 二分匹配最大独立集 ***

题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #incl

UVALive 5962 Strongly Connected Chemicals --最大独立集

题意:给n个阳离子和m个阴离子,并给出相互的吸引关系,求一个最大的点集,使其中的每个阴阳离子相互吸引. 解法:枚举每条边,使该条边存在,然后建立反图,求一个最大匹配,此时的点数减去最大匹配与ans求一个最大值即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <stack

LOJ 1201 - A Perfect Murder(二分匹配 最大独立集)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1201 "Yes, I am the murderer. No doubt" I had to confess it in front of all. But wait, why I am confessing? Nobody wants to go to jail, neither do I. As you have suspected there is something

Timus OJ 1997 Those are not the droids you&#39;re looking for (二分匹配)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时. 下面n行是人进进出出的时刻,0为进,1为出.让你求是否有合法解. 将合法的进入和出去连边,然后二分匹配就可以了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <vector&

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

poj 2594 Treasure Exploration (二分匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2644 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored