网络流(最大独立点集):POJ 1466 Girls and Boys
Girls and Boys
This problem will be judged on PKU. Original ID: 1466
64-bit integer IO format: %lld Java class name: Main
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
题目大意:有n个人每个人又和别的人有关系,求的是没有关系的最大人数。
输入:
第一行 n个人,接下来n行 0--n-1:(与此人有关系的人的个数 ) 有关系的人。
求的是二分图的最大独立集,此题不用划分集合,所以最后的最大匹配数要除以2。
二分图最大独立集 = N - 最大匹配数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],cap[maxm],to[maxm],dis[maxn],gap[maxn],path[maxn]; void addedge(int a,int b,int c)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
cap[cnt]=c;
fir[a]=cnt;
} bool BFS(int S,int T)
{
memset(dis,,sizeof(dis));
dis[T]=;
queue<int>q;q.push(T);
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
{
if(dis[to[i]])continue;
dis[to[i]]=dis[node]+;
q.push(to[i]);
}
}
return dis[S];
}
int fron[maxn];
int ISAP(int S,int T)
{
if(!BFS(S,T))
return ;
for(int i=;i<=T;i++)++gap[dis[i]];
int p=S,ret=;
memcpy(fron,fir,sizeof(fir));
while(dis[S]<=T)
{
if(p==T){
int f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
int &ii=fron[p];
for(;ii;ii=nxt[ii]){
if(!cap[ii]||dis[to[ii]]+!=dis[p])
continue;
else
break;
}
if(ii){
p=to[ii];
path[p]=ii;
}
else{
if(--gap[dis[p]]==)break;
int minn=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])
minn=min(minn,dis[to[i]]);
gap[dis[p]=minn+]++;
fron[p]=fir[p];
if(p!=S)
p=to[path[p]^];
}
}
return ret;
} void Init()
{
memset(fir,,sizeof(fir));
cnt=;
}
int main()
{
int n,k,to;
while(~scanf("%d",&n))
{
Init();
for(int i=;i<=n;i++)addedge(,i,),addedge(i,,),addedge(i+n,*n+,),addedge(*n+,i+n,);
for(int i=;i<n;i++){
scanf("%d: (%d)",&i,&k);
while(k--){
scanf("%d",&to);
addedge(i+,to+n+,);
addedge(to+n+,i+,);
}
}
printf("%d\n",ISAP(,*n+));
}
return ;
}
当然,匈牙利算法是更合适的算法~~~
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std;
const int INF=;
const int maxn=,maxm=; int cnt,fir[maxn],nxt[maxm],to[maxm],match[maxn],vis[maxn]; void addedge(int a,int b)
{
nxt[++cnt]=fir[a];
to[cnt]=b;
fir[a]=cnt;
} int Hungary_algorithm(int node)
{
vis[node]=true;
for(int i=fir[node];i;i=nxt[i]){
if(!match[to[i]]){
match[to[i]]=node;
return ;
}
if(!vis[match[to[i]]]&&Hungary_algorithm(match[to[i]])){
match[to[i]]=node;
return ;
}
}
return ;
} void Init()
{
memset(fir,,sizeof(fir));
memset(match,,sizeof(match));
cnt=;
} int main()
{
int n,k,to,ans;
while(~scanf("%d",&n))
{
Init();ans=;
for(int i=;i<n;i++){
scanf("%d: (%d)",&i,&k);
while(k--){
scanf("%d",&to);
addedge(i+,to+);
}
}
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
ans+=Hungary_algorithm(i);
}
printf("%d\n",n-ans/);
}
return ;
}
网络流(最大独立点集):POJ 1466 Girls and Boys的更多相关文章
- poj 1466 Girls and Boys(二分图的最大独立集)
http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submis ...
- POJ 1466 Girls and Boys
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...
- poj 1466 Girls and Boys 二分图的最大匹配
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...
- POJ 1466 Girls and Boys (匈牙利算法 最大独立集)
Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10912 Accepted: 4887 D ...
- poj 1466 Girls and Boys (最大独立集)
链接:poj 1466 题意:有n个学生,每一个学生都和一些人有关系,如今要你找出最大的人数.使得这些人之间没关系 思路:求最大独立集,最大独立集=点数-最大匹配数 分析:建图时应该是一边是男生的点, ...
- POJ 1466 Girls and Boys (ZOJ 1137 )最大独立点集
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=137 http://poj.org/problem?id=1466 题目大意: ...
- POJ 1466 Girls and Boys(二分图匹配)
[题目链接] http://poj.org/problem?id=1466 [题目大意] 给出一些人和他们所喜欢的人,两个人相互喜欢就能配成一对, 问最后没有配对的人的最少数量 [题解] 求最少数量, ...
- POJ - 1466 Girls and Boys 二分图+最大独立集
标题效果:有着n学生,有一些同学之间的特殊关系.. .为了一探究竟m学生.要求m免两者之间的学生有没有这样的特殊关系 解决问题的思路:二分图的问题,殊关系是对称的.所以能够将两个点集都设置为n个点.求 ...
- POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题
有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开 ...
随机推荐
- Weex 学习教程
一.环境搭建 1.安装Node,官网下载(http://nodejs.org/) 2.查看npm安装版本,终端输入:npm -v版本不能低于2.15.1 3.安装weex-toolkit,终端输入:n ...
- java 反射调用支付SDK
在android开发中会遇到各种SDK的接入,很是麻烦.最初在想能不能把所有的SDK都 融合到一个当中,发现有点异想天开.但是也可以解决SDK资源不小心没有引入,导致程序调用接口崩溃问题.经过查资料, ...
- JVM Run-Time Data Areas.
Ref: JVM Run-Time Data Areas class SimpleThread extends Thread { public SimpleThread(String name) { ...
- iOS socket编程
// // ViewController.m // socket // // Created by emerys on 16/3/2. // Copyright © 2016年 Emerys. All ...
- C#入门经典(第五版)学习笔记(四)
---------------集合.比较和转换--------------- C#中的数组是作为System.Array类的实例实现的,它们是集合类(Collection Classes)中的一种类型 ...
- 掌握JS
1.原生的js,好比全真教的武功,一步步从基础开始(先练气再御剑),很长一段时间内和jquery有很大差距,掌握以后发现jquery只不过是另外一种武功,看一遍既会.且当学原生到一定程度之后,可以自创 ...
- 关于fork( )函数父子进程返回值的问题
fork()是linux的系统调用函数sys_fork()的提供给用户的接口函数,fork()函数会实现对中断int 0x80的调用过程并把调用结果返回给用户程序. fork()的函数定义是在init ...
- linux 监控系统缓存和cpu
a=`free |head -n 2 |tail -n 1 |awk '{print $7}'`if [ $a -ge 900000 ];then sync && echo 1 ...
- Frame与启动流程
1. frame与bounds的区别: frame是从屏幕的左上角开始计算位置,而bounds是从新添加view左上角作为(0,0)开始计算 bounds是以自身为参考系,所以左上角的点为原点 cen ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...