LA 3415 Guardian of Decency (最大独立集)
来源:互联网 发布:数据修炼系统笔趣阁 编辑:程序博客网 时间:2024/05/01 14:21
题目大意:选择尽可能多的学生,两两之间满足四个条件中至少1个:(1)身高差大于40cm (2)性别相同 (3) 最喜欢的音乐属于不同类型 (4)最喜欢的体育比赛相同
建图:将每个学生拆成两个点。当两个学生之间四个条件都不满足的话,连一条边。如此可构成二分图。为什么?注意到条件2,有边的两点必然性别不同,性别相同的一定不会有边。这样可以将点分成两个集合,每个集合中的点和点不会有边。
问题转化为:选择尽量多的点,任意两个点不相邻,即最大独立集。
根据性质:最大独立集=顶点总数-最大匹配数求解即可。
注意由于把学生拆成了两个点,因此求出来的最大匹配数是原来的两倍。需要除以2
被ios::sync_with_stdio(false)给坑了。使用这个后,scanf与cin就不能混用啦!!
#include<bits/stdc++.h>using namespace std;#define N 505struct P{ int h; char sex; string music,sport;} p[N];int t,n;bool T[N],S[N],M[N][N];int Left[N];bool match(int x){ S[x]=1; for(int i=1; i<=n; ++i) if(M[x][i]&&!T[i]) { T[i]=1; if(!Left[i]||match(Left[i])) { Left[i]=x; return 1; } } return 0;}int main(){ ios::sync_with_stdio(false); int i,j; cin>>t; while(t--) { cin>>n; for(i=1; i<=n; ++i) cin>>p[i].h>>p[i].sex>>p[i].music>>p[i].sport; memset(M,0,sizeof(M)); for(i=1; i<=n; ++i) for(j=1; j<=n; ++j) if(abs(p[i].h-p[j].h)<=40&&p[i].sex!=p[j].sex&&p[i].music==p[j].music&&p[i].sport!=p[j].sport) M[i][j]=1; memset(Left,0,sizeof(Left)); int ans=0; for(i=1; i<=n; ++i) { for(j=1; j<=n; ++j) T[j]=0; ans+=match(i); } cout<<n-ans/2<<endl; } return 0;}
0 0
- LA 3415 Guardian of Decency (最大独立集)
- LA 3415 - Guardian of Decency【最大独立集】
- LA 3415 Guardian of Decency (二分图最大独立集)
- LA - 3415 - Guardian of Decency(二分图最大独立点集)
- UVALive 3415 Guardian of Decency(最大独立集)
- POJ 2771 Guardian of Decency (最大独立集Hungary)
- POJ 2771 Guardian of Decency(最大独立点集)
- POJ 2771 Guardian of Decency 最大独立集
- 【POJ】2771 Guardian of Decency 最大独立集
- POJ 2771 Guardian of Decency(二分图最大独立集)
- poj 2771 Guardian of Decency 最大独立集
- UVA 12083 Guardian of Decency(最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- UVAlive3415 Guardian of Decency(最大独立集)
- poj 2771 Guardian of Decency(最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- UVALive3415[Guardian of Decency] 二分图最大独立集模型
- UVALive 3415 Guardian of Decency(二分图最大独立集)
- 新开博客
- MySQL索引不仅影响查询速度而且影响查询结果的排序
- 给Jquery easyui 的datagrid 每行增加操作链接
- android View 详解
- 帧中继
- LA 3415 Guardian of Decency (最大独立集)
- 用service做的音乐播放
- 推荐:21副GIF动图让你了解各种数学概念
- android: listview组件的应用-模拟微博
- HQL: Hibernate查询语言
- xenserver外部网络监听配置 (二)
- Unity编辑器
- 使用IntelliJ IDEA,gradle开发Java web应用步骤
- <随笔>我的计算机视觉之路(一):转行CV