uvalive 3415 Guardian Of Decency
题意:
有一个老师想组织学生出去旅游,为了避免他们之间有情侣产生,他制定了一系列的条件,满足这些条件之一,那么这些人理论上就不会成为情侣:
身高相差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的更多相关文章
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...
- Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游
/** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...
- Guardian of Decency(二分图)
Guardian of Decency Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- POJ 2771 Guardian of Decency 【最大独立集】
传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Tot ...
- POJ 2771 Guardian of Decency (二分图最大点独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6133 Accepted: 25 ...
- uva 12083 Guardian of Decency (二分图匹配)
uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- Guardian of Decency UVALive - 3415(最大独立集板题)
老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学生数量 ...
随机推荐
- Eclipse实用小插件
MyBatipse插件 描述 用于mybatis的Dao层或者mapper层的方法直接跳到对应的xml文件对应的方法 安装 进入IDE(eclipse)的Help——>Install New S ...
- JS实现倒计时(天数,时,分,秒)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" > <titl ...
- Python开发【模块】:aiohttp(一)
AIOHTTP 用于asyncio和Python的异步HTTP客户端/服务器 主要特点: 支持客户端和HTTP服务器. 支持服务器WebSockets和 客户端WebSockets开箱即用,没有回调地 ...
- Linux命令小计
一.yum和apt-get的区别 Linux系统下安装包格式有:rpm包和deb包. pm包主要应用在RedHat系列包括 Fedora等发行版的Linux系统上 deb包主要应用于Debian系列包 ...
- javax.lang.model Implementation Backed by Core Reflection
javax.lang.model Implementation Backed by Core Reflection 1.javax.lang.model: How do I get the type ...
- try catch和spring事务
- python+rabbitMQ实现生产者和消费者模式
(一)安装一个消息中间件,如:rabbitMQ (二)生产者 sendmq.py import pika import sys import time # 远程rabbitmq服务的配置信息 user ...
- vs2005新建项目中没有ASP.NET WEB应用程序
今天正准备使用vs 2005,发现根本打不开老师发过来的源代码Portal_Article.csproj文件,上网查了一下,好多人都说是是因为没有给vs 2005打补丁.我的新建项目里根本没有ASP. ...
- 实例讲解TP5中关联模型
https://blog.csdn.net/github_37512301/article/details/75675054 一.关联模型在关系型数据库中,表之间有一对一.一对多.多对多的关系.在 T ...
- 虚函数后面的const=0
const 和 =0要分开理解. 成员函数后面用 const 修饰,const表示this是一个指向常量的指针,即对象成为一个常量,即它的成员不能够变化.(默认情况下,this的类型是指向类类型非常量 ...