poj3687

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

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

题意:
 

题意:

1~n编号的盒子,放1~n重量的球,给出m对盒子装的小球之间大小的关系,输出1~n编号盒子重量,要求编号越小的盒子,放的重量越小。

分析:

首先,想到有环肯定不行。然后,拓扑思想,每次找入读为0的点,尽量往编号小的盒子放,但这样会错误

1

6 4

1 6

3 1

2 4

4 5

正确输出:2 3 1 4 5 6

按照上面的拓扑排序方法盒子摆放的顺序(其实这样保证了字典序最小):2 3 1 4 5 6,转换成重量输出结果是3 1 2 4 5 6. 

但是正确结果的拓扑排序盒子摆放的顺序:3 1 2 4 5 6,转换成重量是:2 3 1 4 5 6.

过程:

上述例子输入得到的顺序:3 1 6和2 4 5 ,按照上述方法,肯定选择顺序2 3 1 但是3 1 2为正确。

      但是如果我们每次都是优先选出度为0的最大序号球放在当前可能的最大盒子里,那么我们能保证1号盒子是最轻的.因为当我们想放1号盒子的时候,还剩下没放的盒子比如都是比1号盒子要轻的(就算它们编号比1大).如果某个盒子与1号球盒子没关系,那么它肯定已经被放置在了一个大的盒子里
代码:
 



#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>

using namespace std;

const int M = 250 ;
int t, n, m;
int outint[M];
int out[M];
int in[M];
int cut;
vector<int>amap[M];
int flag;

bool toposort()
{
    cut = 0;
    priority_queue<int>que;
    //priority_queue<int,vector<int>,greater<int> > que;
    for(int i=1; i<=n; i++)
        if( !in[i] )
        que.push(i);
    while( !que.empty() )
    {
        int u = que.top();
        que.pop();
        
        outint[ cut++]=u;
        for( int i=0; i<amap[u].size(); i++ )
        {
            int v = amap[u][i];
            if( --in[v]==0 )
                que.push(v);
        }
    }
    if( cut<n )
        return false;
    else
        return true;
}

int main()
{
    scanf( "%d", &t );
    while( t-- )
    {
        memset( in, 0, sizeof(in) );
        scanf( "%d%d", &n, &m );
        for( int i=1; i<=n; i++ )
            amap[i].clear();
        for( int i=1; i<=m; i++ )
        {
            int a, b;
            scanf( "%d%d", &a, &b );
            amap[b].push_back(a);
            in[a]++;
        }
        if( !toposort() )
            printf("-1\n");
        else
        {
            /*for(int i=0;i<n;i++)
            {
                cout<<outint[i]<<" ";
            }*/
            for( int i=0; i<n; i++ )
                out[ outint[i] ] = n-i;
            for(int i=1; i<=n; i++)
            {
                if(i<n)
                    printf( "%d ", out[i] );
                else
                    printf( "%d\n", out[i] );
            }
        }
    }

    return 0;
}

详解:http://www.cppblog.com/Davidlrzh/articles/115620.html
 

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/87892828