zoukankan      html  css  js  c++  java
  • Labeling Balls(拓扑排序wa)

    Labeling Balls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12466   Accepted: 3576

    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
    题解:反向建图,却是wa。。。先放着吧
    wa代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 #define mem(x,y) memset(x,y,sizeof(x)) 
     5 using namespace std;
     6 const int MAXN=210;
     7 const int MAXM=40010<<1;
     8 int head[MAXM];
     9 int que[MAXN],ans[MAXN];
    10 struct Edge{
    11     int from,to,next;
    12 }; 
    13 Edge edg[MAXM];
    14 int edgnum;
    15 int N;
    16 void add(int u,int v){
    17     Edge E={u,v,head[u]};
    18     edg[edgnum]=E;
    19     head[u]=edgnum++;
    20 } 
    21 void topu(){
    22     priority_queue<int>dl;
    23     int top=1;
    24     for(int i=1;i<=N;i++)
    25         if(!que[i])dl.push(i);
    26     while(!dl.empty()){
    27         int k=dl.top();
    28         dl.pop();
    29         ans[top++]=k;
    30         que[k]=-1;
    31         for(int i=head[k];i!=-1;i=edg[i].next){
    32             int v=edg[i].to;
    33             que[v]--;
    34             if(!que[v])dl.push(v);
    35         }
    36     }
    37     if(top!=N+1)puts("-1");
    38     else{
    39     for(int i=N;i>=1;i--){
    40         if(i!=N)printf(" ");
    41         printf("%d",ans[i]);
    42     }
    43     puts("");
    44     }
    45 }
    46 int main(){
    47     int T,M,a,b;
    48     scanf("%d",&T);
    49     while(T--){
    50         scanf("%d%d",&N,&M);
    51         for(int i=1;i<=N;i++)ans[i]=i;
    52         mem(que,0);
    53         mem(head,-1);
    54         edgnum=0;
    55         while(M--){
    56             scanf("%d%d",&a,&b);
    57             add(b,a);//
    58             que[a]++;//反向建图 
    59         }
    60         topu();
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    如何设计一个秒杀系统
    Leetcode题目437:路径总和III(递归-简单)
    Leetcode题目461:汉明距离(位运算-简单)
    Leetcode题目617:合并二叉树(递归-简单)
    分布式锁
    分布式搜索引擎
    数据库
    Java知识体系思维导图
    wav文件头详解,看懂wav文件
    推荐一个最近在学习的AI算法工程师手册,侵删
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4822313.html
Copyright © 2011-2022 走看看