您的位置:首页 > 运维架构

Poj 3687 Labeling Balls (逆向Topo)

2015-08-07 21:55 465 查看
Labeling Balls

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12027Accepted: 3455
Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

No two balls share the same label.
The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".

Can you help windy to find a solution?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤
40,000). The next M line each contain two integers a and b indicating the ball labeled with a must be lighter than the
one labeled with b. (1 ≤ a, b ≤ N) There is a blank line before each test case.

Output

For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with
the smallest weight for label 3 and so on... If no solution exists, output -1 instead.

Sample Input
5

4 0

4 1
1 1

4 2
1 2
2 1

4 1
2 1

4 1
3 2

Sample Output
1 2 3 4
-1
-1
2 1 3 4
1 3 2 4


这道题,要不仔细读,会WA的很惨- - !

注意一个是标签的1-n,另一个是重量的1-n。弄清楚标签和重量。输入的是lable间的关系。输出的是重量间的关系。

题意:有N个球,重量分别是1~N,给着n个球贴上标签。输入n,m代表n个球和m条边(a b),代表 标签为a的要比标签为b的轻。最后输出标签1~N对应的重量(注意是重量,而不是轻重关系),还有要注意“ you
should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... ”,意思是标签小的要尽可能贴在重量小的球上。

那么就有一些问题,[b]



例如上图,若先取入度为0且最小的话,是先取出3,而我们的目的是让标号小的尽量靠前,即应该让1尽量靠前,这与先取出3相违背,这时应该先取出6才能使1尽量靠前。对于2,8,2肯定先出队列。归纳起来便是对于若干平行的路径,小的头部不一定排在前面(如3),但大的尾部一定排在后面(如8).
[/b]
[b]所以就不能按照入度,来进行拓扑了。

[/b]

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int n,m,s;
int Map[210][210],out[210],a[210];
int Topo()
{
    int i,j,k;
    priority_queue<int,vector<int>,less<int> >q;//注意是less即按从大到小的顺序<span id="transmark"></span>
    while(!q.empty())
        q.pop();
    for(i=1;i<=n;i++)
        if(out[i]==0)
            q.push(i);
    memset(a,0,sizeof(a));
    while(!q.empty())
    {
        k=q.top();
        q.pop();
        a[s++]=k;
        for(i=1;i<=n;i++)
        {
            if(Map[i][k])
            {
                out[i]--;
                if(out[i]==0)
                    q.push(i);
            }
        }
    }
    if(s==n)
        return 1;
    return 0;
}
int main()
{
    int i,j,k,cla;
    scanf("%d",&cla);
    while(cla--)
    {
        s=0;
        scanf("%d%d",&n,&m);
        int x,y,bj=0;
        memset(Map,0,sizeof(Map));
        memset(out,0,sizeof(out));
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            if(Map[x][y]==0)
            {
                out[x]++;
                Map[x][y]=1;
            }
        }
        k=Topo();
        int now[210];
        if(k)
        {
            for(i=0;i<=n;i++)
                now[a[i] ]=n-i;
            for(i=1;i<=n;i++)
                i==n?printf("%d\n",now[i]):printf("%d ",now[i]);
        }
        else
        printf("-1\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: