POJ 3687 (拓扑排序 + 优先队列 + 吐血.jpg) 反向思考

Labeling Balls

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15874   Accepted: 4660

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 witha 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

题意:给定n个重量为1到n的球,要求你给其编号(1~n)。 然后给定m对a 和 b, 表示编号为a的球 必须比 编号为b 轻。输出依次输出编号为1~n 的球的重量。如果有多种方案,编号小的球要尽量轻(依次满足)。

如     (有向a 到 b 代表 a 比 b 轻)

重量为                                             0 1 2 3 4 5 6 7 8 9​​​​​​​   (对应下方编号的重量)           0 1 2 3 4 5 6 7 8 9​​​​​​​​​​​​​​

其正确 由轻到重的序列编号为:    6 4 1 3 9 2 5 7 8 0 , 这样才是满足题意的。 如 3 6 9 4 5 1 7 8 2 0, 你会发现编号1的重量没有做到尽量小。

自己怎么可能想的出来思路。。。只想到拓扑。。。Orz。。。看 大牛博客详细得一批

思路:采用 逆向拓扑排序。 首先出度为0的点 入优先队列(递减), 取队首,然后去相应的边,如果去边后出度 为0 在加入优先队列。重复此操作至  队列为空 。    也可以反向建边。  a 到 b 代表  a 比 b 重

AC代码:

#include<cstdio>
#include<queue>
#include<set>
#include<cstring>
using namespace std;

const int maxn = 210;
struct node{
    int v,next;
    node(){}
    node(int v,int next):v(v),next(next) {}
}edge[maxn*maxn];
int in[maxn];
int head[maxn];
int tops[maxn],pto = 0;
int n,m,t;

bool topsort(){
    memset(tops,0,sizeof(tops));
    memset(in,0,sizeof(in));
    pto = 0;
    for(int i = 1;i <= n;i ++)
        for(int j = head[i];j != -1;j = edge[j].next)
            in[edge[j].v] ++;
    priority_queue<int>Q;
    for(int i = 1;i <= n;i ++)
        if(!in[i]) Q.push(i);
    while(!Q.empty()){
        int now = Q.top();
        Q.pop(); tops[pto++] = now;
        for(int i = head[now];i != -1;i = edge[i].next){
            in[edge[i].v] --;
            if(in[edge[i].v] == 0) Q.push(edge[i].v);
        }
    }
    if(pto != n) return 0;
    return 1;
}

int main()
{
    scanf("%d",&t);
    while(t --){
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int a,b; int cnt = 0;
        for(int i = 0;i < m ;i ++){
            scanf("%d%d",&a,&b);
            edge[cnt] = node(a,head[b]);
            head[b] = cnt ++;
        }
        if(!topsort()){
            printf("-1\n");
            continue;
        }
        int ans[maxn] = {0};
        for(int i = 1;i <= n;i ++)      ///tops[] 数组,轻的在后面,重的在前面
            ans[tops[i-1]] = n - i + 1;
        for(int i = 1;i <= n;i ++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/81390054