poj 3687 拓扑排序

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题意:有n个球重量分别1到n;对每个球进行编号,给出编号之间的重量大小,按重量排序从小到大(让编号小的尽可能重量小)。这题意有点绕,wrong了好几次,以5—>6->1,4->3->2为例,正确的顺序应该是5 6 1 4 3 2,而不是4 3 2 5 6 1;难点在于让编号小的排在前面;
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N=210;
int n,deep[N],topo[N];
bool map[N][N];
int toposort()
{
   int temp[N],c=0,k,m,i,j;
   for(i=0;i<=n;i++)
   {
       temp[i]=deep[i];
   }
   for(i=n;i>0;i--)
   {
       m=0;
       for(j=n;j>0;j--)
       {
           if(temp[j]==0){m++;k=j;break;}
       }
       if(m==0) return 0;
       topo[k]=i;
       temp[k]=-1;
       for(j=1;j<=n;j++)
       {
           if(map[k][j]==1)
           {
               temp[j]--;
           }
       }
   }
   return 1;
}
int main()
{
    int t,m,i,x,y;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        memset(deep,0,sizeof(deep));
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            if(!map[y][x])
            {
                deep[x]++;
            }
            map[y][x]=1;
        }
        int sign=toposort();
        if(!sign)
           printf("-1");
        else
        for(i=1;i<=n;i++)
        {
            if(i==1)
               printf("%d",topo[i]);
             else
                printf(" %d",topo[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-14 06:22:46

poj 3687 拓扑排序的相关文章

poj 4084:拓扑排序

poj 4084:拓扑排序 很好的题目,恶心的算法 描述 给出一个图的结构,输出其拓扑排序序列,要求在同等条件下,编号小的顶点在前. 输入 若干行整数,第一行有2个数,分别为顶点数v和弧数a,接下来有a行,每一行有2个数,分别是该条弧所关联的两个顶点编号. v<=100, a<=500 输出 若干个空格隔开的顶点构成的序列(用小写字母). 样例输入 6 8 1 2 1 3 1 4 3 2 3 5 4 5 6 4 6 5 样例输出 v1 v3 v2 v6 v4 v5 解题方案 显然这是有向图,然

Poj 1094 拓扑排序 水题

Sad..这么水的题WA了无数发,题目要看仔细啊,留下来做个警告把 #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #i

poj 2367 拓扑排序

题目链接:http://poj.org/problem?id=2367 题目大意:就是进行拓扑排序,先给你一个数n,代表1~n,对于每个数有一系列的指向,最后将这些数进行排列出来..就是简单的拓扑排序. 首先拓扑排序应该有两种实现的方法.. 一种是用dfs进行每个节点的搜索,最后进行回溯,这样的话很容易就能明白先找出来的应该是后面的数,而最后找出来的应该是之前的数,因为是回溯出来的嘛..所以可以使用一个栈来进行答案的存储,因为栈的特性就是后压入的先弹出. dfs实现的思想:利用一个数组来存储每个

poj 1094 拓扑排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

POJ 3249 拓扑排序+DP

貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中呢.遇见了这样一个问题.有n个城市,m条小道.然后要从入度为0的点出发,出度为0的点结束,中途经过的城市呢,都是要付费的.负数表示花费.正数表示收益.然后让你求收益最大或者说花费最少的总值. 貌似.BFS和DFS都会超时.不妨一试.附代码: #include<stdio.h>#include<

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 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

POJ 3687:Labeling Balls(优先队列+拓扑排序)

id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Accepted: 2815 Description Windy has N balls of distinct weights from 1 unit toN units. Now he tries to label them with 1 toN in such a way that: No two balls

POJ 3687 Labeling Balls【拓扑排序 优先队列】

题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.csdn.net/scf0920/article/details/28108243 然后用优先队列来实现的参看的这一篇 http://ycool.com/post/u9ahrwg#algo3 1 #include<iostream> 2 #include<cstdio> 3 #includ