zoukankan      html  css  js  c++  java
  • poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12246   Accepted: 3508

    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

    这道题搜了下题解,大神的思路就是清新脱俗;附上自己的理解分析
    There is a blank line before each test case. 这句话可把我坑死了 还以为每组测试数据前都要打印出一行空格,pe了好久
    题意:输入n,m代表有n个数参加排序,接下来m行输入数据x ,y表示x要在y的前边,但是重量小的(即数字小的)要尽量放到前边;
    题解:此题要用反向拓扑,正向是肯定不行的,如数据
    5 3
    1 4
    4 2
    3 5
    正向的话输出结果为 3 1 5 2 4 显然不对,正确结果应该为 1 3 4 2 5 同时采用优先队列,重的最先输出,我们先将重的,赋给
    最先出队的(因为设置的优先队列是大的先出队)这样就可以把轻的尽量留在最后(因为是要逆序,所以留到后边,输出时是在前边)
    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<queue>
    using namespace std;
    int n,m;
    int map[300][300];
    int vis[300],a[300];
    void getmap()
    {
    	int i,j;
    	memset(vis,0,sizeof(vis));
    	memset(map,0,sizeof(map));
    	while(m--)
    	{
    		int a,b;
    		scanf("%d%d",&a,&b);
    		if(!map[b][a])
    		{
    			map[b][a]=1;
    		    vis[a]++;
    		}
    	}
    }
    void tuopu()
    {
    	int i,j,k,g=n;
    	k=0;
    	memset(a,0,sizeof(a));
    	priority_queue<int>q;
    	while(!q.empty())
    	    q.pop();
    	for(i=1;i<=n;i++)
    	    if(!vis[i])
    	        q.push(i);
    	int u;
    	while(!q.empty())
    	{
    		u=q.top();
    		q.pop();
    		a[u]=g--;
    		for(i=1;i<=n;i++)
    		{
    			if(map[u][i])
    			{
    				vis[i]--;
    				if(vis[i]==0)
    				q.push(i);
    			}
    		}
    	}
    	if(g!=0)
    	printf("-1
    ");
    	else
    	{
    		for(i=1;i<n;i++)
    		printf("%d ",a[i]);
    		printf("%d
    ",a[n]);
    	}
    }
    int main()
    {
    	int t,o=0;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%d",&n,&m);
    		getmap();		
    		tuopu();
    	}
    	return 0;
    }
    

      



  • 相关阅读:
    Log4j appender、layout
    EhCache缓存框架的使用
    Log4j rootLogger根配置以及4种日志级别
    开发chrome 插件, background.js中 console log 看不到解决方法
    Windows cmd 长时间不输出新内容 直到按下ctrl + c 取消或者回车的解决办法
    如何查看当前分支从哪个支线创建而来
    C# 获取相对路径的字符串
    解决adobe air sdk打包 apk后自动在包名前面加上air. (有个点)前缀的问题
    sublime text 输入法候选词不跟随光标
    Windows 批处理设置dns ,解决能上qq不能开网页
  • 原文地址:https://www.cnblogs.com/tonghao/p/4730528.html
Copyright © 2011-2022 走看看