题目链接: 

id=1636">POJ 1636 Prison rearrangement

Prison rearrangement
Time Limit: 3000MS   Memory Limit: 10000K
Total Submissions: 2194   Accepted: 984

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

  1. 3
  2. 101 0
  3. 3 3
  4. 1 2
  5. 1 3
  6. 1 1
  7. 8 12
  8. 1 1
  9. 1 2
  10. 1 3
  11. 1 4
  12. 2 5
  13. 3 5
  14. 4 5
  15. 5 5
  16. 6 6
  17. 7 6
  18. 8 7
  19. 8 8

Sample Output

  1. 50
  2. 0
  3. 3

Source

题意:

       有两个监狱都有m个囚犯,如今为了更好地管理,须要相互交换一部分人(小于等于m/2)。可是有一个问题就是,某一些有冲突的人的人不能待在一个监狱里面。也就是说。监狱1中的囚犯A和监狱2的囚犯B假设放在一起的话,将会产生非常严重的后果。如今你要求出可行的最大交换人数(不大于一半)。使得有冲突的人不能待在一个监狱里面。

思路:

       
这道题卡了挺久,网上看了非常多题解,然后自己认真想了想,发觉自己对背包的理解还不是非常透彻。

想想看,首先要将有关系的人分开成一个个集合(p, q)。表示监狱1中要拿出p个人交换的话,监狱2得同一时候拿出q个人。怎样找到这个集合呢?细心地人一眼就看出了是求连通分量了。

先构图,由于两个监狱的囚犯的人数和编号都是一样的,为了构图方便。我们将第二个监狱的囚犯的便后向后挪m(+m)。

然后有关系的两个人之间连一条无向边。之后的事情就交给dfs求连通分支了,当然中间要记录监狱1和监狱2须要交换多少个人。

那如今我们得到了cnt个连通分支了,也就是有cnt个集合(p, q),如今看看0/1背包的思想。

我们用二维数组来记录在不发生危急的情况下可进行交换的情况。dp[i][j] = true表示第一个监狱拿出i个人、第二个监狱拿出j个人进行交换不产生冲突的情况。

利用滚动数组的思想,从后往前更新全部可能情况。

由于最后要保证两个监狱交换人数同样。因而找到最大的i使dp[i][i]
== true,i(《= m/2)就是我们要求的结果。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int m, g[402][402];
  7. int p1[202], p2[202], dp[110][110];
  8. int cnt;
  9. bool vis[402];
  10. void dfs(int s)
  11. {
  12. vis[s] = true;
  13. if(s <= m)
  14. p1[cnt]++;
  15. else
  16. p2[cnt]++;
  17. for(int i = 1; i <= 2*m; i++)
  18. if(!vis[i] && g[s][i])
  19. dfs(i);
  20. }
  21. int main()
  22. {
  23. int t, r, a, b;
  24. //freopen("out.txt", "w", stdout);
  25. scanf("%d", &t);
  26. while(t--)
  27. {
  28. scanf("%d%d", &m, &r);
  29. memset(g, 0, sizeof(g));
  30. while(r--)
  31. {
  32. scanf("%d%d", &a, &b);
  33. g[a][b+m] = g[b+m][a] = 1;
  34. }
  35. memset(vis, false, sizeof(vis));
  36. cnt = 0;
  37. for(int i = 1; i <= 2*m; i++)
  38. if(!vis[i])
  39. {
  40. p1[cnt]= p2[cnt] = 0;
  41. dfs(i);
  42. cnt++;
  43. }
  44. memset(dp, false, sizeof(dp));
  45. dp[0][0] = true;
  46. for(int i = 0; i < cnt; i++)
  47. for(int j = m/2; j >= p1[i]; j--)
  48. for(int k = m/2; k >= p2[i]; k--)
  49. if(dp[j-p1[i]][k-p2[i]])
  50. dp[j][k] = true;
  51. int index = m/2;
  52. for(int i = m/2; i >= 0; i--)
  53. if(dp[i][i])
  54. {
  55. index = i;
  56. break;
  57. }
  58. printf("%d\n", index);
  59. }
  60. return 0;
  61. }

POJ 1636 Prison rearrangement DFS+0/1背包的更多相关文章

  1. poj 1636 Prison rearrangement

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

  2. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  3. [POJ]Find The Multiple(DFS)

    题目链接 http://poj.org/problem?id=1426 题意 输入一个数n,输出任意一个 只含0.1且能被n整除的数m.保证n<=200,m最多100位. 题解 DFS/BFS都 ...

  4. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  5. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  6. Sticks POJ - 1011 少林神棍 dfs四次剪枝

    http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  9. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

随机推荐

  1. Linux使用笔记

    1:Ubuntu系统获取超级权限: 在终端输入:sudo passwd,重置uinx密码.然后输入 su ,输入密码,即可进入root权限. 2:更改文件属性 Linux文件的基本权限有九个,分别是o ...

  2. lxml包引入错误

    在使用第三方包lxml引入etree模块时报错: >>> from lxml import etree Traceback (most recent call last): File ...

  3. Xamarin.Android之SQLite.NET ORM

    一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之ContentProvider>的学习,我们已经掌握了如何 ...

  4. 使用nmap查看web服务支持的http methods

    安装nmap yum install nmap 查看web server支持的http methods u02 ~]$ nmap -p --script http-methods www.somewh ...

  5. 安装 directx sdk 出现 S1023 解决

    造成原因: directx sdk 在安装10.0.30319 of the Visual C++ Redistributable的时候发现有一个新版本的 Microsoft Visual C++ 2 ...

  6. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  7. 彻底解决asp.net mvc5.2.2:vs2013 cshtml视图文件报错(当前上下文中不存在名称“model”,ViewBag,Url)

    最近遇到一个奇葩的问题,在vs2013下cshtml视图文件报错,出现当前上下文中不存在名称“model”,ViewBag,Url等等),在视图中也没有智能提示了,用@model声明视图的model类 ...

  8. 【Spring】Spring框架如何集成Hibernate框架

    下面个整理一下hibernate和Spring框架的结合. 首先是引入hibernate框架的包.Spring框架的包.数据库驱动包. User.java文件 package cn.shop.bean ...

  9. java php 等,路径 上级路径,上上级路径表示方法

    如何表示上级目录 ../表示源文件所在目录的上一级目录,../../表示源文件所在目录的上上级目录,以此类推. ../表示源文件所在目录的上一级目录,../../表示源文件所在目录的上上级目录,以此类 ...

  10. jquery ajax 回调函数的值alert出来[object Object] 解决方法

    $("#activity_project").change(function(){ var pro=$("#activity_project").val(); ...