POJ 1466 大学谈恋爱 二分匹配变形 最大独立集
| Time Limit: 5000MS | Memory Limit: 10000K | |
| Total Submissions: 11694 | Accepted: 5230 |
Description
Input
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 (n <=500 ), for n subjects.
Output
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
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; vector<int> G[505];
int match[505],used[505];
int n;
void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int w=match[v];
if(w<0||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;//匹配的边两端点同时标记
return true;
}
}
return false;
} int bipartite_match()
{
memset(match,-1,sizeof(match));
int res=0;
for(int i=0;i<n;i++)
if(match[i]<0)//先前标记过就不用再标记了
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++) G[i].clear();
for(int u=0;u<n;u++)
{
int k,num,v;
scanf("%d: (%d)",&k,&num);
for(int i=0;i<num;i++)
{
scanf("%d",&v);
add_edge(u,v);
}
}
printf("%d\n",n-bipartite_match());
}
return 0;
}
分析:最大独立集问题,看得这篇介绍http://blog.sina.com.cn/s/blog_6635898a0100lyui.html
他们写的模板最后二分匹配出来后都要除以2,因为每条边都重复算了一次,但是因为我的模板
的问题,不需要除以2,因为同时把匹配的边两顶点都标记了
最大独立集=顶点数-匹配的顶点数/2(我的模板中即bipartite_match()返回的边数)
POJ 1466 大学谈恋爱 二分匹配变形 最大独立集的更多相关文章
- POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题
有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开 ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9754 Accepted: 3618 Desc ...
- TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5004 Accepted: 1444 ...
- poj 1466 Girls and Boys(二分匹配之最大独立集)
Description In the second year of the university somebody started a study on the romantic relations ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 1274 The Prefect Stall - 二分匹配
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22736 Accepted: 10144 Description Far ...
- Guardian of Decency POJ - 2771 【二分匹配,最大独立集】
Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...
随机推荐
- Windows文件共享配置与遇到的问题
一.Windows 7 访问共享文件权限不足 问题 最近在 Windows 10 上共享了一个文件夹,并创建了一个用户,用于在别人访问该共享文件夹时进行认证,但是在一个同事的电脑(Windows7,当 ...
- 【Python】【demo实验1】【Python运行时强制刷新缓冲区,输出信息】(Python3 应不存在类似情况)
[原文] 需求:打印一颗 ”*” 休息1s 代码如下: #!/usr/bin/python #coding=utf-8 ''' 暂停1s输出 ''' import time def printStar ...
- CentOS7下MySQL服务启动失败原因及解决方法
在重启阿里的CentOS7服务器后,重启MySQL 出现错误 Starting mysqld (via systemctl): Job for mysqld.service failed becaus ...
- 正确理解Widget::Widget(QWidget *parent) :QWidget(parent)这句话
原文:https://zhuanlan.zhihu.com/p/31310536 /********原文********/ 最近很多学习Qt的小伙伴在我的微信公众号私信我,该如何理解下面段代码的第二行 ...
- js创建jsonArray传输至后台及后台解析
//产品清单 //产品数量长度 var cnbvLength = FieldCount9 + 1; var arrayList=[]; for(var i = 0; i < cnbvLength ...
- 如何去除List集合中的重复元素? a,b,c,a,c,b,d,,,,,,
package com.fs.test; import java.util.ArrayList; import java.util.List; public class Listdemo { publ ...
- PHP高级进阶之路
一:常见模式与框架 学习PHP技术体系,设计模式,流行的框架 常见的设计模式,编码必备 Laravel.ThinkPHP开发必不可少的最新框架 YII.Symfony4.1核心源码剖析 二:微服务架构 ...
- groovy程序设计
/********* * groovy中Object类型存在隐式转换 可以不必使用as强转 */ Object munber = 9.343444 def number1 = 2 println mu ...
- date( ) 日期函数
date('Y-m-dT2:00') 实际时间为14:00 date('Y-m-d 2:00') 实际时间为2:00 扩展:每天的时间戳秒数为 86400
- Nginx源码安装配置
Nginx web服务器简介 Nginx ("engine x") 是一个高性能HTTP 和 反向代理 服务器.IMAP.POP3.SMTP 服务器. Nginx 是由 Igor ...