zoukankan      html  css  js  c++  java
  • POJ 3687 Labeling Balls

    Labeling Balls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14617   Accepted: 4278

    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
    -------------------------------------------------------------------------------------------------------------------手动分割线-----------------------------------------------------------------------------------------------------------------
    题目大意:
      
    给你1~N个小球,要求从轻到重排序。
      第一行输入T(T表示测试数据个数)
      第二行输入N,M (N是小球数量,M是想描述的个数)
      下面接着M行,每一行有两个数a,b 表示a比b轻。
      
      最后从轻到重输出小球编号
      无法排序则输出-1
    大致思路:
      
    逆序的拓扑排序。
    代码送上:
     
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 #define N 205
     7 int e[N][N];
     8 int in[N];
     9 int ans[N];
    10 int sum;
    11 bool tuopu(int n)
    12 {
    13     priority_queue<int> q;
    14     for(int i=1;i<=n;i++)
    15         if(in[i]==0)q.push(i);
    16     for(int i=1;i<=n;i++)
    17     {
    18         if(q.empty())return false;
    19         else
    20         {
    21             int x=q.top();q.pop();
    22             ans[x]=sum--;
    23             for(int j=1;j<=n;j++)
    24                 if(e[x][j]&&(--in[j])==0)q.push(j);
    25         }
    26     }
    27     return true;
    28 }
    29 int main()
    30 {
    31     int t;
    32     int n,m;
    33     int a,b;
    34     scanf("%d",&t);
    35     while(t--)
    36     {
    37         scanf("%d%d",&n,&m);
    38         sum=n;
    39         memset(in,0,sizeof(in));
    40         memset(e,0,sizeof(e));
    41         for(int i=0;i<m;i++)
    42         {
    43 
    44             scanf("%d%d",&a,&b);
    45             if(e[b][a]==0)in[a]++;
    46             e[b][a]=1;
    47         }
    48        if(tuopu(n))
    49         {
    50             for(int i=1;i<=n;i++)
    51             {
    52                 printf("%d",ans[i]);
    53                 if(i!=n)printf(" ");
    54             }
    55             printf("
    ");
    56         }
    57         else printf("-1
    ");
    58     }
    59         return 0;
    60 }

  • 相关阅读:
    Java 给Word指定字符串添加批注
    Java 打印Word文档
    Java 在PDF中添加页面跳转按钮
    C# 添加、修改、删除Excel图表数据标签
    C# 添加、读取、删除Excel文档属性
    Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
    【Luogu5348】密码解锁(莫比乌斯反演,数论)
    AtCoder Grand Contest 015
    AtCoder Grand Contest 014
    Codeforces Round #556 (Div. 1)
  • 原文地址:https://www.cnblogs.com/xxjnoi/p/7193194.html
Copyright © 2011-2022 走看看