Prison rearrangement
 
Time Limit: 3000MS   Memory Limit: 10000K
Total Submissions: 2158   Accepted: 971

Description

In order to lower the risk of riots and escape attempts, the boards of two nearby prisons of equal prisoner capacity, have decided to rearrange their prisoners among themselves. They want to exchange half of the prisoners of one prison, for half of the prisoners of the other. However, from the archived information of the prisoners' crime history, they know that some pairs of prisoners are dangerous to keep in the same prison, and that is why they are separated today, i.e. for every such pair of prisoners, one prisoners serves time in the first prison, and the other in the second one. The boards agree on the importance of keeping these pairs split between the prisons, which makes their rearrangement task a bit tricky. In fact, they soon find out that sometimes it is impossible to fulfil their wish of swapping half of the prisoners. Whenever this is the case, they have to settle for exchanging as close to one half of the prisoners as possible.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two non-negative integers m and r, 1 < m < 200 being the number of prisoners in each of the two prisons, and r the number of dangerous pairs among the prisoners. Then follow r lines each containing a pair xi yi of integers in the range 1 to m,which means that prisoner xi of the first prison must not be placed in the same prison as prisoner yi of the second prison.

Output

For each test scenario, output one line containing the largest integer k <= m/2 , such that it is possible to exchange k prisoners of the first prison for k prisoners of the second prison without getting two prisoners of any dangerous pair in the same prison.

Sample Input

3
101 0
3 3
1 2
1 3
1 1
8 12
1 1
1 2
1 3
1 4
2 5
3 5
4 5
5 5
6 6
7 6
8 7
8 8

Sample Output

50
0
3
原文地址
题目意思:两个监狱,各有n个犯人,每个两个监狱之间一些犯人之间有一定的关系,对于有关系的犯人不能放在同一个监狱,原状态肯定是满足的,
因为存在这种关系的不存在同一个监狱的。求最大交换次数使得条件依然满足,并且交换次数不能超过n/2。 后记:我们两队的比赛题,琨哥说题目很水,我信了,这能水,唉,高估我们能力了,主要是用到 dfs + o1 背包问题,但是的确很难想到,我还想着用并查集呢,不会,参考了下别人的代码;
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int SIZE = ;
int m;
int r;
//map[i][j]表示i和j是否会冲突
int map[SIZE][SIZE];
//A组里的人数
int aSize;
//b组里的人数
int bSize;
//dp[i][j] 表示用A组的i个人换B组的j个人是否可行
bool dp[SIZE][SIZE];
//visited[0][i] 表示用A组中的点i是否被访问过
//visited[1][i] 表示用B组中的点i是否被访问过
bool visited[][SIZE];
void Init()
{
memset( map, , sizeof(map) );
memset( visited, , sizeof(visited) );
memset( dp, , sizeof(dp) );
}
void Input()
{
cin >> m >> r;
for( int i=; i<r; i++ )
{
int a, b;
cin >> a >> b;
map[a][b] = ;
}
}
//side=0 表示当前正在搜索A组
//side=1 表示当前正在搜索B组
//id 表示当前正在搜索的编号
void DFS( int side, int id )
{
visited[side][id] = true;
//如果当前搜索的是A组
if( side == )
{
//记录A组中的元素个数
aSize++;
for( int i=; i<=m; i++ )
{
//搜索的是B组中对应的点
if( map[id][i] && !visited[][i] )//看一组的 id ,是否有和二组的 i ,相连的不,并且二组的没有被标记;
{
DFS( , i ); //搜寻一组中是否有与二组相连的数;
}
}
}
else
{
bSize++;
for( int j=; j<=m; j++ )
{
if( map[j][id] && !visited[][j] )
{
DFS( , j );
}
}
}
}
//利用二维背包计算
void Knapsack()
{
dp[][] = true;
for( int x=m/; x>=aSize-; x-- )
{
for( int y=m/; y>=bSize-; y-- )
{
if( dp[x][y] || dp[x - aSize][y - bSize] )
{
// printf("%d %d\n",x,y);
dp[x][y] = true;
}
}
}
}
void Output()
{
for( int i=m/; i>=; i-- )
{
if( dp[i][i] ) // dp[i][i]; 表示各方都拿出来 i 个人,进行交换;
{
cout << i << endl;
break;
}
}
}
int main()
{
int caseNum;
cin >> caseNum;
while( caseNum-- )
{
Init();
Input();
for( int i=; i<=m; i++ )
{
//跳过已经处理过的节点
if( visited[][i] ) continue;
//计算A、B中的人数
aSize = ;
bSize = ;
DFS( , i ); // 搜索一次,就出现两组不相容的团体;
//利用二维背包计算
Knapsack();
}
for( int i=; i<=m; i++ )
{
if( visited[][i] ) continue;
aSize = ;
bSize = ;
DFS( , i );
Knapsack();
}
Output();
}
return ;
}

poj 1636 Prison rearrangement的更多相关文章

  1. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  2. POJ 1636 DFS+DP

    思路: 先搜索出来如果选这个点 其它哪些点必须选 跑个背包就好了 //By SiriusRen #include <cstdio> #include <cstring> #in ...

  3. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  4. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  5. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  6. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  7. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  8. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

随机推荐

  1. 在matlab中clear,clc,clf,hold作用介绍

    clear 是清变量: clc 只清屏: clf 清除图形窗口上的旧图形: hold on 是为了显示多幅图像时,防止新的窗口替代旧的窗口: close 关闭所有显示的图像.

  2. 正规方程 Normal Equation

    正规方程 Normal Equation 前几篇博客介绍了一些梯度下降的有用技巧,特征缩放(详见http://blog.csdn.net/u012328159/article/details/5103 ...

  3. leetcoder-50-Pow(x, n)

    Pow(x, n) 能够直接用库函数pow(x,n)一步搞定,但明显这样就没意思了.   參考   快 速 幂 取 模 二分.复杂度为O(logn) 递归方法 class Solution { pub ...

  4. TCP-滑动窗口概念

    参考博客: http://blog.chinaunix.net/uid-26275986-id-4109679.html

  5. Python学习之路上的几个经典问题

    1.python有三元运算符语法(类似C语言的"?")么? 语法如下: [on_true] if [expression] else [on_false] 如果[expressio ...

  6. 新型数据库Kudu应用经验分享

    小米使用kudu的案例 http://www.aiweibang.com/yuedu/60603532.html 调研kudu的情况

  7. [PHP]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑

    Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class, 它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境. 这个设备检测 ...

  8. 内网渗透技巧:判断机器真实外网IP的5种方法总结

    在内网渗透中有时需要在某台WEB服务器中留下后门,该机器可以通过内网IP建立IPC连接,但还需要获知外网IP或域名才能访问Wbshell,在无网关权限的情况下,我总结了有如下方法: 1.通过nsloo ...

  9. 一个.net Cookie组件的bug引发的题外话

    在.net里,做过Http模拟发送请求的朋友们应该遇到过,有个时候无论怎么努力,都没办法让Cookie跟网页用浏览器所收集的一样,其中原因除了有些Cookie大概是ReadOnly之外,似乎另有隐情: ...

  10. 算法笔记_089:蓝桥杯练习 7-2求arccos值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 利用标准库中的cos(x)和fabs(x)函数实现arccos(x)函数,x取值范围是[-1, 1],返回值为[0, PI].要求结果准确 ...