POJ3687.Labeling Balls 拓扑排序

Labeling Balls

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 13201
Accepted: 3811

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, bN) 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
 

题目链接:

http://poj.org/problem?id=3687
 

题意:,有N个质量不同的球,分别为1~N.每个测试数据a,b表示两个球的质量a<b.输出编号1~N的球的质量.如果存在多个方案,则输出编号为1的球质量最轻的方案,如果还是有多个方案,则输出编号为2得球质量最轻的方案,以此类推.

 

思路:输出的是质量,而不是编号.所以,输出的是编号所在的位置.这道题目用的是逆拓扑排序.先确定哪些球的入出为0,说明他们的质量是最大的一批,选取其中编号最大的球最为质量最大的球,重复此步骤.最后输出1~N号球的质量.

例:

不考虑输出要求。进行拓扑排序的结果(序号)

  • 1   4   5   3   2
  • 1   5   3   4   2
  • 1   5   4   3   2
  • 5   1   3   4   2
  • 5   1   4   3   2
  • 5   3   1   4   2

这所有的情况,第2种符合题目要求。那么输出为1 5 3 4 2(这里并不是序号,而是质量,即每个序号所在的位置)。


 代码:
#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include<algorithm> 
using namespace std; 
int n,m,num; 
int sign[210]; 
int in[210],out[210]; 
int indegree[210][210],outdegree[210][210]; 
struct node 
{ 
    int p,m; 
} ans[210]; 
int cmp(node a,node b) 
{ 
    return a.p<b.p; 
} 
int Topsort() 
{ 
    int i,j,t; 
    num=0; 
    for(t=0; t<n; t++) 
    { 
        for(i=n; i>=1; i--) 
        { 
            if(sign[i]==1&&out[i]==0) 
                break; 
        } 
        if(i<=0) return -1; 
        else 
        { 
            sign[i]=0; 
            ans[num].p=i; 
            ans[num].m=n-(num++); 
            for(j=0; j<in[i]; j++) 
                out[indegree[i][j]]--; 
        } 
    } 
    return 1; 
} 
int main() 
{ 
    int i,T,u,v; 
    cin>>T; 
    while(T--) 
    { 
        cin>>n>>m; 
        memset(in,0,sizeof(in)); 
        memset(out,0,sizeof(out)); 
        memset(indegree,0,sizeof(indegree)); 
        memset(outdegree,0,sizeof(outdegree)); 
        for(i=0; i<m; i++) 
        { 
            scanf("%d%d",&u,&v); 
            if(outdegree[u][out[u]]==0)outdegree[u][out[u]++]=v; 
            if(indegree[v][in[v]]==0)indegree[v][in[v]++]=u; 
        } 
        for(i=1; i<=n; i++) sign[i]=1; 
        if(Topsort()<0) cout<<"-1"<<endl; 
        else 
        { 
            sort(ans,ans+n,cmp); 
            for(i=0; i<n-1; i++) 
                cout<<ans[i].m<<" "; 
            cout<<ans[i].m<<endl; 
        } 
    } 
    return 0; 
} 
/* 
2

5 4 
1 4 
4 2 
5 3 
3 2

5 3 
1 4 
4 2 
3 5

ans= 
1 5 3 4 2 
1 3 4 2 5 
*/ 
View Code

I am a slow walker,but I never walk backwards.
原文地址:https://www.cnblogs.com/GeekZRF/p/5395135.html