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 ...
随机推荐
- mongodb远程连接以及备份、还原、导出、导入
一.远程连接mongodb 连接命令:mongo -u username -p pwd 192.168.41.215:27017/database(用户名对应的数据库) 二.mongodump备份数据 ...
- wampserver环境下,安装ucenter1.6.0
1,)从官网下载UCenter_1.6.0_SC_UTF8.zip,解压拷贝upload到www下,重命名upload->ucenter; 2,)D:\wamp\bin\apache\Apach ...
- (Protype Pattern)原型模式
定义: 原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的对象 适用性: 当我们系统中有一些类,在使用的时候都有同样需要大量的创建,而这样的创建是复杂的而且是浪费CPU,内存资源的 ...
- CSS文档流与块级元素和内联元素
CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...
- Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-1
学习目标 一.进一步学习Swift的游戏制作 二.掌握SKNode,SKSpriteNode的运用 三.了解SpriteKit的物理系统 四.掌握动作(SKAction)的运用 在这一章,我们要通过制 ...
- C++之路进阶——bzoj3172(单词)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...
- springday04-go2
练习:计算一个人的bmi指数.算法如下: 身高 单位是米 比如1.70 体重 单位是公斤 比如90 bmi指数 = 体重/身高/身高 如果bmi>24过重,否则正常.视图需要两个,一个是bmi_ ...
- 软件需求分析之NABCD模型
软件的特点:支持交友 N:想找到志同道合的人结伴出游,即可增进友谊,也可以提高出行的安全性. A:在景点下设置模块,可以看到其他人的出行计划,并可以相互交流,共同出游. B:这款软件可以让你交到朋友, ...
- ssh & display
在Windows下用ssh连接服务器的话putty是一个小巧而且实用的工具,如果想要图形界面,可以使用X工具配合putty. 或者直接使用xmanager enterprise,非 常方便. 如果在U ...
- yii2开启session
1.在写入session的页面. use yii\web\Session;$session = new Session;$session->open(); 2.在获取session的页面 use ...