poj3687 拓扑排序

Problem 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

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

 

source:poj3687

题意有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... ”,意思是标签小的要尽可能贴在重量小的球上。

AC代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<stdio.h>
#include <cstdlib>
#include<malloc.h>
#include<algorithm>
#include<functional>
#include<utility>
#include<cmath>
#include<Map>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<list>
int Max(int a, int b) { return a > b ? a : b; }
int Min(int a, int b) { return a > b ? b : a; }
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
typedef unsigned long long ull;
const double INF = 99999999999.0;
const double eps = 1e-6;
#define siz 205
vector<int>vec[siz];
bool gra[siz][siz];
int out[siz];
int n,m,cnt,ans[siz];
int num;
bool flag;
void read()
{
    int a,b;
    FOR(i,1,n)vec[i].clear();
    memset(out,0,sizeof(out));memset(gra,false,sizeof(gra));
    scanf("%d %d",&n,&m);
    FOR(i,1,m){
        scanf("%d %d",&a,&b);
        if(a==b||gra[b][a])flag=false;//不能直接返回false,因为后面的数据还没读取完
        gra[a][b]=true;
        vec[b].push_back(a);
        out[a]++;
    }
}
bool topo()
{
    priority_queue<int>q;
    for(int i=1;i<=n;i++){
        if(out[i]==0)q.push(i);
    }
    int nn=0;
    while(!q.empty()){
        int tmp=q.top();
        q.pop();
        ans[tmp]=num--;
        nn++;
        for(int i=0;i<vec[tmp].size();i++){
            int v=vec[tmp][i];
            if(--out[v]==0)q.push(v);
        }
    }
    if(nn!=n)return false;
    else return true;
}
int main()
{
    //freopen("E:\\对拍\\data.in","r",stdin);
    int cas;
    scanf("%d",&cas);
    while(cas--){
        flag=true;
        read();
        if(!flag){printf("-1\n");continue;}
        cnt=0;num=n;
        if(!topo()){printf("-1\n");continue;}
        FOR(i,1,n-1)printf("%d ",ans[i]);printf("%d\n",ans[n]);
    }
    return 0;
}







/*
10 6
2 1 5 3 6 3 7 9 10 8 8 9
*/

  

 

posted @ 2018-03-15 13:58  WindFreedom  阅读(140)  评论(0)    收藏  举报