浅谈欧拉回路与欧拉路径:http://blog.csdn.net/prime_min/article/details/40686563
数据结构之图(存储结构、遍历) :http://blog.chinaunix.net/uid-26548237-id-3483650.html
HashSet,TreeSet和LinkedHashSet的区别:http://www.cnblogs.com/Terry-greener/archive/2011/12/02/2271707.html
java 中的Set,List,Map:http://blog.csdn.net/wordinput/article/details/5223876
海岛Blog的C++版:http://blog.csdn.net/tigerisland45/article/details/56664423
——号称是100分,然而实际测试只得了50分,也是运行错误。
——经我再三研究,认为是出题方那里有问题...至少目前没发现我代码的问题...
这道题思路还是很清晰的:
给定一个图,让你求欧拉路径。
欧拉路径存在需要条件:1.图是通路。2.存在0个或2个奇度定点。
可以通过并查集的方法,来判断两点是否连通。
在字典序最小解的问题上,只需将数据先按字典序排序,再DFS,这样出来的结果就是题目要的解。
思路绝对没有错,然鹅=_=提交后“运行错误”,70分代码如下:
package csp2015_12_4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Main{
static List<Integer> list[]; //存储数据
static boolean vis[][]; //对vis[a][b] = 边a-b是否被访问过
static Stack<Integer> path = new Stack<Integer>(); //最后的欧拉路径
//并查集
static int pre[];
//查找+压缩
static int find(int x){
if(x == pre[x]) return x;
else return pre[x] = find(pre[x]);
}
static void join(int x,int y){
x = find(x);
y = find(y);
if(x!=y){
pre[x] = y;
}
}
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
vis = new boolean[n+1][n+1];
pre = new int[n+1];
for (int i = 0; i < pre.length; i++) {
pre[i] = i;
}
list = new ArrayList[n+1];
for (int i = 0; i < list.length; i++) {
list[i] = new ArrayList<Integer>();
}
int a,b;
for (int i = 0; i < m; i++) {
a = sc.nextInt();
b = sc.nextInt();
list[a].add(b);
list[b].add(a);
join(a,b);
}
boolean haveSolution = true;
//have wrong?加上这一部分就运行错误
int root = find(1);
for (int i = 2; i <= n; i++) {
if(root!=find(i)){
haveSolution = false;
break;
}
}
int num = 0;
for (int i = 1; i <= n; i++) {
if(list[i].size()%2 != 0){
num++;
}
}
if(num!=0 && num!=2){
haveSolution = false;
}
if(haveSolution){
//排序,这样dfs出来的结果就是自然顺序
for (int i = 0; i < list.length; i++) {
Collections.sort(list[i]);
}
//从结点1开始查找
dfs(1);
//输出结果
while(!path.isEmpty()){
System.out.print(path.pop()+" ");
}
}else{
//不存在该路
System.out.println("-1");
}
}
private static void dfs(int i) {
for (int j = 0; j < list[i].size(); j++) {
int next = list[i].get(j);
if(!vis[i][next]){
vis[i][next] = true;
vis[next][i] = true;
dfs(next);
}
}
path.push(i);
}
}
/**
* 其他的建立邻接表的方式,但测试还是上面List的方法更快,而且能很方便的实现排序
*
class Node{
int name;
Node next = null;
Node(int name,Node next){
this.name = name;
this.next = next;
}
Node(int name){
this.name = name;
}
public Node() {
}
}
public class{
static Node VNode[];
public static void main(String [] args){
VNode = new Node[n+1];
for (int i = 0; i < VNode.length; i++) {
VNode[i] = new Node();
VNode[i].name = i;
VNode[i].next = null;
}
int a,b;
for (int i = 0; i < m; i++) {
a = sc.nextInt();
b = sc.nextInt();
Node temp = new Node(b);
getLast(VNode[a]).next = temp;
//加上下面的就是无向图
Node temp2 = new Node(a);
getLast(VNode[b]).next = temp2;
}
}
static Node getLast(Node node) {
while(node.next!=null){
node = node.next;
}
return node;
}
}
**/
问题描述
试题编号: | 201512-4 |
试题名称: | 送货 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述
为了增加公司收入,F公司新开设了物流业务。由于F公司在业界的良好口碑,物流业务一开通即受到了消费者的欢迎,物流业务马上遍及了城市的每条街道。然而,F公司现在只安排了小明一个人负责所有街道的服务。
任务虽然繁重,但是小明有足够的信心,他拿到了城市的地图,准备研究最好的方案。城市中有 n个交叉路口, m条街道连接在这些交叉路口之间,每条街道的首尾都正好连接着一个交叉路口。除开街道的首尾端点,街道不会在其他位置与其他街道相交。每个交叉路口都至少连接着一条街道,有的交叉路口可能只连接着一条或两条街道。 小明希望设计一个方案,从编号为1的交叉路口出发,每次必须沿街道去往街道另一端的路口,再从新的路口出发去往下一个路口,直到所有的街道都经过了正好一次。
输入格式
输入的第一行包含两个整数
n,
m,表示交叉路口的数量和街道的数量,交叉路口从1到
n标号。
接下来 m行,每行两个整数 a, b,表示和标号为 a的交叉路口和标号为 b的交叉路口之间有一条街道,街道是双向的,小明可以从任意一端走向另一端。两个路口之间最多有一条街道。
输出格式
如果小明可以经过每条街道正好一次,则输出一行包含
m+1个整数
p
1,
p
2,
p
3, ...,
pm
+1,表示小明经过的路口的顺序,相邻两个整数之间用一个空格分隔。如果有多种方案满足条件,则输出字典序最小的一种方案,即首先保证
p
1最小,
p
1最小的前提下再保证
p
2最小,依此类推。
如果不存在方案使得小明经过每条街道正好一次,则输出一个整数-1。
样例输入
4 5
1 2 1 3 1 4 2 4 3 4
样例输出
1 2 4 1 3 4
样例说明
城市的地图和小明的路径如下图所示。
样例输入
4 6
1 2 1 3 1 4 2 4 3 4 2 3
样例输出
-1
样例说明
城市的地图如下图所示,不存在满足条件的路径。
评测用例规模与约定
前30%的评测用例满足:1 ≤
n ≤ 10,
n-1 ≤
m ≤ 20。
前50%的评测用例满足:1 ≤ n ≤ 100, n-1 ≤ m ≤ 10000。 所有评测用例满足:1 ≤ n ≤ 10000, n-1 ≤ m ≤ 100000。 |