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. poj 1849 Two

    /*poj 1849 two 思考一下会发现 就是求直径 直径上的中点就是两个人分开的地方(不再有交集)*/ #include<cstdio> #define maxn 100010 us ...

  2. Mac下载并编译Google安卓AOSP项目代码

    Mac下载并编译Google安卓AOSP项目代码 参考 https://source.android.com/source/index.html 这两天用Mac下载安卓AOSP源码,且把遇到的问题记下 ...

  3. mac下开发环境常用操作与命令

    [1] 修改hosts文件 vim /private/etc/hosts

  4. powerdesigner设置唯一键,但不是主键的方式

    [转载]http://blog.csdn.net/cnham/article/details/6676650 唯一约束 唯一约束与创建唯一索引基本上是一回事,因为在创建唯一约束的时候,系统会创建对应的 ...

  5. tomcat发布项目时,空文件夹未发布成功

    问题背景: 项目发布到服务器时,缺少文件夹,到时向此文件夹写数据时发生错误. 后来经查,缺少这个文件夹,项目部署发布时,并不会把空文件夹发布上去 解决: 1.在空文件中加入,一个文件.就可以发布成功 ...

  6. C#和asp.net中链接数据库中 参数的几种传递方法

    #region 参数传递方法第一种 //参数设置方法(第一种) //SqlParameter sp = new SqlParameter("@Name", str_Name); / ...

  7. ios9 http请求失败的问题

    最近做项目的时候 将电脑版本升级到10.11.3  xcode'升级到 7.2  但是在模拟器上边进行数据请求的时候告诉我说网路哦有问题 截图如下 通过网络终于找到了解决的办法  原来是ios9 采用 ...

  8. JavaScript HTML DOM - 改变CSS

    JavaScript HTML DOM - 改变CSS HTML DOM 允许 JavaScript 改变 HTML 元素的样式. 改变 HTML 样式 如需改变 HTML 元素的样式,请使用这个语法 ...

  9. 泛型? extents super

    ?可以接受任何泛型集合,但是不能编辑集合值.所以一般只在方法参数中用 例子: ? extends Number  则类型只能是Number类的子孙类 ? super String  则类型只能是Str ...

  10. mysql,left join on

    转自http://www.oschina.net/question/89964_65912 觉得很有帮助,用来学习. 即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇 ...