Girls and Boys

Time Limit: 5000ms
Memory Limit: 10000KB

This problem will be judged on PKU. Original ID: 1466
64-bit integer IO format: %lld      Java class name: Main

In 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.

Input

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 (n <=500 ), for n subjects.

Output

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 

题目大意:有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的更多相关文章

  1. poj 1466 Girls and Boys(二分图的最大独立集)

    http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submis ...

  2. POJ 1466 Girls and Boys

    Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...

  3. poj 1466 Girls and Boys 二分图的最大匹配

    Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...

  4. POJ 1466 Girls and Boys (匈牙利算法 最大独立集)

    Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10912   Accepted: 4887 D ...

  5. poj 1466 Girls and Boys (最大独立集)

    链接:poj 1466 题意:有n个学生,每一个学生都和一些人有关系,如今要你找出最大的人数.使得这些人之间没关系 思路:求最大独立集,最大独立集=点数-最大匹配数 分析:建图时应该是一边是男生的点, ...

  6. POJ 1466 Girls and Boys (ZOJ 1137 )最大独立点集

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=137 http://poj.org/problem?id=1466 题目大意: ...

  7. POJ 1466 Girls and Boys(二分图匹配)

    [题目链接] http://poj.org/problem?id=1466 [题目大意] 给出一些人和他们所喜欢的人,两个人相互喜欢就能配成一对, 问最后没有配对的人的最少数量 [题解] 求最少数量, ...

  8. POJ - 1466 Girls and Boys 二分图+最大独立集

    标题效果:有着n学生,有一些同学之间的特殊关系.. .为了一探究竟m学生.要求m免两者之间的学生有没有这样的特殊关系 解决问题的思路:二分图的问题,殊关系是对称的.所以能够将两个点集都设置为n个点.求 ...

  9. POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

    有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开 ...

随机推荐

  1. heibernate增删改查总结一下自己的不足

    难点也就我不熟悉的是数据库语句的书写,要加强复杂查询语句的书写 /* 简单的在共享类中已经可以用的了 * 总结: * * --------------------------------------查 ...

  2. C#生成验证码实例

    常用生成验证码实例封装: /// <summary> /// 生成内存位图 /// </summary> /// <param name="Code" ...

  3. Source not found for AeceManager$$FastClassByCGLIB$$15dcd49c.invoke(int, Object, Object[]) line: not available 问题解决

    一般出现这个问题,是manager的问题.控制台没有报错.是调试出来的.. 解决办法: 在调用此方法的manager里的方法上加上try  ...catch 重新启动调试, 就可在控台看到问题所在. ...

  4. JAVA 环境变量

    Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...

  5. IIS部署.NET项目的有关事项_2015.07.02

    今天在做项目中的关于发送邮件的一些功能.在部署服务的时候遇到了一些奇葩的问题,基本上是和IIS有关的问题. 首先,项目是基于.NET Framework4.0 版本的,由于本人用的是一台新配置好的机器 ...

  6. [转]Delphi中,让程序只运行一次的方法

    program onlyRunOne; uses Forms,Windows,SysUtils, Dialogs, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} v ...

  7. bootstrap学习--模态弹出框modals轮子

    1.点击按钮型 <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css"> ...

  8. 为什么Tomcat的webapps目录下新建的目录不能访问html文件?

    在Tomcat安装目录中,webapps默认为部署网站用的目录.webapps/ROOT是网站的根目录,其它目录都是网站的子目录,如webapps\jsp-examples目录.但是,当我们新建一个子 ...

  9. OC学习总结之面向对象和类

    OC学习总结之面向对象和类   Objective-c是c语言的母集合,它的原意就是在原始的c语言的主体上加入面向对象的特性.1.面向对象和面向过程  面向对象和面向过程是编程的两种思考方式.面向对象 ...

  10. Java中的自动拆箱装箱(Autoboxing&Unboxing)

    一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类 ...