• 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
  • 相关阅读:
    高阶 DS --- AVL 树
    Vue+SpringBoot打造学生综合素质评价系统
    通过docker启动Jenkins容器报错
    成功的程序化交易者需要具备什么技能?
    父组件可以监听到子组件的生命周期吗?
    每日刷题记录 (三)
    商户订单信息语音通知功能如何实现?
    第22章_数据库的设计规范
    Chrome扩展的核心:manifest 文件(上)
    Java框架-MyBatis 详细介绍(crud+缓存+联表+缓存+日志...)
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/126138144