Labeling Balls

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 11893 Accepted: 3408

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

有一点不太明白,为什么从小到大就WA

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#define WW freopen("a1.txt","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; int n,m; int Du[250]; int a[250]; bool Map[250][250]; bool vis[250];
int Topo()拓扑排序
{
int wight=n; memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++)
{
int ans=0;
for(int j=n; j>0; j--)
{
if(!vis[j]&&Du[j]==0)
{
ans=j;
break;
}
}
if(!ans)
{
break;
}
vis[ans]=true;
a[ans]=wight--;
for(int j=1; j<=n; j++)
{
if(!vis[j]&&Map[j][ans])
{
Du[j]--;
}
}
}
return wight;
} int main()
{
int T;
int u,v;
bool flag;
scanf("%d",&T); while(T--)
{
scanf("%d %d",&n,&m);
flag=true;
memset(Map,false,sizeof(Map));
memset(Du,0,sizeof(Du));
for(int i=1; i<=m; i++)
{
scanf("%d %d",&u,&v);
if(!Map[u][v])
{
Map[u][v]=true;
Du[u]++;
}
}
if(flag)
{
if(Topo()==0)
{
for(int i=1; i<=n; i++)
{
if(i!=1)
{
printf(" ");
}
printf("%d",a[i]);
}
printf("\n");
}
else
{
printf("-1\n");
}
} }
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏的更多相关文章

  1. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  2. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...

  4. Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  5. 菊花加载第三方--MBprogressHUD 分类: ios技术 2015-02-05 19:21 120人阅读 评论(0) 收藏

    上次说到了网络请求AFN,那么我们在网络请求的时候,等待期间,为了让用户不认为是卡死或程序出错,一般都会放一个菊花加载,系统有一个菊花加载类叫UIProgressHUD.但是我今天要说的是一个替代它的 ...

  6. 浅谈IOS8之size class 分类: ios技术 2015-02-05 19:06 62人阅读 评论(0) 收藏

    文章目录 1. 简介 2. 实验 3. 实战 3.1. 修改 Constraints 3.2. 安装和卸载 Constraints 3.3. 安装和卸载 View 3.4. 其他 4. 后话 以前和安 ...

  7. Transact-SQL 常用函数 分类: SQL Server 2015-02-03 09:47 284人阅读 评论(0) 收藏

    (1)DECLARE 两种用法: 1>: DECLARE @usid VARCHAR(50),@usna NVARCHAR(100),@grna NVARCHAR(100); 2>: DE ...

  8. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  9. Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏

    C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. SQL Server 2008中的Service SID 介绍

    [介绍] 我们打开SQL Server 2008 Management Studio, 会发现有如下几个登录: NT SERVICE\ClusSvc, NT SERVICE\MSSQL$KATMAI和 ...

  2. 树形DP(01组合背包The Ghost Blows Light HDU4276)

    题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...

  3. jsp编写页面时常见错误提示

    jsp编写页面时常见错误提示 404-->未部署web应用 500-->代码有问题 无法显示网页-->未启动tomcat webRoot-->URL输入有误 web-inf-- ...

  4. 自定义view(使用EditTetx实现记事本特效)

    先看一下效果图: 思路: 创建一个类,继承自EditText,在onDraw方法中绘制我们的下划线,通过屏幕的高度和每行控件的高度的比值得出屏幕中应该绘制多少行下划线,再来实现一些自定义的属性,设置控 ...

  5. android复习第二天------布局

    1,在4,0版本前一共有五种布局,且都是ViewGroup的子类分别是 RelativeLayout(相对),AbsoluteLayout(绝对),LinearLayout(线性),FrameLayo ...

  6. sql 根据一个表更新 另一个表的例子及可能遇到的问题

    例子: update a set a.name=b.name1 from a,b where a.id=b.id 例子延伸:更新的时候会把字符串 转为科学计数法  怎么办? 答:用 cast 转换一下 ...

  7. mongodb的js操作

    在包含url的test库中运行test.js mongo url:port/test test.js

  8. 转:VS2008 vs2010中JQUERY智能提醒

    第一步: 安装VS 2008 SP1 VS 2008 SP1 在Visual Studio中加了更丰富的JavaScript intellisense支持,对很大部分的JavaScript库加了代码完 ...

  9. React的一个简单示例

    首发:个人博客,更新&纠错&回复 React的核心是定义组件类,组件有三个要素:状态.行为.界面. 1.渲染状态到界面:状态由组件对象的state属性持有,从状态到界面的渲染工作由组件 ...

  10. :has(selector) 匹配含有选择器所匹配的元素的元素

    描述: 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码: <div><p>Hello</p></div> <div&g ...