zoukankan      html  css  js  c++  java
  • POJ3687——Labeling Balls(反向建图+拓扑排序)

    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:
    No two balls share the same label.
    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和M,N表示有N个小球,编号分别为1—N。M表示下面用M组数据(x,y)。

        数据(x,y)表示小球x比小球y轻。每个小球的重量都不同,且范围是[1,n]。

        输出为 编号1的小球的重量,编号2小球的重量。。。编号N小球的重量(且使编号小的小球尽量轻)。条件矛盾的话输出-1。

    解题思路:

        错误思路:正向建图+拓扑排序+贪心查找最小的编号 在根据编号在拓扑数组中的位置输出。(正向的贪心不能完全保证序号小的节点尽量排在前面。??)

        eg:6→1→3←5←4←2 

            1)查找到6和2 将2加入topo数组

            2)查找到6和4 将4加入topo数组

            3)查找到6和5 将5加入topo数组

            4)将6加入数组 将1加入数组 将3加入数组

            topo数组:2 4 5 6 1 3

            输出为:5 1 6 2 3 4

        正确思路:反向建图+拓扑排序(尽量保证编号大的小球先确定大的重量)

        eg:6→1→3←5←4←2  反向之后:6←1←3→5→4→2

            1)查找到3 将3加入topo数组

            2)查找到5 将5加入topo数组

            3)查找到4 将4加入topo数组

            4)将2加入数组 将1加入数组 将6加入数组

            topo数组:6 1 4 5 3

            输出为: 2 3 6 4 5 1 

    Code:

     1 #include<stdio.h>
     2 #include<string>
     3 #include<iostream>
     4 #include<cstring>
     5 #define MAXN 300
     6 using namespace std;
     7 int topo[MAXN+10],map[MAXN+10][MAXN+10],dis[MAXN+10];
     8 bool vis[MAXN+10];
     9 int main()
    10 {
    11     int T,N,M;
    12     cin>>T;
    13     while (T--)
    14     {
    15         memset(vis,0,sizeof(vis));
    16         memset(dis,0,sizeof(dis));
    17         memset(topo,0,sizeof(topo));
    18         memset(map,0,sizeof(map));
    19         bool ok=1;
    20         cin>>N>>M;
    21         int i,j;
    22         for (i=1; i<=M; i++)
    23         {
    24             int t1,t2;
    25             cin>>t1>>t2;
    26             if (!map[t2][t1]) dis[t1]++;
    27             map[t2][t1]=1;
    28         }
    29         for (i=N; i>=1; i--)
    30         {
    31             for (j=N; j>=1; j--)
    32                 if (!dis[j]&&!vis[j])
    33                 {
    34                     topo[i]=j;
    35                     vis[j]=1;
    36                     break;
    37                 }
    38             if (j==0) ok=0;
    39             for (int k=1; k<=N;k++)
    40                 if (map[j][k])
    41                     dis[k]--;
    42         }
    43         if (ok)
    44         {
    45             for (i=1; i<=N; i++)
    46             {
    47                 //printf("%d",topo[i]);
    48                 for (int j=1; j<=N; j++)
    49                 {
    50                     if (topo[j]==i)
    51                         printf("%d",j);
    52                 }
    53                 if (i==N) printf("
    ");
    54                 else printf(" ");
    55             }
    56         }
    57         else printf("-1
    ");
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    UVALive 4329 Ping pong
    面试题——设计一个程序:运行报错Stack Overflow Error
    MongoDB 主从复制小实验
    我总结的18个非常好用的vim指令
    php性能优化
    error LNK2019: unresolved external symbol / error LNK2001: 无法解析的外部符号
    CopyU!v2 已经收录到腾讯软件管家!
    HDU 1498 50 years, 50 colors (行列匹配+最小顶点覆盖)
    汉语-词语-庸俗:百科
    汉语-成语-见笑大方:百科
  • 原文地址:https://www.cnblogs.com/Enumz/p/3844201.html
Copyright © 2011-2022 走看看