传送门:http://poj.org/problem?id=1636

题意:有两个监狱,每个监狱有n个人,有m种关系,表示A监狱第i个人不能跟B监狱第j个人在一个监狱,问你最多能换几组人(从A,B监狱互换一个人,ans<=n/2)

方法:首先这是一个很明显的二分图,我们可以很轻松的建边,但交换一次要把所有与这两个人有关的情况都遍历一遍.dp[i][j]表示从A监狱换i个人再从B监狱换j个人是否可行.

代码

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node{
int a,b;
}guan[405];
int n,m,fa[405];
int find(int x)
{
if (fa[x]==x) return x;
else
{
guan[fa[x]].a+=guan[x].a;
guan[fa[x]].b+=guan[x].b;
guan[x].a=guan[x].b=0;
return fa[x]=find(fa[x]);
}
}
bool f[205][205];
int main()
{
int t;
cin>>t;
while (t--)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=2*n;i++)//初始化
{
if (i<=n) guan[i].a=1,guan[i].b=0,fa[i]=i;
else guan[i].a=0,guan[i].b=1,fa[i]=i;
}
for (int i=1;i<=m;i++)//建边
{
int x,y;
scanf("%d%d",&x,&y);
y+=n;
int dx=find(x);
int dy=find(y);
if (dx!=dy)
{
if (dx<dy) fa[dy]=dx;
else fa[dx]=dy;
}
}
memset(f,false,sizeof(f));
f[0][0]=true;
for (int i=1;i<=2*n;i++)
{
find(i);
}
int fx=0,fy=0;
for (int i=1;i<=2*n;i++)//solve
{
int z=find(i);
int x=guan[z].a,y=guan[z].b;
if (x&&y)
{
for (int i=n/2;i>=x;i--)
for (int j=n/2;j>=y;j--)
{
if (i-x>=0&&j-y>=0&&f[i-x][j-y]) f[i][j]=true;
} }
else if (x) fx++;
else if (y) fy++;
guan[z].a=guan[z].b=0;
}
int ans=0;
int y=min(fx,fy);
for (int i=0;i<=n/2;i++)//统计
for (int j=0;j<=n/2;j++)
if (f[i][j])
{
if (i==j)
ans=max(ans,i+y);
else if (i<=j&&i+fx>=j)
ans=max(ans,j+min(fx-j+i,fy));
else if (j<=i&&j+fy>=i)
ans=max(ans,i+min(fy-i+j,fx));
}
printf("%d\n",min(ans,n/2));
}
}

poj 1626的更多相关文章

  1. 括号序列问题 uva 1626 poj 1141【区间dp】

    首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. 改善你的jQuery的25个步骤 千倍级效率提升

    1. 从Google Code加载jQueryGoogle Code上已经托管了多种JavaScript类库,从Google Code上加载jQuery比直接从你的服务器加载更有优势.它节省了你服务器 ...

  2. Jmeter学习(三十三)调试工具Debug Sampler

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  3. 设计模式学习笔记c++版——单例模式

    特别注意单例模式c++实现在main.cpp中引用的时候要去申明下: Singleton * Singleton::m_Instance = NULL; //定义性声明 不然会报错:无法解析的外部符号 ...

  4. Webform Application、ViewState

    Application(全局对象) Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApplication类对象Application被自动 ...

  5. js乱码解决方法

    在开发中引用了Bootstrap多选插件,将其中显示的英文改为中文后,页面出现乱码. 对于大多数的Web页面我们一般使用俩种编码:UTF-8和GB2312,所以我们只要统一页面和JS的编码就可以避免乱 ...

  6. background-image小解

    body { width: 600px; height: 500px; border: 10px solid #789; margin: 10px 100px; padding: 40px; back ...

  7. 在Page_Loaded下删除PivotItem出错的解决方案

    之前我一个例子中出现无法再页面Loaded事件中删除PivotItem的情况,页面会报错 Value does not fall within the expected range. 附图 原因是因为 ...

  8. node.js表单——formidable/////z

    node.js表单--formidable   node处理表单请求,需要用到formidable包.安装formidable包的命令如下: npm install formidable 安装pack ...

  9. HDU5870 Alice's Adventure in Wonderland

    大概做法是这样的 考虑最朴素的做法,预处理出1到所有点的最短路数组dis1和方案数数组cnt1,和预处理出n到所有点的最短路数组dis2和方案数数组出cnt2,然后暴力枚举点对(A,B),如果A和B之 ...

  10. Win7开机登陆密码忘记了?不必重做系统(详图)

     1)如果是普通账户密码忘了.方法:重新启动电脑,启动到系统登录界面时,同时按住Ctrl+Alt键,然后连击Del键两次,会出现新的登录界面,用户名处输入“Administrator”密码为空,回车即 ...