POJ3687Labeling Balls 解题思路加感受

Labeling Balls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12767 Accepted: 3672

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, bN) 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


题目大意:首先对N个质量分别为1,2,~N的球进行编号为1,2,~N,然后对某些编号的球的质量会有要求,比如:3  2,这就要求序号为3的球的质量必须小于序号为2的球,在这样的要求下可能会有多种排序方式,但没有涉及的一些球之间也要满足序号小的球的质量要小于序号大的球的质量,比如上面第五个样例中给出的 n=4,m=1,3  2 代表序号为3的球的质量要小于序号为2的球,最终答案时1  3  2  4,而不是3  2  1  4或者 2   4   3  1 等等,,此道题目利用拓扑进行逆序建图会方便不少。 

上代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
using namespace std;
int map[210][210],in[210],out[210],vis[210];
int n,m;
int tp[210];
int topo()
{
    stack<int >s;
    int num,num1,i,j;
    for(i=1,num=0; i<=n; i++)                                // 该for循环最后得到有要求的点的数目
    {
        if(vis[i]==1)
        {
            num++;
        }
    }
    num1=0;
    for(i=1;i<=n;i++)                                             //开始判断有木有环       在这里额wa了n次
    {
        if(vis[i]==1&&in[i]==0)   s.push(i);           
    }
    while(!s.empty())
    {
        int x=s.top();
        s.pop();
        num1++;
        for(i=1;i<=n;i++)
        {
            if(map[x][i])
            {
                in[i]--;
                if(in[i]==0)
                    s.push(i);
            }
        }
    }
    if(num1!=num)                           //有环输出-1
    {
        printf("-1\n");
        return 0;
    }
    num=n;
    memset(tp,0,sizeof(tp));
    while(1)
    {
        for(i=n; i>0; i--)
        {
            if(tp[i]==0&&out[i]==0)                  //依据编号从大到小进行安排
                break;
        }
        if(i==0) break;
        tp[i]=num--;
        for(j=1; j<=n; j++)
        {
            if(map[j][i]==1)
                out[j]--;
        }
    }
    for(i=1; i<=n; i++)
    {
        if(i!=n) printf("%d ",tp[i]);
        else  printf("%d\n",tp[i]);
    }
    return 0;
}
int main()
{
    int T,s,e,i;
    scanf("%d",&T);
    while(T--)
    {
        memset(map,0,sizeof(map));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(vis,0,sizeof(vis));
        scanf("%d %d",&n,&m);
        if(m==0)
        {
            for(i=1; i<=n; i++)
            {
                if(i!=n)
                    printf("%d ",i);
                else printf("%d\n",i);
            }
        }
        else
        {
            for(i=1; i<=m; i++)
            {
                scanf("%d %d",&s,&e);
                if(map[s][e]==0)                                     //记得要判断重边
                {
                    map[s][e]=1;
                    in[e]++;
                    out[s]++;
                    vis[s]=vis[e]=1;                                      //对有要求的点做个记号
                }
            }
            topo();                //here  we  go  topo
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值