zoukankan      html  css  js  c++  java
  • Labeling Balls--poj3687

    Labeling Balls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12273   Accepted: 3516

    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




    这是一个拓扑题,还不是普通的拓扑,这必须要反向建图+逆向输出,并且注意,这个题让输出的是各个人的位置!!!



     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 int map[250][250];
     7 int degree[250];
     8 void topo(int n)
     9 {
    10     int i,j,mark,que[250];
    11     for(i=n;i>=0;i--)
    12     {
    13         int x=0;//标记看是否满足要求
    14         for(j=n;j>=0;j--)
    15         {
    16             if(degree[j]==0)
    17             {
    18                 x=1;
    19                 mark=j;
    20                 break;
    21             }
    22         }
    23         if(x==0)
    24             break;
    25         que[mark]=i;
    26         degree[mark]=-1;
    27         for(j=1;j<=n;j++)
    28         {
    29             if(map[mark][j])
    30                 degree[j]--;
    31         }
    32     }
    33     if(i!=-1)
    34         printf("-1
    ");
    35     else
    36     {
    37         printf("%d",que[1]);
    38         for(i=2;i<=n;i++)
    39         printf(" %d",que[i]);
    40         printf("
    ");
    41 
    42     }
    43 }
    44 
    45 int main()
    46 {
    47     int N,i,m,n,a,b;
    48     scanf("%d",&N);
    49     while(N--)
    50     {
    51         memset(map,0,sizeof(map));
    52         memset(degree,0,sizeof(degree));
    53         scanf("%d%d",&n,&m);
    54         for(i=0;i<m;i++)
    55         {
    56             scanf("%d%d",&a,&b);
    57             if(!map[b][a])//避免重复录入
    58             {
    59                 map[b][a]=1;
    60                 degree[a]++;//反向建图
    61             }
    62         }
    63         topo(n);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    mysql常用命令(2)
    mysql常用命令(1)
    svn使用方法介绍(1)
    java设计模式
    maven常见错误
    Powershell上线MSF
    Alibaba Nacos 认证绕过
    好视通-视频会议存在弱口令&任意文件下载漏洞
    金山 V8 终端安全系统存在默认口令
    (CVE-2021-3297)Zyxel NBG2105身份验证绕过漏洞
  • 原文地址:https://www.cnblogs.com/Eric-keke/p/4735608.html
Copyright © 2011-2022 走看看