描述

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

输出

For each given data set, the program should write to standard output a line containing the result.

样例输入

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

样例输出

5
2

题意

N个人,求最大子集使得任何两个人都没有亲密关系

题解

先把亲密关系连图,跑匈牙利得到最大匹配,这里不知道男女,所以最大匹配得/2

答案就是N-最大匹配/2

代码

 #include<bits/stdc++.h>
#define pb push_back
using namespace std; const int N=; vector<int> G[N];
int n,match[N],vis[N];
bool dfs(int u)
{
for(int i=;i<(int)G[u].size();i++)
{
int v=G[u][i];
if(!vis[v])
{
vis[v]=;
if(match[v]==-||dfs(match[v]))
{
match[v]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int ans=;
memset(match,-,sizeof match);
for(int i=;i<n;i++)
{
memset(vis,,sizeof vis);
ans+=dfs(i);
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)G[i].clear();
for(int i=,x,y,k;i<n;i++)
{
scanf("%d: (%d)",&x,&k);
while(k--)scanf("%d",&y),G[x].pb(y);
}
printf("%d\n",n-hungary()/);
}
return ;
}

TZOJ 1321 Girls and Boys(匈牙利最大独立集)的更多相关文章

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

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

  2. HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1068 Girls and Boys (二分图最大独立集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合 ...

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

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

  5. HDU 1068 Girls and Boys(最大独立集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意:有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该集合 ...

  6. hdu - 1068 Girls and Boys (二分图最大独立集+拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...

  7. hdu1068 Girls and Boys 匈牙利算法(邻接表)

    #include <cstdio> #include <algorithm> #include <cstring> #include <vector> ...

  8. hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 后台设计的基石:用户权限管理(RBAC)及工作流(workflow)模型

    后台产品同学在设计后台时,会发现一般后台的各个功能模块总结起来有两大类型:功能类.流程类.在设计功能或流程前都需要预判不同的使用角色对应不同权限,设计流程前则还得思考最基本的工作流原理. 用户权限是设 ...

  2. nohup php -f xx.php &

     nohup php -f xx.php &

  3. redis 哨兵模式 Connection refused

    spring整合redis哨兵,修改了bind ,protected 任然连接拒绝,是因为哨兵的mastername 和spring里面的名称不一致..导致拒绝了...... 哨兵模式配置文件 属性  ...

  4. javascript_ajax 地址三级联动

    1.三级地址联动思路如下: 2.建立数据库.这里直接使用网上的地址数库,最后一个字段无用,先不去管它 3.建立一个server.php 文件 <?php // 数据库连接 mysql_conne ...

  5. while read line

    # grep "请求报文:" application-20170822-*.log >> applog # cat applog|cut -d ":" ...

  6. Python调用外部系统命令

    利用Python调用外部系统命令的方法可以提高编码效率.调用外部系统命令完成后可以通过获取命令执行返回结果码.执行的输出结果进行进一步的处理.本文主要描述Python常见的调用外部系统命令的方法,包括 ...

  7. Matlab实现单层感知机网络识别字母

    感知机网络的参数设置 % 具体用法: % net=newp(pr,T,TF,LF); % % pr: pr是一个R×2的矩阵,R为感知器中输入向量的维度(本例中使用35个字符表征一个字母,那么其维度为 ...

  8. windows 10 专业版 激活

    参考文章:https://jingyan.baidu.com/article/c14654134b99de0bfcfc4c8c.html http://www.windowszj.com/news/2 ...

  9. 吴裕雄 15-MySQL LIKE 子句

    我们知道在 MySQL 中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录.WHERE 子句中可以使用等号 = 来设定获取数据的 ...

  10. 前往央都之行-gdufe1529

    前往央都之行 Time Limit: 2000/1000ms (Java/Others) Problem Description: 刀光哥桐人和尤吉欧两人为了拯救爱丽丝,同时从卢利特村出发要尽快同时赶 ...