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. HTML语言的一些元素(三)

    本章节主要介绍:<div>和<span> 可以通过 <div> 和 <span> 将 HTML 元素组合起来. HTML <div> 元素是 ...

  2. mysql 查看是否存在某一张表

    判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 或者 SHOW TABLE ...

  3. how to use automapper in c#, from cf~

    [DataContract] public class GroupDto { [DataMember] public int id { get; set; } [DataMember] public ...

  4. JSP 中 forward 和 redirect 的区别_2014.12.31

    重定向,只能访问工程下(WebRoot文件夹)的页面,不能访问到内部(WEB_INF文件夹)的页面 1.从地址栏显示来说:forward浏览器显示路径不变,redirect浏览器显示路径改变forwa ...

  5. Demo12SimpleAdapter

    /Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/data ...

  6. 转 SSIS处理导入数据时, 存在的更新, 不存在的插入

    SSIS处理导入数据时, 存在的更新, 不存在的插入 分类: DTS/SSIS2006-09-10 12:43 18185人阅读 评论(22) 收藏 举报 ssissql servermicrosof ...

  7. Mysql 修改字段长度、修改列名、新增列

    alter table 表名 modify column 字段名 类型; 例如 数据库中user表 name字段是varchar(30) 可以用 ) ; --修改字段长度 )--修改表列名 ); -- ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站26之网站前台列表页的显示和完成分页功能

    我们用大I接收到我们get过来的栏目页的id然后通过文章的ar_cateid 来判断是不是属于该栏目下的,如果文章表ar_cateid = 栏目表的cate_id 那么就可以选出我们要查找的信息, 然 ...

  9. PAT乙级 1006. 换个格式输出整数 (15)

    1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...

  10. JSP-03-实现数据传递

    会话跟踪:隐藏表单域.URL重写和Cookie 3.1 传参方法 get /post 区别   3.2  request 获取参数 数据类型  变量名  =  (数据类型)request.getPar ...