POJ 3687 Labeling Balls

来源:互联网 发布:办公软件培训视频 编辑:程序博客网 时间:2024/04/28 00:39
Labeling Balls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13433 Accepted: 3879

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


#include<iostream>    #include<cstdio>    #include<cstring>    using namespace std;    int mp[211][211];    int wei[211];     int chu[211];    int main()    {        int t;        cin>>t;        while(t--)        {            int n,m;            cin>>n>>m;            int i,j;            memset(mp,0,sizeof(mp));            memset(chu,0,sizeof(chu));             memset(wei,0,sizeof(wei));            for(i=1;i<=m;i++)            {                int a,b;                cin>>a>>b;                if(mp[a][b]==0)                {                    mp[a][b]=1;                    chu[a]++;                }            }            int t=n;            while(1)            {                for(i=n;i>=1;i--) if(wei[i]==0&&chu[i]==0) break;                if(i==0) break;                wei[i]=t--;                for(j=1;j<=n;j++) if(mp[j][i]==1) chu[j]--;            }            if(t==0)            {                for(i=1;i<=n;i++)                {                    if(i>1) cout<<" ";                    cout<<wei[i];                }                cout<<endl;            }            else cout<<"-1"<<endl;        }        return 0;    }


0 0