zoukankan      html  css  js  c++  java
  • poj 3687

    Labeling Balls
     
    http://poj.org/problem?id=3687
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions:17110   Accepted: 5002

    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



    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    
    const int maxn=200+3;
    const int maxm=40000+3;
    int in[maxn];
    int t, n,m,a,b;
    
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(in,0,sizeof(in));
            vector<int>edge[maxn];
            while(m--){//反向拓扑排序 
                scanf("%d%d",&a,&b);
                edge[b].push_back(a);
                in[a]++; 
            }
            priority_queue<int,vector<int>,less<int> >q;
            for(int i=1;i<=n;i++){
                if(in[i]==0){
                    q.push(i);
                }
            }
            vector<int>ans;
            while(!q.empty()){
                int u=q.top();
                q.pop();
                ans.push_back(u);
                for(int i=0;i<edge[u].size();i++){
                    int v=edge[u][i];
                    in[v]--;
                    if(in[v]==0){
                        q.push(v);
                    }
                }
            }
            
            if(ans.size()==n){
                int as[maxn];
                int cnt=0;
                for(int i=n-1;i>=0;i--){
                    as[cnt++]=ans[i];
                }
                int bs[maxn];
                for(int i=1;i<=n;i++)
                {
                    for(int j=0;j<n;j++){
                        if(i==as[j]){
                            bs[i]=j+1;
                        }
                    }
                }
                printf("%d",bs[1]);
                for(int i=2;i<=n;i++){
                    printf(" %d",bs[i]);
                }
                printf("
    ");
            }else{
                printf("-1
    ");
            }
        }
        return 0;
    } 
  • 相关阅读:
    [北大机试C]:走迷宫(BFS)
    [北大机试B][OpenJ_Bailian-2965]:玛雅历(模拟)
    [北大机试A]:有趣的跳跃(模拟)
    [2019北大机试D]:最大上升子序列和(DP)[计蒜客-T1221/HDU1087]
    [牛客练习赛53] A.超越学姐爱字符串 [dp]
    [牛客][北大考研复试]I Wanna Go Home[dijkstra]
    SP1716 GSS3
    牛客小白月赛13_A_小A的签到题
    自习室管理系统,基于B/S模式下的JAVA系统
    基于SSH的聊天室
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/11672889.html
Copyright © 2011-2022 走看看