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;
    } 
  • 相关阅读:
    通过jquery和js绑定下拉列表 东师理想
    Struts2中EL表达式与<s:property />细微区别(转载) 东师理想
    SQL Prompt v5.3.0.3 安装文件及方法(支持sqlserver2012) 东师理想
    spring 框架概述 东师理想
    iterator标签 东师理想
    memcached的特征 东师理想
    为什么使用memcached 东师理想
    Hibernate教程,IBatis教程,Spring教程链接地址 东师理想
    .Net反射技术
    云时代的GIS软件技术与应用(一)
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/11672889.html
Copyright © 2011-2022 走看看