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

    http://poj.org/problem?id=3687

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14842   Accepted: 4349

    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
    

    Source

     
    啊啊啊,输出重量,Word_day
    反向建边大根堆维护,先给重的赋值重量。
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 const int M(40233);
     9 const int N(233);
    10 int head[N],sumedge;
    11 struct Edge
    12 {
    13     int v,next;
    14     Edge(int v=0,int next=0):v(v),next(next){}
    15 }edge[M];
    16 inline void ins(int u,int v)
    17 {
    18     edge[++sumedge]=Edge(v,head[u]);
    19     head[u]=sumedge;
    20 }
    21 
    22 priority_queue<int>que;
    23 int rd[N],ans[N],cnt;
    24 inline void init()
    25 {
    26     sumedge=cnt=0;
    27     memset(rd,0,sizeof(rd));
    28     memset(ans,0,sizeof(ans));
    29     memset(head,0,sizeof(head));
    30     memset(edge,0,sizeof(edge));
    31 }
    32 
    33 int AC()
    34 {
    35     int t; scanf("%d",&t);
    36     for(int n,m,if_;t--;init())
    37     {
    38         scanf("%d%d",&n,&m);
    39         for(int u,v;m--;ins(v,u))
    40             scanf("%d%d",&u,&v),rd[u]++;
    41         for(int i=1;i<=n;i++)
    42             if(!rd[i]) que.push(i);
    43         for(int u,v,weight=n;!que.empty();)
    44         {
    45             u=que.top(); que.pop();
    46             ans[u]=weight--;cnt++;
    47             for(int i=head[u];i;i=edge[i].next)
    48             {
    49                 v=edge[i].v;
    50                 if(--rd[v]==0) que.push(v);
    51             }
    52         }
    53         if(cnt!=n) puts("-1");
    54         else
    55         {
    56             for(int i=1;i<n;i++)
    57                 printf("%d ",ans[i]);
    58             printf("%d
    ",ans[cnt]);
    59         }
    60     }
    61     return 0;
    62 }
    63 
    64 int I_want_AC=AC();
    65 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    ORA-01113: file xxxx needs media recovery
    Linux rm删除大批量文件
    SQL Server删除distribution数据库
    SQL Server 2008 R2 Service Pack 3 已经发布
    YourSQLDba开源项目发布到codeplex网站了
    ORACLE回收站机制介绍
    ORACLE临时表空间总结
    数据库服务器改名导致Reporting Service不可用的案例
    ORACLE 11g 数据库体系结构图
    ORACLE 10g 数据库体系结构图
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7423280.html
Copyright © 2011-2022 走看看