Poj 3687 Labeling Balls (逆向Topo)

来源:互联网 发布:魔法王座各种升阶数据 编辑:程序博客网 时间:2024/05/17 22:26
Labeling Balls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12027 Accepted: 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:

  1. No two balls share the same label.
  2. 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

54 04 11 14 21 22 14 12 14 13 2

Sample Output

1 2 3 4-1-12 1 3 41 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... ”,意思是标签小的要尽可能贴在重量小的球上。

那么就有一些问题,

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

所以就不能按照入度,来进行拓扑了。

(注意又有标号又有重量,并且又要保证标号小的重量大)


#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<queue>#include<cmath>#include<algorithm>#define MAX 1000#define inf 0x3f3f3f3fusing namespace std;int n,a[300][300],du[3000];int tmp[3000],sum,now[300];void Topo(){    int i;    priority_queue<int,vector<int>,less<int> >q;    while(!q.empty())        q.pop();    for(i=1;i<=n;i++)        if(!du[i]){            q.push(i);        }    while(!q.empty()){        int v=q.top();        q.pop();        tmp[sum++]=v;        for(i=1;i<=n;i++){            if(a[i][v]){                du[i]--;                if(!du[i]){                    q.push(i);                }            }        }    }}int main(){    int  m,i,j,k,cla;    scanf("%d",&cla);    while(cla--){        scanf("%d%d",&n,&m);        sum=0;        for(i=0;i<=n;i++){            du[i]=0;            for(j=0;j<=n;j++){                a[i][j]=0;            }        }        int x,y;        for(i=0;i<m;i++){            scanf("%d%d",&x,&y);            if(!a[x][y]){///**********////                a[x][y]=1;                du[x]++;            }        }        Topo();        if(sum!=n){            printf("-1\n");        }        else{            for(i=0;i<sum;i++)               now[tmp[i] ]=n-i;            for(i=1;i<=sum;i++)                printf(i==sum?"%d\n":"%d ",now[i]);        }    }    return 0;}


1 0
原创粉丝点击