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

    Labeling Balls

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 9641
    Accepted: 2636

    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

                           [Submit]   [Go Back]   [Status]   [Discuss]

    ::做这道题,原先的思路就是从前面往后扫,入度为0的就赋予未被使用的最小值,可是这样无法得到字典序最小。

    看了题解才知道,这道题要逆向建图,且从后往前扫,入度为0的赋予当前未使用的最大值。

       1: #include <iostream>
       2: #include <cstdio>
       3: #include <cstring>
       4: #include <algorithm>
       5: using namespace std;
       6: typedef long long ll;
       7: const int maxn=40010;
       8: int head[maxn],in[220],ans[220],vis[220];
       9: int cnt,n,m;
      10:  
      11: struct node
      12: {
      13:     int u,v,p;
      14: }e[maxn];
      15:  
      16: int topo()
      17: {
      18:     memset(vis,0,sizeof(vis));//标记数组
      19:     int k=n;
      20:     int i,j,u;
      21:     for(i=1; i<=n; i++)
      22:     {
      23:         for(u=n; u>0; u--)
      24:         {
      25:             if(!vis[u]&&in[u]==0)
      26:                break;
      27:         }
      28:         if(u<=0) return 0;
      29:         vis[u]=1;
      30:         ans[u]=k--;
      31:         for(j=head[u]; j!=-1; j=e[j].p)
      32:         {
      33:             in[e[j].v]--;
      34:         }
      35:     }
      36:     return 1;
      37: }
      38:  
      39: int main()
      40: {
      41:     int T;
      42:     scanf("%d",&T);
      43:     while(T--)
      44:     {
      45:         int ok=1,a,b;
      46:         cnt=0;
      47:         scanf("%d%d",&n,&m);
      48:         memset(head,-1,sizeof(head));
      49:         memset(in,0,sizeof(in));
      50:         while(m--)
      51:         {
      52:             scanf("%d%d",&a,&b);
      53:             e[cnt].u=b;
      54:             e[cnt].v=a;
      55:             e[cnt].p=head[b];
      56:             head[b]=cnt++;
      57:             in[a]++;
      58:             if(a==b) ok=0;
      59:         }
      60:         if(ok==0||topo()==0)
      61:            printf("-1
    ");
      62:         else
      63:         {
      64:             for(int i=1; i<=n; i++)
      65:                 printf("%d ",ans[i]);
      66:             printf("
    ");
      67:         }
      68:         //printf("
    ");
      69:     }
      70:     return 0;
      71: }
  • 相关阅读:
    函数调用时函数栈状态分析
    [Project Euler] Problem 51
    [Project Euler] Problem 52
    [Project Euler] Problem 49
    自己的路
    COBOL FILE STATUS(return code)
    SVN客户端使用手册
    堆栈在VBA中的实现
    一个月以来的实习总结
    OpenCVPython系列之图像腐蚀与膨胀
  • 原文地址:https://www.cnblogs.com/zyx1314/p/3633330.html
Copyright © 2011-2022 走看看