poj 3687 Labeling Balls 【拓扑排序 输出元素在拓扑序列中的位置】

来源:互联网 发布:手机淘宝5.6.0版本 编辑:程序博客网 时间:2024/06/06 22:46
Labeling Balls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12239 Accepted: 3506

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

54 04 11 14 21 22 14 12 14 13 2

Sample Output

1 2 3 4-1-12 1 3 4

1 3 2 4

大致题意:给你N个点和M个约束条件。问你拓扑序列是否存在,若存在则输出每个元素在 字典序最小的 拓扑序列中的位置,否则输出-1.

具体思路不说了。注意要反向拓扑 + 优先队列 才能找到字典序最小的解。

AC代码:

#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <algorithm>#define MAXN 200+10#define MAXM 40000+10using namespace std;struct Edge{    int from, to, next;};Edge edge[MAXM];int head[MAXN], edgenum;int in[MAXN];int N, M;void init(){    edgenum = 0;    memset(head, -1, sizeof(head));    memset(in, 0, sizeof(in));}void addEdge(int u, int v){    int i;    for(i = head[u]; i != -1; i = edge[i].next)    {        if(edge[i].to == v)            break;    }    if(i == -1)    {        in[v]++;        Edge E = {u, v, head[u]};        edge[edgenum] = E;        head[u] = edgenum++;    }}void getMap(){    int a, b;    while(M--)    {        scanf("%d%d", &a, &b);        addEdge(b, a);    }}int pos[MAXN];//序列中位置int num;void solve(){    priority_queue<int> Q;    stack<int> S;    for(int i = 1; i <= N; i++)        if(in[i] == 0) Q.push(i);    while(!Q.empty())    {        int u = Q.top();        Q.pop();        S.push(u);        for(int i = head[u]; i != -1; i = edge[i].next)        {            int v = edge[i].to;            if(--in[v] == 0)                Q.push(v);        }    }    if(S.size() == N)    {        num = 0;        while(!S.empty())        {            pos[S.top()] = ++num;//存储元素 在拓扑序列中的位置            S.pop();        }        for(int i = 1; i <= N; i++)        {            if(i > 1) printf(" ");            printf("%d", pos[i]);        }        printf("\n");    }    else        printf("-1\n");    while(!S.empty())        S.pop();}int main(){    int t;    scanf("%d", &t);    while(t--)    {        //printf("\n");        scanf("%d%d", &N, &M);        init();        getMap();        solve();    }    return 0;}


0 0
原创粉丝点击