1. 程式人生 > >POJ 1636 Prison rearrangement DFS+0/1背包

POJ 1636 Prison rearrangement DFS+0/1背包

freopen 細心 mon mono print span 滾動數組 turn which

題目鏈接:?

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

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

Source

Northwestern Europe 2003

題意:

? ? ? ?有兩個監獄都有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)就是我們要求的結果。

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int m, g[402][402];
int p1[202], p2[202], dp[110][110];
int cnt;
bool vis[402];
void dfs(int s)
{
    vis[s] = true;
    if(s <= m)
        p1[cnt]++;
    else
        p2[cnt]++;
    for(int i = 1; i <= 2*m; i++)
        if(!vis[i] && g[s][i])
            dfs(i);
}
int main()
{
    int t, r, a, b;
    //freopen("out.txt", "w", stdout);
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &m, &r);
        memset(g, 0, sizeof(g));
        while(r--)
        {
            scanf("%d%d", &a, &b);
            g[a][b+m] = g[b+m][a] = 1;
        }
        memset(vis, false, sizeof(vis));
        cnt = 0;
        for(int i = 1; i <= 2*m; i++)
            if(!vis[i])
            {
                p1[cnt]= p2[cnt] = 0;
                dfs(i);
                cnt++;
            }
        memset(dp, false, sizeof(dp));
        dp[0][0] = true;
        for(int i = 0; i < cnt; i++)
            for(int j = m/2; j >= p1[i]; j--)
                for(int k = m/2; k >= p2[i]; k--)
                    if(dp[j-p1[i]][k-p2[i]])
                        dp[j][k] = true;
        int index = m/2;
        for(int i = m/2; i >= 0; i--)
            if(dp[i][i])
            {
                index = i;
                break;
            }
        printf("%d\n", index);
    }
    return 0;
}

POJ 1636 Prison rearrangement DFS+0/1背包