poj 1636 Prison rearrangement

来源:互联网 发布:保护眼睛的电脑软件 编辑:程序博客网 时间:2024/05/17 07:27

题目传送门

Prison rearrangement
Time Limit: 3000MS Memory Limit: 10000KTotal Submissions: 2692 Accepted: 1171

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

3101 03 31 21 31 18 121 11 21 31 42 53 54 55 56 67 68 78 8

Sample Output

5003

Source

Northwestern Europe 2003

题解

很显然我们会发现当我们要移动某一个人时,与他有关的人也会移动。因此我们可以将这些人当作一个物体。我们设定dp[i][j]表示第一个监狱i个人与第二个监狱j个人能否交换。不难得出dp[i][j] = dp[i][j] | dp[i - x][j - y](x,y为处于同一物体的第一第二监狱人数)。至于建立物体则直接用dfs。

#include <cstdio>#include <cstring>#include <vector>using namespace std;bool dp[103][103], vis[2][203];vector<int>edge[2][203];int Siz[2];void dfs(int Belong, int u) {vis[Belong][u] = 1; ++Siz[Belong];for (int i = 0; i < edge[Belong][u].size(); ++i)if (!vis[Belong^1][edge[Belong][u][i]]) dfs(Belong ^ 1, edge[Belong][u][i]);}void Search(int Belong, int n) {for (int i = 1; i <= n; ++i)if (!vis[Belong][i]) {memset(Siz, 0, sizeof Siz);dfs(Belong, i);for (int x = n >> 1; x >= Siz[0]; --x)for (int y = n >> 1; y >= Siz[1]; --y)dp[x][y] |= dp[x - Siz[0]][y - Siz[1]];}}int main() {int T, n, k, a, b;scanf("%d", &T);while (T--) {scanf("%d%d", &n, &k);memset(dp, 0, sizeof dp); dp[0][0] = true;memset(vis, 0, sizeof vis);for (int i = 1; i <= n; ++i) edge[0][i].clear(), edge[1][i].clear();for (int i = 1; i <= k; ++i) {scanf("%d%d", &a, &b);edge[0][a].push_back(b); edge[1][b].push_back(a);}Search(0, n); Search(1, n);for (int i = n >> 1; ~i; --i)if (dp[i][i]) {printf("%d\n", i); break;}}return 0;}


0 2
原创粉丝点击