zoukankan      html  css  js  c++  java
  • HDU 3687 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:

    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 #include<cstdio>
     2 #include<vector>
     3 #include<cstring>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int indegree[205];
     9 int topo[205];
    10 int G[205][205];
    11 int m,n;
    12 
    13 void init()
    14 {
    15     memset(indegree,0,sizeof(indegree));
    16     memset(G,0,sizeof(G));
    17     //memset(topo,0,sizeof(topo));
    18 }
    19 
    20 bool toposort()
    21 {
    22     int t=n;
    23     priority_queue<int,vector<int>,less<int> >q;
    24     for(int i=n;i>=1;i--)
    25         if(indegree[i]==0)
    26         q.push(i);
    27     while(!q.empty())
    28     {
    29         int u=q.top();
    30         q.pop();
    31         topo[u]=t--;
    32         for(int v=1;v<=n;v++)
    33         {
    34             if(G[u][v])
    35             {
    36                 indegree[v]--;
    37                 if(indegree[v]==0)
    38                 q.push(v);
    39             }
    40         }
    41     }
    42     if(t==0)
    43     return true;
    44     return false;
    45 }
    46 
    47 int main()
    48 {
    49     int i,a,b,t;
    50     scanf("%d",&t);
    51     while(t--)
    52     {
    53         init();
    54         scanf("%d%d",&n,&m);
    55         for(i=0;i<m;i++)
    56         {
    57             scanf("%d%d",&a,&b);
    58             if(!G[b][a])
    59             indegree[a]++;
    60             G[b][a]=1;
    61         }
    62         bool flag=toposort();
    63         if(flag)
    64         {
    65             for(i=1;i<=n;i++)
    66             {
    67                 if(i!=n)
    68                 printf("%d ",topo[i]);
    69                 else
    70                 printf("%d
    ",topo[i]);
    71             }
    72         }
    73         else
    74             printf("-1
    ");
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    HTML5 ViewPort 资料收集
    [ASP.NET 技术点滴] Jquery 前端验证
    [转帖]Asp.NET 弹出页面
    [转]ASP.NET 页生命周期概述
    C#时间转整型(时间戳),模仿php strtotime函数的部分功能
    [转]使用微软的官方类库CHSPinYinConv获得汉字拼音
    [转]ASP.NET数据库连接字符串总结
    gradle 生成 pom,引用mybatis-plus源代码到自己的工程中
    困扰的问题终于解决了-docker时区不正确的问题修改记
    mybatis plus3.1.0 热加载mapper
  • 原文地址:https://www.cnblogs.com/homura/p/4730729.html
Copyright © 2011-2022 走看看