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. (转载)Hadoop示例程序WordCount详解

    最近在学习云计算,研究Haddop框架,费了一整天时间将Hadoop在Linux下完全运行起来,看到官方的map-reduce的demo程序WordCount,仔细研究了一下,算做入门了. 其实Wor ...

  2. [洛谷P2384]最短路

    题目大意:给你一个图,要你求出其中1->n路径中乘积最小的一条路 题解:用$log_2$把乘法变成加法,然后记录每个点的前驱,最后求出答案 C++ Code: #include<cstdi ...

  3. visio中相关设置-菜单视图

    1.获取或设置窗口中页面的当前显示大小(缩放系数) Window.Zoom Dim dZoom As Double dZoom = m_Visio.Window.Zoom'获取显示比例 m_Visio ...

  4. [Leetcode] Reorder list 重排链表

    Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...

  5. PHP 5.4语法改进与弃用特性

    PHP 5.4于本月尘埃落定,它是 PHP 自 2009 年以来的首次重大更新.该版本对语言部分进行了增强,包括支持 Traits 和移除部分争议特性. Traits 同 Java 和 .NET 一样 ...

  6. [codeforces gym Matrix God]随机矩阵乘法

    题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...

  7. ng websocket

    ng使用websocket 1.安装依赖库npm install ws --save 2.安装类型定义文件 npm install @types/ws --save 3.编写服务 import { I ...

  8. 两数之和 [ leetcode ]

    原题地址:https://leetcode-cn.com/articles/two-sum/ 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元 ...

  9. nginx压力测试和优化配置

    115 yum -y install gcc automake autoconf libtool make 116 yum install ctags 117 mkdir -m 644 -p /usr ...

  10. hadoop 架构