• POJ3687Labeling Balls题解


    POJ3687Labeling Balls题解

    题目

    链接

    http://poj.org/problem?id=3687

    字面描述

    Labeling Balls
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 19619 Accepted: 5624
    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:

    No two balls share the same label.
    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
    Source

    POJ Founder Monthly Contest – 2008.08.31, windy7926778

    思路

    可以先筛选编号大的球进行拓扑排序,注意图要反建

    代码实现

    #include
    #include
    #include
    #include
    using namespace std;
    
    const int maxn=200+10;
    int t,n,m;
    int indegree[maxn],topo[maxn];
    bool map[maxn][maxn];
    inline bool Topo_sort(){ 
    	for(int i=n;i>=1;i--){
    		int temp=-1;
    		for(int j=n;j>=1;j--){
    			if(!indegree[j]){
    				temp=j;
    				break;
    			}
    		} 
    		if(temp==-1)return false;
    		indegree[temp]=-1;
    		topo[temp]=i;
    		for(int j=1;j<=n;j++){
    			if(map[temp][j])indegree[j]--;
    		}
    	} 
    	return true;
    }
    int main(){
    	scanf("%d",&t);
    	while(t--){
    		memset(indegree,0,sizeof(indegree));
    		memset(topo,0,sizeof(topo));
    		memset(map,false,sizeof(map));
    		scanf("%d%d",&n,&m);
    		for(int i=1;i<=m;i++){
    			int x,y;
    			scanf("%d%d",&x,&y);
    			if(!map[y][x]){
    				map[y][x]=true;
    				indegree[x]++;
    			}
    		}
    		if(!Topo_sort())printf("-1\n");
    		else {
    			for(int i=1;i<=n;i++)printf("%d ",topo[i]);
    			printf("\n");
    		}
    	}
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    关于Win10安装keil并且电脑连接jlink踩过的坑
    JAVA JDBC 概述
    数据库读写分离
    QPS、TPS、RT、并发用户数、吞吐量
    以antd为例 React+Typescript 引入第三方UI库
    学习Java编程入门书籍
    使用 FastEndpoints 来垂直切割Web API的控制器方法
    Synopsys Sentaurus TCAD系列教程之-Tcl《1》
    【BOOST C++ 13 并行编程】(3) 线程本地存储
    linux文件存储之inode,硬链接,软链接详解
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/126138144