码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3687

时间:2014-07-23 13:40:06      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   strong   

Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10220   Accepted: 2838

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

POJ Founder Monthly Contest – 2008.08.31, windy7926778

AC代码:

#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int f1;
int r[205];
int ans_r[205];
int in[205];
vector <int> a[205];
void top_sort(){
    int t=n;
    priority_queue <int> s;
    while(!s.empty())
        s.pop();
    f1=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)
            s.push(i);
    while(!s.empty()){
        int tmp=s.top();
        s.pop();
        r[tmp]=t--;         //优先给大编号分配最重的球
        for(int i=0;i<a[tmp].size();i++){
            in[a[tmp][i]]--;
            if(in[a[tmp][i]]==0)
                s.push(a[tmp][i]);
        }
    }
    if(t>0)
        f1=1;
}
int main(){
    int T; cin>>T;
    while(T--){
        memset(in,0,sizeof(in));
        for(int i=0;i<=n;i++)
            if(!a[i].empty()){
                a[i].clear();
            }
        cin>>n>>m;
        for(int i=0;i<m;i++){
            int j,x,y;
            cin>>x>>y;
            for(j=0;j<a[y].size();j++)
                if(a[y][j]==x)
                    break;
            if(j>=a[y].size()){
                a[y].push_back(x);    //重的编号指向轻的编号
                in[x]++;              //轻的编号的入度加1,为了在top_sort的时候优先给重的编号分配重量
            }
        }
        top_sort();
        if(f1==1)
            cout<<"-1"<<endl;
        else {
            for(int i=1;i<=n;i++)
                cout<<r[i]<<' ';
            cout<<endl;
        }
    }
    return 0;
}


poj 3687,布布扣,bubuko.com

poj 3687

标签:des   style   http   color   os   strong   

原文地址:http://blog.csdn.net/my_acm/article/details/38052177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!