Labeling Balls--poj3687
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12273 | Accepted: 3516 |
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 这是一个拓扑题,还不是普通的拓扑,这必须要反向建图+逆向输出,并且注意,这个题让输出的是各个人的位置!!!
#include <iostream>
#include<cstdio>
#include<cstring> using namespace std;
int map[][];
int degree[];
void topo(int n)
{
int i,j,mark,que[];
for(i=n;i>=;i--)
{
int x=;//标记看是否满足要求
for(j=n;j>=;j--)
{
if(degree[j]==)
{
x=;
mark=j;
break;
}
}
if(x==)
break;
que[mark]=i;
degree[mark]=-;
for(j=;j<=n;j++)
{
if(map[mark][j])
degree[j]--;
}
}
if(i!=-)
printf("-1\n");
else
{
printf("%d",que[]);
for(i=;i<=n;i++)
printf(" %d",que[i]);
printf("\n"); }
} int main()
{
int N,i,m,n,a,b;
scanf("%d",&N);
while(N--)
{
memset(map,,sizeof(map));
memset(degree,,sizeof(degree));
scanf("%d%d",&n,&m);
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
if(!map[b][a])//避免重复录入
{
map[b][a]=;
degree[a]++;//反向建图
}
}
topo(n);
}
return ;
}
Labeling Balls--poj3687的更多相关文章
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- POJ3687.Labeling Balls 拓扑排序
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...
- Labeling Balls(poj3687)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13109 Accepted: 3782 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- Labeling Balls(拓扑排序wa)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12466 Accepted: 3576 D ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- Labeling Balls(变种拓扑)
Labeling Balls Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
随机推荐
- iOS学习之下拉刷新
今天我们来给昨天的Demo加上下拉刷新和上拉加载更多的功能. 1.下拉刷新. 在viewDidLoad中调用方法addRefreshControl,下拉时可以出现风火轮加载更多的效果. - (void ...
- BIOS讲解
首先 BIOS其实没什么神奇的 就是 Bisic input/output System,所以基本输入输出系统是一块装入了启动和自检程序的EPROM或EEPROM集成块,实际上它是被固化在计算机R ...
- iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习
iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习 如果你不希望应用运行时 iPhone 进入锁屏待机状态,加入下面这行代码即可 [[UIApplication share ...
- 自制单片机之一------AT89S51最小系统制做
C51最小系统电路在网上一搜一大把,大同小异.我略做改动后如图: 加一个11.0592MHZ的晶振是为了以后做串口通信时和PC有相同的波特率.可用短路帽切换.说说板子的布局:网上卖的最小系统都是把板子 ...
- 【转】Ubuntu命令行下安装、卸载、管理软件包的方法
原文网址:http://oss.org.cn/html/47/n-67447.html 一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname ...
- 我所理解的设计模式(C++实现)——状态模式(State Pattern)
概述: 看看我们平时用的开关,同样一个开关他有2种状态:开和关,当她处于不同的状态的时候她的行为是不一样的,比如当她是开着的时候,你按她一下,她就变成了关闭状态,她是关着的时候按她一下,她就变成了开着 ...
- jQuery 遍历 json 方法大全
1.for循环: var obj = { "status":1, "bkmsg":"\u6210\u529f", "bkdata& ...
- 源码分析之spring-JdbcTemplate日志打印sql语句
对于开源的项目来说的好处就是我们遇到什么问题可以通过看源码来解决. 比如近期有个同事问我说,为啥JdbcTemplate中只有在Error的时候才打印出sql语句呢.我一想,这和log的配置有关系吧. ...
- python批量下载
# -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4.Qt import *from PyQt4.QtCore import *f ...
- J2EE基础总结(1)——J2EE入门
J2EE诞生的背景 在传统的开发模式(单层应用结构)下.应用普遍存在下面致命缺点: - 数据.页面和业务逻辑在一个逻辑层次中.功能紧密耦合. - 代码重用性极低,可维护性差. - 应用耦合度高,全然没 ...