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:

  1. No two balls share the same label.
  2. 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


题意和思路:给你标号为1~N的球,a b代表a比b轻,需要你排列出这N个球的顺序,要求按编号字典序排列,最后要你依次给出这N个球的重量(这里要注意不是叫你给出编号的顺序,而是1的质量,2的质量...N的质量这样输出,就这里这WA了...)。思路很简单,就是把轻的球加度,然后重的指向轻的,然后度0的就是重的,优先队列要用降序,这样就能保证度为0的比较重的先取出,(这里用倒着存到ans[ ],从头往后读就是编号的正确顺序),但我们要注意他要我们输出重量,所以ans[ ]编号里从最后一个开始依次赋值1~N

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=210;
const int INF=1e9;
using namespace std;
int n,m,degree[N],ans[N],weight[N];    //ans记录倒序的编号,weight记录最终答案
vector<int> g[N];     //邻接表
void topo(){
int cnt=0;
priority_queue<int,vector<int>,less<int> > q;
for(int i=1;i<=n;i++){
if(degree[i]==0) q.push(i);
}
while(!q.empty()){
int v=q.top();
q.pop();
ans[cnt]=v;
cnt++;
for(int i=0;i<g[v].size();i++){
int p=g[v][i];
degree[p]--;
if(degree[p]==0) q.push(p);
}
}
if(cnt!=n) cout<<-1<<endl;
else{
int wei=1;
for(int i=cnt-1;i>=0;i--){
weight[ans[i]]=wei++;
}
for(int i=1;i<=n;i++) printf("%d ",weight[i]);
cout<<endl;
}
}
int main(){
int T,a,b;
scanf("%d",&T);
while(T--){
memset(degree,0,sizeof(degree));
for(int i=0;i<N;i++) g[i].clear();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
degree[a]++; //轻的加度
g[b].push_back(a);
}
topo();
}
return 0;
}

POJ 3687 Labeling Balls(拓扑排序)题解的更多相关文章

  1. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  2. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  3. PKU 3687 Labeling Balls(拓扑排序)

    题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...

  4. poj 3687 Labeling Balls - 贪心 - 拓扑排序

    Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...

  5. POJ 3687 Labeling Balls【拓扑排序 优先队列】

    题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...

  6. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  7. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  8. poj 3687 Labeling Balls(拓补排序)

    Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...

  9. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

  10. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

随机推荐

  1. 10个实用的Django建议(转)

    前言:随着Django1.4第二个候选版的发布,虽然还不支持Python3,但Django团队已经在着手计划中,据官方博客所说, Django1.5将会试验性的支持python3.Django 作为一 ...

  2. sql优化 慢查询分析

    查询速度慢的原因很多,常见如下几种 SQL慢查询分析 转自:https://www.cnblogs.com/firstdream/p/5899383.html 1.没有索引或者没有用到索引(这是查询慢 ...

  3. 菜鸟教程之工具使用——Git的基本使用

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/liushuijinger/article/details/37569907 Git是进来比較火的版本 ...

  4. 使用navicat mysql 远程连接数据库

    远程连接数据库,假设两台主机上都有navicat 客户端 远程主机A    ip地址:192.168.100.91  ,port  3306,数据库用户名 rootA   密码 123456A 本地主 ...

  5. linux 使用文件作为交换分区

    sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile ...

  6. RMAN中%d %t %s %u,%p,%c 等代替变量的意义

    backup incremental level 0 database format='LEV0_%d_%t_%U_%s_%p' format=string 文件路径和名称的格式串,其中可包含宏变量: ...

  7. LeetCode--44 通配符匹配

    题目就是说两个字符串,一个含有通配符,去匹配另一个字符串:输出两个字符串是否一致. 注意:’?’表示匹配任意一个字符,’*’表示匹配任意字符0或者多次 首先,我们想到暴力破解.如果从头到尾的破解,到第 ...

  8. 几乎考虑到了每个细节的php图片上传

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  9. -webkit-line-clamp超过两行就出现省略号

    -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 限制在一个块元素显示的文本的行数. 为了实现该 ...

  10. 性能分析之– JAVA Thread Dump 分析

    最近在做性能测试,需要对线程堆栈进行分析,在网上收集了一些资料,学习完后,将相关知识整理在一起,输出文章如下. 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Du ...