layout: post

title: 训练指南 UVALive - 3415(最大点独立集)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 二分图

- 图论

- 训练指南


Guardian of Decency

UVALive - 3415

我们将男女分开来 就可以建出一个二分图,对于任意的男女生 只要上边四个条件一个也不满足 就表示不能同时去 ,那么我们在其中间连一条边,那么最终的结果就是我们从中取出尽量多的点,使得任意两个点之间没有连线。那么问题就转化成了求最大点独立集。 二分图的最大点独立集= 总点数 - 最大匹配(最小点覆盖)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e3+50;
const ll inf=1e10;
const ll INF = 1000000000;
const double eps=1e-5;
#define bug cout<<"bbibibibbbb="<<endl;
/// 二分图最大基数匹配
struct BPM{
int n,m; /// 左右顶点个数
int G[maxn][maxn]; /// 邻接表
int left[maxn]; /// left[i]为右边第i个点的匹配点编号,-1表示不存在
bool T[maxn]; /// T[i]为右边第i个点是否已标记 int right[maxn]; /// 求最小覆盖用
bool S[maxn]; /// 求最小覆盖用 void init(int n,int m){
this->n=n;
this->m=m;
memset(G,0,sizeof(G));
} /* void AddEdge(int u,int v){
G[u].push_back(v);
}*/ bool match(int u){
S[u]=true;
for(int v=0;v<m;v++){
//int v=G[u][i];
if(G[u][v]&&!T[v]){
T[v]=true;
if(left[v]==-1||match(left[v])){
left[v]=u;
right[u]=v;
return true;
}
}
}
return false;
}
/// 求最大匹配
int solve(){
memset(left,-1,sizeof(left));
memset(right,-1,sizeof(right));
int ans=0;
for(int u=0;u<n;u++){
memset(S,0,sizeof(S));
memset(T,0,sizeof(T));
if(match(u))ans++;
}
return ans;
}
/// 求最小覆盖。X和Y为最小覆盖中的点集
int mincover(vector<int>& X,vector<int>& Y){
int ans=solve();
memset(S,0,sizeof(S));
memset(T,0,sizeof(T));
for(int u=0;u<n;u++)
if(right[u]==-1)match(u);
for(int u=0;u<n;u++)
if(!S[u])X.push_back(u);
for(int v=0;v<n;v++)
if(T[v])Y.push_back(v);
return ans;
}
};
BPM solver;
struct node{
int h;
string m,s;
node(int h,string m,string s):h(h),m(m),s(s){}
};
bool ok(node a,node b){
return abs(a.h-b.h)<=40&&a.m==b.m&&a.s!=b.s;
}
int R,C,N;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<node>a,b;
for(int i=0;i<n;i++){
int h;string m,s,sex;
cin>>h>>sex>>m>>s;
if(sex[0]=='M')a.push_back(node(h,m,s));
else b.push_back(node(h,m,s));
}
int x=a.size(),y=b.size();
solver.init(x,y);
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
if(ok(a[i],b[j]))solver.G[i][j]=1;
cout<<x+y-solver.solve()<<endl;
}
return 0;
}

训练指南 UVALive - 3415(最大点独立集)的更多相关文章

  1. 训练指南 UVALive - 3126(DAG最小路径覆盖)

    layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...

  2. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  3. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  4. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  5. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  6. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  7. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  8. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  9. 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)

    layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...

随机推荐

  1. [洛谷P4124][CQOI2016]手机号码

    题目大意:给你两个$l,r$,求出$[l,r]$中符合要求的数,要求为至少有$3$个相邻的相同数字,且不可以同时出现$8$和$4$ 题解:数位$DP$ 卡点:无 C++ Code: #include ...

  2. 【luogu 1439 最长公共子序列】

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

  3. 【NOIP模拟赛】chess 建图+spfa统计方案数

    似乎弗洛伊德和迪杰斯特拉都干不了统计方案数,spfa的话就是不断入队就好. #include <cstdio> #include <cstring> #include < ...

  4. Any gotchas at all with converting from MyISAM to InnoDB?

    Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...

  5. SICAU-OJ: 第k小

    第k小 题意: 给出一个长度不超过5000的字符串,然后让你找出第K小的字串(1<=K<=5).重复的串大小相等. 题解: 这里我们知道某些串的前缀是肯定小于等于其本身的. 那么长度为5的 ...

  6. Django随笔 01

    Django 视图 不处理用户输入,而仅仅决定要展现哪些数据给用户: Django 模板 仅仅决定如何展现Django视图指定的数据. dd http://blog.csdn.net/pipisorr ...

  7. java 多线程 原子性

    原子性 原子性:原子操作是不能被线程调度机制中断的操作,一旦操作开始,那么它就一定可以在可能发生的“上下文切换”之前(切换到其他线程执行)执行完毕. 依赖原子性是很棘手且很危险的,除非你是并发专家,否 ...

  8. gitlab之:gitlab 403 forbidden 并发引起ip被封

    步骤: * 打开/etc/gitlab/gitlab.rb文件. * 查找gitlab_rails['rack_attack_git_basic_auth']关键词. * 取消注释 * 修改ip_wh ...

  9. jsp小知识点(2)

    一:自定义的函数库:在wh.tld中 <description>JSTL 1.1 functions library</description> <display-nam ...

  10. [ CodeVS冲杯之路 ] P2456

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/2456/ 用贪心的思想,木材当然要尽量分成多的木板,而大的木材能够分成大木板,但是小的木材不一定能够分成大的木板,所以木 ...