zoukankan      html  css  js  c++  java
  • poj 3687 Labeling Balls(拓补排序)

    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的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小。(先保证1号球最轻,其次2号……)

        这道题每次输入a,b的时候表示的是编号为a的球比编号为b的球轻,最后输出的是从编号 1到编号 n每个小球的重量,如果存在多组解,输出使最小重量尽量

               排在前边的那组解,亦即 所有解中 1到 n号球的重量的字典序最小。。。。所以说最后重量 1 到 n 的球的标号的字典序最小的做法是不对的。

    分析:拓扑排序,注意根据题的要求,要先保证1号球最轻,如果我们由轻的向重的连边,然后我们依次有小到大每次把重量分给一个入度为0的点,那么在拓扑时我们面对多个入度为0的点,我们不知道该把最轻的分给谁才能以最快的速度找到1号(使1号入度为0),并把当前最轻的分给1号。所以我们要由重的向轻的连边,然后从大到小每次把一个重量分给一个入度为0的点。这样我们就不用急于探求最小号。我们只需要一直给最大号附最大值,尽量不给小号赋值,这样自然而然就会把轻的重量留给小号。

    注意重边。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 const int maxm=210;
     8 int rd[maxm];
     9 bool g[maxm][maxm],vis[maxm];
    10 int answer[maxm];
    11 int t,n,m;
    12 void tupu()
    13 {
    14     if(m==0)
    15     {
    16         for(int i=1; i<n; i++)
    17             cout<<i<<' ';
    18         cout<<n<<endl;
    19         return ;
    20     }
    21     memset(vis,false,sizeof(vis));
    22     memset(answer,0,sizeof(answer));
    23     int k;
    24     for(int i=n; i>0; i--)
    25     {
    26         k=-1;
    27         for(int j=n; j>0; j--)
    28         {
    29             if(!vis[j]&&rd[j]==0) //如果没有点为被访问过且入度为0的点
    30             {
    31                 k=j; //记录下标
    32                 break;
    33             }
    34         }
    35         if(k==-1) //若没有入度为0的点
    36         {
    37             cout<<"-1"<<endl;
    38             return ;
    39         }
    40         vis[k]=true;
    41         answer[k]=i;
    42         for(int j=1; j<=n; j++)
    43         {
    44             if(g[k][j])
    45                 rd[j]--;
    46         }
    47     }
    48     for(int i=1; i<n; i++)
    49         cout<<answer[i]<<" ";
    50     cout<<answer[n]<<endl;
    51     return ;
    52 }
    53 int main()
    54 {
    55     int a,b;
    56     cin>>t;
    57     while(t--)
    58     {
    59         memset(g,false,sizeof(g));
    60         memset(rd,0,sizeof(rd));
    61         cin>>n>>m;
    62         for(int i=1; i<=m; i++)
    63         {
    64             cin>>a>>b;
    65             if(!g[b][a]) 
    66                 rd[a]++; //重量轻的入度加一
    67             g[b][a]=true; 
    68         }
    69         tupu();
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    浅析count(1)、count(*)与count(列名)的执行区别
    浅析尽量不用count(*)来判断是否有数据、判断记录是否存在 exists 和 top 1 要比 count 快
    浅析MySQL中的计算列(Generated Column列)与计算字段的介绍与应用-如何让数据库中某个字段随时间自动更新
    AcWing 837. 连通块中点的数量
    AcWing 836. 合并集合
    AcWing 143. 最大异或对
    AcWing 835. Trie字符串统计
    AcWing 831. KMP字符串
    P2866 [USACO06NOV]Bad Hair Day S
    P1901 发射站
  • 原文地址:https://www.cnblogs.com/cxbky/p/4918600.html
Copyright © 2011-2022 走看看