题意:

有一个老师想组织学生出去旅游,为了避免他们之间有情侣产生,他制定了一系列的条件,满足这些条件之一,那么这些人理论上就不会成为情侣:

身高相差40cm;性别相同;喜欢的音乐风格不同;最喜欢的运动相同。

给出若干个学生的身高,性别,喜欢的音乐风格和喜欢的运动,问最多有多少人可以出去。

思路:

对于两个人,如果以上所有条件都不满足,那么就在他们之间连边,说明他们不能同时去。

问题就转化成了求一个二分图的最大独立集。(独立集是两两之间不存在边的点的集合,最大顾名思义)

最大独立集 = 点数- 最小点覆盖  = 点数 – 最大匹配

匈牙利算法,复杂度O(n^3)。

代码:

 #include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std; const int N = ; vector<int> g[N];
int link[N];
bool vis[N]; struct node
{
int hei;
string sex,music,sport; node(int a,string b,string c,string d)
{
hei = a;
sex = b;
music = c;
sport = d;
}
}; vector<node> ns; bool dfs(int u)
{
for (int i = ;i < g[u].size();i++)
{
int v = g[u][i]; if (!vis[v])
{
vis[v] = ; if (link[v] == - || dfs(link[v]))
{
link[v] = u;
link[u] = v; return true;
}
}
} return false;
}
int solve(int n)
{
int cnt = ; memset(link,-,sizeof(link)); for (int i = ;i < n;i++)
{
if (link[i] == -)
{
memset(vis,,sizeof(vis));
if (dfs(i)) cnt++;
}
} return cnt;
} int mabs(int x)
{
return x >= ? x : -x;
} int main()
{
int t; scanf("%d",&t); while (t--)
{
int n; scanf("%d",&n); ns.clear(); for (int i = ;i < n;i++)
{
g[i].clear();
int a;
string b,c,d; cin >> a >> b >> c >> d; ns.push_back(node(a,b,c,d));
} for (int i = ;i < n;i++)
{
for (int j = i + ;j < n;j++)
{
bool f = ; if (mabs(ns[i].hei - ns[j].hei) > ) f = ;
if (ns[i].sex == ns[j].sex) f = ;
if (ns[i].music != ns[j].music) f = ;
if (ns[i].sport == ns[j].sport) f = ; if (!f)
{
g[i].push_back(j);
g[j].push_back(i);
}
}
} int ans = solve(n); printf("%d\n",n - ans);
} return ;
}

uvalive 3415 Guardian Of Decency的更多相关文章

  1. UVALive 3415 Guardian of Decency(二分图的最大独立集)

    题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...

  2. Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游

    /** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...

  3. Guardian of Decency(二分图)

    Guardian of Decency Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  4. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  5. POJ 2771 Guardian of Decency 【最大独立集】

    传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  6. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  7. uva 12083 Guardian of Decency (二分图匹配)

    uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...

  8. poj——2771 Guardian of Decency

    poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   ...

  9. Guardian of Decency UVALive - 3415(最大独立集板题)

    老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学生数量 ...

随机推荐

  1. python摸爬滚打之day010----函数进阶

    1.函数动态传参 *args : 将所有的位置参数打包成一个元组的形式. **kwargs : 将所有的关键字参数打包成一个字典的形式. 形参的接收顺序:    位置参数 > *args > ...

  2. Python一个文件调用另外一个文件的方法

    from common.Log import MyLog as Log 写法不完善修改为 import common.Log.MyLog as Log 即可

  3. python中的一些用法总结

    用python写了一个测试Demo,其中涉及到一些常用的用法,现在记录在这里,方便后续查阅: 1 python中全局变量的使用: 引用全局变量,不需要golbal声明,修改全局变量,需要使用globa ...

  4. kafka4 副本机制

    概述 每个分区有n个副本,可以承受n-1个节点故障. 每个副本都有自己的leader,其余都是follower. zk中存放分区的leader和 follower replica的信息.(get /b ...

  5. CA证书和TLS介绍

    数字签名 用自己的私钥给数据加密就叫数字签名 公钥传输威胁 在A和B的通信中,C可以把自己的公钥发给A,让A把C的公钥当成B的公钥,这样的话.B拿到加密数据反而无法解密,而C却可以解密出数据.从而实现 ...

  6. Windows XP解决显示桌面图标消失的问题

    1.依次单击“开始”,“运行”,在运行对话框中输入regsvr32 /n /i:u shell32.dll,按回车键 2.弹出“shell32.dll中的DllInstall成功”的窗口,表明已将数据 ...

  7. Centos7限速和测速

    限速 wondershaper是国外人开发的一款在Linux内核下基于TC工具的对整块网卡的限度工具. 第一种安装方法 首先下载wondershaper的rpm安装包:wondershaper-1.1 ...

  8. Android使得Fragment 切换时不重新实例化

    以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...

  9. Jmeter知识点

    聚合报告说明 https://www.cnblogs.com/duanxz/p/5464993.html JMeter之Ramp-up Period(in seconds)说明(可同时并发) http ...

  10. mybatis oracle 插入自增记录 获取主键值 写回map参数

    网上搜了好多文章照着弄都返回不了主键给map, 实践证明要在传入的map参数里写回插入的主键,要这样写 <selectKey resultType="java.lang.Integer ...