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;
    } 
  • 相关阅读:
    基础面试
    三级联动
    数据库分裤分表操作
    缓存的三种方式
    php部分--session的三种用法
    css
    数组
    生成中国地区随机IP
    xml的读取
    React入门
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/11672889.html
Copyright © 2011-2022 走看看