1. 程式人生 > >poj 1486 Sorting Slides

poj 1486 Sorting Slides

二分 push plm tar algorithm test case which starting set

Sorting Slides
Time Limit: 1000MS Memory Limit: 10000K
http://poj.org/problem?id=1486

Description

Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minimalist, he wants to do this with the minimum amount of work possible.

The situation is like this. The slides all have numbers written on them according to their order in the talk. Since the slides lie on each other and are transparent, one cannot see on which slide each number is written.
技術分享圖片

Well, one cannot see on which slide a number is written, but one may deduce which numbers are written on which slides. If we label the slides which characters A, B, C, ... as in the figure above, it is obvious that D has number 3, B has number 1, C number 2 and A number 4.

Your task, should you choose to accept it, is to write a program that automates this process.

Input

The input consists of several heap descriptions. Each heap descriptions starts with a line containing a single integer n, the number of slides in the heap. The following n lines contain four integers xmin, xmax, ymin and ymax, each, the bounding coordinates of the slides. The slides will be labeled as A, B, C, ... in the order of the input.

This is followed by n lines containing two integers each, the x- and y-coordinates of the n numbers printed on the slides. The first coordinate pair will be for number 1, the next pair for 2, etc. No number will lie on a slide boundary.

The input is terminated by a heap description starting with n = 0, which should not be processed.

Output

For each heap description in the input first output its number. Then print a series of all the slides whose numbers can be uniquely determined from the input. Order the pairs by their letter identifier.

If no matchings can be determined from the input, just print the word none on a line by itself.

Output a blank line after each test case.

Sample Input

4
6 22 10 20
4 18 6 16
8 20 2 18
10 24 4 8
9 15
19 17
11 7
21 11
2
0 2 0 2
0 2 0 2
1 1
1 1
0

Sample Output

Heap 1
(A,4) (B,1) (C,2) (D,3)

Heap 2
none

題解:這題就是煩人的幻燈片的改編版,原來是要求全部匹配,這次是要求輸出最大的匹配(我因為這個WA了一下午,我自己都佩服自己);
中間有不確定的不用管;
1.拓撲:於是拓撲排序時就不能只看幻燈片或者數字的度數,要兩者結合才能找最大匹配,因為幻燈片一對多或者數字一對多都是存在的;
技術分享圖片
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int M = 70;
int xl[M], xr[M], deg[M], yl[M], yr[M], xp[M], mp[M][M], cp[M];
bool vis[M];

int main(){
    int idc = 0, n;
    while(scanf("%d", &n) && n){
        memset(deg, 0, sizeof(deg));
        memset(mp, 0, sizeof(mp));
        memset(cp, 0, sizeof(cp));
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        for(int i = 1; i <= n; i++)scanf("%d%d%d%d", &xl[i], &xr[i], &yl[i], &yr[i]);
        for(int i = 1; i <= n; i++){
            int x, y;
            scanf("%d%d", &x, &y);
            for(int j = 1; j <= n; j++)
                if(x <= xr[j] && x >= xl[j] && y <= yr[j] && y >= yl[j]){
                    mp[i][j + n] = mp[j + n][i] = 1;
                    deg[i]++;deg[j + n]++;
                }
        }
        queue <int> q;
        for(int i = 1; i <= 2*n; i++)
            if(deg[i] == 1){
                q.push(i);
            }
        while(!q.empty()){
            int u = q.front(); q.pop();
            if(vis[u])continue;
            vis[u] = 1;
            for(int i = 1; i <= 2 * n; i++){
                if(mp[u][i]){
                    cp[u] = i, cp[i] = u;
                    vis[i] = 1;
                    deg[i]--; deg[u]--; 
                    for(int j = 1; j <= 2*n; j++)
                        if(mp[i][j]) {
                            deg[j]--, mp[i][j] = mp[j][i] = 0;
                            if(deg[j] == 1)q.push(j);
                        }
                    break;
                }
            }
        }
        for(int i = 1; i <= n; i++) if(cp[i])ans = -1;
        printf("Heap %d\n", ++idc);
        if(ans == 0)puts("none");
        else {
            for(int i = 1 + n; i <= 2*n; i++)
                if(cp[i]){
                    printf("(%c,%d) ", i - n - 1 + A, cp[i]);
                }
            puts("");    
        }
        puts("");    
    }
    
}
View Code


2.二分圖匹配,因為幻燈片和數字是一一對應關系,所以可以往這方面聯想;
原圖顯然是找一個完美匹配,必須這個匹配必須唯一;
我們考慮這個匹配中的邊,如果刪除他,匹配數減少,說明他是一條必須邊,這個匹配是可以唯一確定的;
我們就枚舉每條邊看是否為必須邊
技術分享圖片
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int M = 70;
int xl[M], xr[M], yl[M], tot, t[M], yr[M], mp[M][M], lik[M];
bool vis[M];
int cc, n;
bool dfs(int u){
    for(int i = 1 + n; i <= 2 * n; i++){
        if(mp[u][i] && mp[u][i] != cc && !vis[i]){
            vis[i] = 1;
            if(!lik[i] || dfs(lik[i])){
                lik[i] = u; lik[u] = i;
                return 1;
            }
        }
    }
    return 0;
}


int hungry(){
    memset(lik, 0, sizeof(lik));
    int ans = 0;
    for(int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));
        if(dfs(i))ans++;
    }
    return ans;
}


int main(){
    int idc = 0;
    while(scanf("%d", &n) && n){
        memset(t, 0, sizeof(t));
        memset(mp, 0, sizeof(mp));
        cc = 0; tot = 0;
        int ans = 0;
        for(int i = 1; i <= n; i++)scanf("%d%d%d%d", &xl[i], &xr[i], &yl[i], &yr[i]);
        for(int i = 1; i <= n; i++){
            int x, y;
            scanf("%d%d", &x, &y);
            for(int j = 1; j <= n; j++)
                if(x <= xr[j] && x >= xl[j] && y <= yr[j] && y >= yl[j])mp[j][i + n] = ++tot;
        }
        printf("Heap %d\n", ++idc);
        int Max = hungry(), fg = 0;
        for(int i = 1; i <= n; i++)t[i] = lik[i];
        for(int i = 1; i <= n; i++){
            if(t[i]){
                cc = mp[i][t[i]];
                int tmp = hungry();
                if(tmp < n) printf("(%c,%d) ", i - 1 + A, t[i] - n), fg = 1;
            }
        }
        if(!fg)printf("none");
        puts("");puts("");    
    }
    
}
View Code

 

poj 1486 Sorting Slides