zoukankan      html  css  js  c++  java
  • poj 3687

    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

    今天学习拓扑排序


    其实这就是求出出度为0的点

    逆向的拓扑排序


     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 using namespace std;
     5 const int maxn = 205;
     6 
     7 int tu[maxn][maxn], out[maxn], ans[maxn];
     8 int t, n, m;
     9 int main() {
    10     scanf("%d", &t);
    11     while(t--) {
    12         scanf("%d %d", &n, &m);
    13         memset(ans, 0, sizeof(ans));
    14         memset(tu, 0, sizeof(tu));
    15         memset(out, 0, sizeof(out));
    16         for (int i = 0 ; i < m; i++ ) {
    17             int a, b;
    18             scanf("%d %d", &a, &b);
    19             if (!tu[a][b]) {
    20                 tu[a][b] = 1;
    21                 out[a]++;
    22             }
    23         }
    24         int cnt = n;
    25         priority_queue<int>q;
    26         for (int i = 1 ; i <= n ; i++)
    27             if (!out[i]) q.push(i);
    28         while(!q.empty()) {
    29             int temp = q.top();
    30             q.pop();
    31             ans[temp] = cnt--;
    32             for (int i = 1 ; i <= n ; i++) {
    33                 if (tu[i][temp]) {
    34                     out[i]--;
    35                     if (!out[i]) q.push(i);
    36                 }
    37             }
    38         }
    39         if (cnt > 0) printf("-1
    ");
    40         else {
    41             for (int i = 1 ; i <= n ; i++)
    42                 printf("%d ", ans[i]);
    43             printf("
    ");
    44         }
    45     }
    46     return 0;
    47 }



  • 相关阅读:
    C++11: reference_wrapper
    mac 查看目前哪些进程占用哪些端口
    Intellij IDEA 10.5 语言设置
    linux中的strip命令简介------给文件脱衣服
    HashMap的key可以是可变的对象吗???
    java BIO/NIO/AIO 学习
    java 反射
    Java线程同步
    maven modules
    设计模式在cocos2d-x中的使用--简单工厂模式(Simple Factory)
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9089385.html
Copyright © 2011-2022 走看看