Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 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 ...
- 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 ...
- 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...
- 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 ...
- 菊花加载第三方--MBprogressHUD 分类: ios技术 2015-02-05 19:21 120人阅读 评论(0) 收藏
上次说到了网络请求AFN,那么我们在网络请求的时候,等待期间,为了让用户不认为是卡死或程序出错,一般都会放一个菊花加载,系统有一个菊花加载类叫UIProgressHUD.但是我今天要说的是一个替代它的 ...
- 浅谈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. 后话 以前和安 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 安装shopex注意事项
[原创]关于PHP5.3.x和Zend Optimizer(Zend Guard Loader),以及shopex4.8.5安装的问题 http://dzmailbox.blog.163.com/b ...
- python入门到精通[一]:搭建开发环境
摘要:Python认识,及在windows和linux上安装环境,测试是否安装成功. 1.写在前面 参加工作也有5年多了,一直在做.net开发,近一年有做NodeJS开发.从一开始的不习惯,到逐步适应 ...
- 使用gradle创建java程序
创建一个Java项目 我们可以使用Java插件来创建一个Java项目,为了做到这点,我们需要把下面这段语句加入到build.gradle文件中: 1 apply plugin: 'java' 就是这样 ...
- C++(VS2012)DLL动态库的生成和调用
DLL动态链接库的生成: 首先打开VS2012,新建——项目——Win32控制台应用程序(项目名称:ConsoleApplication1)——空项目 新建一个源文件source.cpp 先用控制台e ...
- [原创]java WEB学习笔记86:Hibernate学习之路-- -映射 n-n 关系,单向n-n,双向n-n
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 如何为 Eclipse 中的 Java 源文件设置为 UTF-8 默认编码(转)
要让一个 Java 源文件打开时编码格式为 UTF-8,需要做2件事情: 1)设置Java 源文件的默认编码格式为UTF-8: 2)设置workspace的编码格式为UTF-8. 相应设置如下: 设置 ...
- @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不
@有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特 ...
- 关于用php插入汉字到oracle中出现不乱码问题
$conn = oci_pconnect("IBADMINTON", "5206576360", $db,'utf8'); 在代码中加入‘utf8’即可: ...
- MVC3/4 自定义HtmlHelper截断文本内容(截取)
在MVC目录下新建一个名为 Extersions 的文件夹,在该文件夹中新建一个截断文本类,取名为:CutOfTextExtersions 该类代码如下: using System; using S ...
- 【rails3教材】博客构建过程2
2. 使用脚手架快速搭建网页 rails的脚手架可以快速生成应用程序的一些片段,如果你需要为一个资源创建一系列的控制器视图模型,那么脚手架就是你需要的工具 3. 创建资源 对于一个博客程序,你可以以生 ...