hihocoder 1181 : 歐拉路·二

時間限制:10000ms
單點時限:1000ms
內存限制:256MB

描述

在上一回中小Hi和小Ho控制着主角收集了分散在各個木橋上的道具,這些道具其實是一塊一塊骨牌。

主角繼續往前走,面前出現了一座石橋,石橋的盡頭有一道火焰牆,似乎無法通過。

小Hi注意到在橋頭有一張小紙片,於是控制主角撿起了這張紙片,只見上面寫着:

將M塊骨牌首尾相連放置於石橋的凹糟中,即可關閉火焰牆。切記骨牌需要數字相同才能連接。
——By 無名的冒險者

小Hi和小Ho打開了主角的道具欄,發現主角恰好擁有M快骨牌。

小Ho:也就是說要把所有骨牌都放在凹槽中才能關閉火焰牆,數字相同是什麼意思?

小Hi:你看,每一塊骨牌兩端各有一個數字,大概是隻有當數字相同時纔可以相連放置,比如:

小Ho:原來如此,那麼我們先看看能不能把所有的骨牌連接起來吧。

 

提示:Fleury算法求歐拉路徑

 

輸入

第1行:2個正整數,N,M。分別表示骨牌上出現的最大數字和骨牌數量。1≤N≤1,000,1≤M≤5,000

第2..M+1行:每行2個整數,u,v。第i+1行表示第i塊骨牌兩端的數字(u,v),1≤u,v≤N

輸出

第1行:m+1個數字,表示骨牌首尾相連後的數字

比如骨牌連接的狀態爲(1,5)(5,3)(3,2)(2,4)(4,3),則輸出"1 5 3 2 4 3"

你可以輸出任意一組合法的解。

樣例輸入
5 5
3 5
3 2
4 2
3 4
5 1
樣例輸出
1 5 3 4 2 3
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
	//Fluery算法
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int num=scan.nextInt();
		int[][] map=new int[num][num];
		int cc=scan.nextInt();
		int start=0;
		for(int i=0;i<cc;i++){
			int x=scan.nextInt()-1;
			int y=scan.nextInt()-1;
			map[x][y]++;
			map[y][x]++;
		}
		int nn=0;
		for(int i=0;i<num;i++){
			int count=0;
			for(int j=0;j<num;j++){
				if(map[i][j]>0) count+=map[i][j];
			}
			if(count%2==1){
				nn++;
				start=i;
			} 
		}
		if(nn!=2&&nn!=0){
			System.out.println("no urler");
		}else{
			List<Integer> re=flery(start,map);
			for(int i=0;i<re.size();i++){
				System.out.print(re.get(i)+1+" ");
			}
		}
	}
	private static List<Integer> flery(int start,int[][] map) {
		List<Integer> re=new ArrayList<Integer>();
		Stack<Integer> stack=new Stack<Integer>();
		stack.push(start);
		while(!stack.isEmpty()){
			boolean hasBridge=false;
			for(int i=0;i<map.length;i++){
				if(map[stack.peek()][i]>0){
					hasBridge=true;break;
				}
			}
			if(!hasBridge){
				re.add(0,stack.pop());
			}else{
				dfs(stack.pop(),stack,map);
			}
		}
		return re;
	}
	
	public static void dfs(int start,Stack<Integer> stack,int[][] map){
		stack.push(start);
		for(int i=0;i<map.length;i++){
			if(map[start][i]>0){
				map[start][i]--;
				map[i][start]--;
				dfs(i,stack,map);
				break;
			}
		}
	}
}
/*
5 5
1 2
2 5
2 4
4 3
3 2
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main2 {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int num=scan.nextInt();
		int[] degree=new int[num+1];
		int count=scan.nextInt();
		HashMap<Integer,LinkedList<Integer>> map=new HashMap<>();
		for(int i=1;i<=num;i++) map.put(i,new LinkedList<Integer>());
		for(int i=0;i<count;i++){
			int x=scan.nextInt();
			int y=scan.nextInt();
			degree[x]++;degree[y]++;
			map.get(x).add(y);
			map.get(y).add(x);
		}
		int start=1;
		for(int i=1;i<=num;i++){
			if(degree[i]%2==1){
				start=i;break;
			}
		}
		List<Integer> re=fluery(map,start);
		for(Integer a:re){
			System.out.print(a+" ");
		}
	}
	private static List<Integer> fluery(HashMap<Integer, LinkedList<Integer>> map, int start) {
		List<Integer> re=new LinkedList<Integer>();
		if(map.isEmpty()) return re;
		dfs(map,start,re);
	    return re;
	}
	
	public static void dfs(HashMap<Integer,LinkedList<Integer>> map,int start,List<Integer> re){
		while(!map.get(start).isEmpty()){
			LinkedList<Integer> list=map.get(start);
			Integer a=list.remove(0);
			if(list.size()==0) map.remove(list);
			map.get(a).remove((Integer)start);
			if(map.get(a).isEmpty()) map.remove(map.get(a));
			dfs(map,a,re);
		}
		re.add(0,start);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章