1 问题描述

给出一些球,从1N编号,他们的重量都不相同,也用1N标记加以区分(这里真心恶毒啊,估计很多WA都是因为这里),然后给出一些约束条件,< a , b >要求编号为 a 的球必须比 b 轻,现在要求按编号升序输出每个球的重量,如果有多种解,输出字典序最小的那个。

例如:

input:

1

5 4

5 1

4 2

1 3

2 3

output:

2 4 5 3 1

package com.liuzhen.practice;

import java.util.ArrayList;
import java.util.Scanner; public class Main {
public static int count; //顶点的编号
public static int[] degree; //计算顶点的入度
public static ArrayList<edge>[] map; //表示图
public static ArrayList<String> result1 = new ArrayList<String>(); static class edge {
public int a; //边的起点
public int b; //边的终点 public edge(int a, int b) {
this.a = a;
this.b = b;
}
} @SuppressWarnings("unchecked")
public void init(int n) {
count = n;
degree = new int[n + 1];
map = new ArrayList[n + 1];
for(int i = 0;i <= n;i++) {
map[i] = new ArrayList<edge>();
degree[i] = 0;
}
return;
} public String getResult() {
String result = "";
int[] ans = new int[degree.length];
while(count >= 1) {
int i = degree.length - 1;
for(;i >= 1;i--) {
if(degree[i] == 0) {
ans[i] = count--;
degree[i]--;
for(int j = 0;j < map[i].size();j++)
degree[map[i].get(j).b]--;
break;
}
}
if(i == 0) //此时给定图存在回环
return "-1";
}
StringBuilder temp = new StringBuilder("");
for(int i = 1;i < ans.length;i++) {
temp.append(ans[i]);
if(i != ans.length - 1)
temp.append(" ");
}
result = temp.toString();
return result;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int t = in.nextInt(); //要输入图的个数
while(t > 0) {
t--;
int n = in.nextInt();
test.init(n);
int k = in.nextInt(); //输入图的边的个数
for(int i = 0;i < k;i++) {
int a = in.nextInt();
int b = in.nextInt();
boolean judge = true;
for(int j = 0;j < map[b].size();j++) { //检查重复边
if(map[b].get(j).b == a){
judge = false;
break;
}
}
if(judge && a != b) {
map[b].add(new edge(b, a));
degree[a]++; //顶点a的入度自增1
}
}
result1.add(test.getResult());
}
for(int i = 0;i < result1.size();i++) {
System.out.println(result1.get(i));
}
}
}

运行结果:

4
1
2
3
3
5
1
1
8
1
8
4 5 3 1
1 6 2 7 8 3 4 9 10

Java实现Labeling Balls(拓扑排序的应用)的更多相关文章

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

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

  2. POJ3687.Labeling Balls 拓扑排序

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...

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

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

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

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

  5. [poj3687]Labeling Balls_拓扑排序

    Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...

  6. Labeling Balls(拓扑)

    http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...

  7. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  8. 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 ...

  9. POJ 3687:Labeling Balls(优先队列+拓扑排序)

    id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...

随机推荐

  1. 基于ELK搭建MySQL日志平台的要点和常见错误

    第一部分 概括 ELK是集分布式数据存储.可视化查询和日志解析于一体的日志分析平台.ELK=elasticsearch+Logstash+kibana,三者各司其职,相互配合,共同完成日志的数据处理工 ...

  2. Spring MVC教程——检视阅读

    Spring MVC教程--检视阅读 参考 Spring MVC教程--一点--蓝本 Spring MVC教程--c语言中午网--3.0版本太老了 Spring MVC教程--易百--4.0版本不是通 ...

  3. 多线程实践—Python多线程编程

    多线程实践 前面的一些文章和脚本都是只能做学习多线程的原理使用,实际上什么有用的事情也没有做.接下来进行多线程的实践,看一看在实际项目中是怎么使用多线程的. 图书排名示例 Bookrank.py: 该 ...

  4. Mysql 常用函数(10)- strcmp 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html strcmp 的作用 比较两个字符串的顺序是否完全 ...

  5. Nuget一键打包上传以及高级应用

    Nuget是什么不用多说,大家应该也没少用过Nuget, 不少人也应该使用过工具打Nuget包,接下来先一步步说明如何使用脚本完成Nuget一键打包 Nuget一键打包 配置Nuget环境 下载地址: ...

  6. Docker & k8s 系列二:本机k8s环境搭建

    本篇将会讲解k8s是什么?本机k8s环境搭建,部署一个pod并演示几个kubectl命令,k8s dashboard安装. k8s是什么 k8s是kubernetes的简写,它是一个全新的基于容器技术 ...

  7. rabbitMQ基于spring-rabbitnq

    一.什么是MQ MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递 ...

  8. webpack使用babel

    几个月没用webpack都忘了好多了. webpack构建前端,使用时除了entry/output,就是plugins和module.loaders,还有本地测试的devServer. npm ins ...

  9. 2.2 Go变量类型

    内置类型 值类型: bool 布尔类型 int(32 or 64), int8, int16, int32, int64 整数类型 uint(32 or 64), uint8(byte), uint1 ...

  10. 发现用System.Net.Mail发邮件(代码附后),附件稍微大一点就会造成程序假死. 有没有什么简单的解决办法呢? 多谢!!

    附件大,上传,发送一定会慢.程序卡,应该是主线程正在发送,邮件造成的.创建其他线程在后台去发.这样就不影响主线程做其他工作了   using System; using System.Collecti ...