Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9100    Accepted Submission(s): 4185

Problem Description
the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.

 
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
 
Sample Output
5
2
 
Source
 

这题大概意思就是说找出一个最大的集合使得该集合的任意两个人木有关系。

根据最大独立集 =顶点数 - 最大匹配数

由于题目没有给出哪些是男的哪些是女的,也就是说没有明显的二分图,所以将一个人拆成两个人进行最大匹配。由于一个拆成两个,所以最大匹配数应该是求出来的数除以2 。最后再用顶点数减就行了。

匈牙利算法求二分图最大匹配

/*
ID: LinKArftc
PROG: 1068.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int mp[maxn][maxn];
int n;
int linker[maxn];
bool vis[maxn]; bool dfs(int u) {
for (int v = ; v < n; v ++) {
if (mp[u][v] && !vis[v]) {
vis[v] = true;
if (linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungry() {
memset(linker, -, sizeof(linker));
int res = ;
for (int i = ; i < n; i ++) {
memset(vis, , sizeof(vis));
if (dfs(i)) res ++;
}
return res;
} int main() {
int u, v, cnt;
while (~scanf("%d", &n)) {
memset(mp, , sizeof(mp));
for (int i = ; i < n; i ++) {
scanf("%d: (%d)", &u, &cnt);
for (int j = ; j < cnt; j ++) {
scanf("%d", &v);
mp[u][v] = ;
}
}
printf("%d\n", n - hungry() / );
} return ;
}

HDU1068 (二分图最大匹配匈牙利算法)的更多相关文章

  1. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  2. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  3. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  4. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  5. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  6. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  7. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  8. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)

    System Engineer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 507   Accepted: 217 Des ...

  9. POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 814 ...

随机推荐

  1. Python-学习-小例子练习

    网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...

  2. CCF-NOIP-2018 提高组(复赛) 模拟试题(四)

    T1 贪吃蛇 [问题描述] 贪吃蛇是一个好玩的游戏.在本题中,你需要对这个游戏进行模拟. 这个游戏在一个 \(n\) 行 \(m\) 列的二维棋盘上进行. 我们用 \((x, y)\) 来表示第 \( ...

  3. IDEA + Maven + SSM 框架整合步骤

    因为前段时间自己想写个SSM的demo,然而不知怎么回事,配置完之后出现错误,怎么都调不好.最后从朋友那里拷了一个SSM的demo过来搭建成功,写这篇东西也是为了以后如果还有需要可以方便的查阅,并且也 ...

  4. docker容器中启动kvm虚拟机

    .安装docker yum install docker systemctl start docker.service systemctl enable docker.service .拉取cento ...

  5. iOS-初识swift

    在学习iOS开发之前,先掌握一点swift知识是必要的.note:不论是iOS开发还是编程语言的学习,都应该是迭代.由浅入深的过程,是理论实践相结合的过程. 中文文档 swift3(与swift4稍有 ...

  6. 爬虫:Scrapy17 - Common Practices

    在脚本中运行 Scrapy 除了常用的 scrapy crawl 来启动 Scrapy,也可以使用 API 在脚本中启动 Scrapy. 需要注意的是,Scrapy 是在 Twisted 异步网络库上 ...

  7. 面试题中经常遇到的SQL题:删除重复数据,保留其中一条

    如题,解决思路如下: 1.首先我们需要找出拥有重复数据的记录 ---以name字段分组 select Name,COUNT(Name) as [count] from Permission group ...

  8. Spring MVC前台POST/GET方式传参数的方法

    假设前台通过submit传值,代码如下: <form action="testPost.do" method="post"> 页码:<inpu ...

  9. 【题解】NOIP2016愤怒的小鸟

    一眼n<=18状压dp……方程什么的都很显然,枚举两只小鸟,再将这条抛物线上的小鸟抓出来就好啦.只是这样O(n^3)的dp必然是要TLE的,我一开始这样交上去显然跑得巨慢无比,后来转念一想:面对 ...

  10. [洛谷P4208][JSOI2008]最小生成树计数

    题目大意:有$n$个点和$m$条边(最多有$10$条边边权相同),求最小生成树个数 题解:对于所有最小生成树,每种边权的边数是一样的.于是就可以求出每种边权在最小生成树中的个数,枚举这种边的边集,求出 ...