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 ...
随机推荐
- winner tree 胜者树
在树形选择排序中,利用锦标赛思想建立的树称为胜者树. 1.每个非终端节点存储的是左右孩子节点中的优胜者. 2.通过减少比较次数,提高效率. 3.胜者树就是一颗特殊的线段树. 一.构建树 Procedu ...
- Eclipse jar打包
方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...
- nginx字体文件配置
http://www.doucode.com/nginx-support-font/ ====== nginx location语法 基本语法:location [=|~|~*|^~] /uri/ { ...
- NABCD模式
各位用户: 我们的“昵妆”是为了帮助不会化妆的用户解决困难, 他们需要有适合他们的优质的化妆品和 正确的视频或者化妆师来指导他们,但是现有的方案并没有很好地解决这些需求,我们有独特的办法, ...
- Leetcode: Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...
- CentOS 7 内核更新后删除旧内核
0.当前 # uname -sr Linux -.el7.x86_64 1.搜索查询 # rpm -q kernel kernel--.el7.x86_64 kernel--.el7.x86_64 k ...
- Java基础(48):归并排序的Java封装含原理,完整可运行,结合VisualGo网站更好理解)
原理: 归并排序建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序, ...
- C++之路起航——标准模板库(vector)
vector(动态数组或向量):动态分配内存空间的线性储存结构. 需要包括头文件<vector> 定义: vector<数据类型> 变量名: Eg: vector<int ...
- 1019: A+B和C比大小
1019: A+B和C比大小 时间限制: 1 Sec 内存限制: 128 MB提交: 518 解决: 300[提交][状态][讨论版] 题目描述 给定区间[-231, 231]内的3个整数A.B和 ...
- Linux内核之旅 List_entry()
#include "iostream" #define List_entry(type,member)\ (type *)(()->data)) ) using namesp ...