• 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
  • 相关阅读:
    初始Hadoop
    vue3封装分页组件
    集成仿真软件 PLEXOS 9.0 授权永久完美
    JAVA鲜花订购网微服务计算机毕业设计Mybatis+系统+数据库+调试部署
    Cy3 diacid/bis-carboxylic acid,Cy3-COOH/carboxylic acid/羧基羧酸(bis)
    汽车行业使用LDO直接连接电池的应用
    Python每日一练(牛客新题库)——第24天:内置函数
    使用BigDecimal的坑
    数据结构1 -- leetcode练习
    Missing-Semester : The Shell 笔记
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/126138144