POJ3687Labeling Balls

Labeling Balls

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14278   Accepted: 4162

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

题意:

这道题每次输入a,b的时候表示的是编号为a的球比编号为b的球轻,最后输出的是从编号 1

    到编号 n每个小球的重量,如果存在多组解,输出使最小重量尽量排在前边的那组解,亦即 所有解中 1到 n号球的重量的字典序最小


/*
解题思路:
    将输入的n行建立有向图,如果该图产生回路,那么输出-1,如果不产生回路,将入度为0的点放进优先队列中,
    优先队列从大到小排序,每个队列按顺序出队,当出队的数与其他点有边存在,就在相应该点的入度减一,
    然后在判断是否入度为0,如果为0再次入队。本题一个比较容易出错的就是判重,如果输入两个相同的a和b,
    那么如果没有判重将会输出0,判重就在输入边时判断该边是否已经存在,如果存在该边的入度就不在自加。
    两组比较好的测试案例:
    第二个测试案例有5个,但是有2个一样的,所以按 4 个算
2

5 4
5 1
4 2
1 3
2 3

10 5
4 1
8 1
7 8
4 1
2 8

ans:
2 4 5 3 1        逆向建图
5 1 6 2 7 8 3 4 9 10  没有判重边的话就输出 -1
典型的拓扑排序满足不了题目小号排前的要求,可以采用反向拓扑排序,加优先队列完成。
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>  

using namespace std; 

int g[210][210];
int degree[210];//入度
int value[210];
priority_queue<int> q;//定义优先队列

//得到入度为0的点
int toposort(int n)
{
    int j=n;
    for(int i=1;i<=n;i++)
    {
        if(degree[i]==0)
        {
            q.push(i);
        }
    }
    if(q.empty())
        return 0;
    while(!q.empty())
    {
        int t = q.top();
        q.pop();
        value[t]=j;
        j--;
        for(int i=1;i<=n;i++)
        {
            if(g[i][t]!=0)
            {
                g[i][t]=0;
                degree[i]--;
                if(degree[i]==0)
                    q.push(i);
            }
        }
    }
    if(j!=0)
        return 0;
    return 1;
} 

int main()
{
    int T;
    int n,m;
    int a,b;
    scanf("%d",&T);
    while(T--)
    {
        memset(g,0,sizeof(g));
        memset(degree,0,sizeof(degree));
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            if(g[a][b]>0)        //判重,如果输入一样的那么只算一个
                degree[a]--;
            g[a][b]=1;          //a到b的边,起点a,终点b的边
            degree[a]++;        //反向建图
        }
        int x=toposort(n);
        if(x==0)
            printf("-1\n");
        else
        {
            for(int i=1;i<n;i++)
                printf("%d ",value[i]);
            printf("%d\n",value[n]);
        }
    }
    return 0;
}

心若向阳,无言悲伤

时间: 2024-08-24 07:14:36

POJ3687Labeling Balls的相关文章

poj3687Labeling Balls(反向拓扑+优先队列)

题目链接: 啊哈哈,点我点我 题意:就是给了m个限制条件,然后形式是啊a,b就是说编号为a的小球比编号为b的小球青,最后输出字典序最小的序列出来. 思路:如果正常的正向建图的话,有可能得到的不是字典序最小的序列..比如有这样一个例子1->5->4,6->2->3,如果正向建图得到的序列将会是 5 2 1 3 4 6,,而正确的序列式怎么感觉碰到一些题目按字典序都要逆向枚举啊..1 3 4 6 5 2..所以要反向拓扑.. 题目: Labeling Balls Time Limit:

ACM/ICPC 之 拓扑排序-反向(POJ3687)

难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重量最轻,若依旧多组则输出编号次小的Ball重量最轻的方案. 题解:在看懂题意后,再开始做会省很多时间...曲解题意还真是人类的本能啊. 为了完成单向关系排序,需要用到拓扑排序: 为了符合编号最小的重量最轻...的题意,需要用到反向拓扑排序: 输入可能会有重复的关系,因此需要判重: 输出需要按原编号输

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

codeforces 653A A. Bear and Three Balls(水题)

题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each

[2016-02-08][UVA][679][Dropping Balls]

UVA - 679 Dropping Balls Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped f

poj 3687 Labeling Balls(拓补排序)

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: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled with a is lighter

杭电3635-Dragon Balls(并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4582    Accepted Submission(s): 1747 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

Hdoj 5194 DZY Loves Balls 【打表】+【STL】

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 394    Accepted Submission(s): 221 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts