Pagini recente » Cod sursa (job #2478875) | Cod sursa (job #322010) | Cod sursa (job #442385) | Cod sursa (job #1386522) | Cod sursa (job #1265279)
package graphs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
public class Euler {
private Scanner sc;
private PrintWriter pw;
private static HashMap<Integer, Integer>[] graph;
private static ArrayList<Integer> solution;
public Euler() throws FileNotFoundException {
solution = new ArrayList<Integer>();
pw = new PrintWriter(new File("ciclueuluer.out"));
readData();
parseGraph(0);
for(int v : solution) {
pw.print((v + 1) + " ");
}
pw.close();
}
@SuppressWarnings("unchecked")
private void readData() {
try {
sc = new Scanner(new File("ciclueuluer.in"));
graph = new HashMap[sc.nextInt()];
int vertexes = sc.nextInt();
for (int i = 0; i < vertexes; i++) {
int vertex1 = sc.nextInt() - 1;
int vertex2 = sc.nextInt() - 1;
if (graph[vertex1] == null) {
graph[vertex1] = new HashMap<Integer, Integer>();
}
if (graph[vertex2] == null) {
graph[vertex2] = new HashMap<Integer, Integer>();
}
if (!graph[vertex1].containsKey(vertex2)) {
graph[vertex1].put(vertex2, 1);
} else {
graph[vertex1].put(vertex2, 2);
}
if (!graph[vertex2].containsKey(vertex1)) {
graph[vertex2].put(vertex1, 1);
} else {
graph[vertex2].put(vertex1, 2);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
sc.close();
}
}
private static void parseGraph(int vertex) {
System.out.print((vertex + 1) + " ");
solution.add(vertex);
for(Entry<Integer, Integer> entry : graph[vertex].entrySet()) {
if (entry.getValue() > 0 && graph[entry.getKey()].getOrDefault(vertex, 0) > 0) {
entry.setValue(entry.getValue() - 1);
graph[entry.getKey()].put(vertex, entry.getValue() - 1);
//System.out.println((vertex + 1) + " - " + (entry.getKey() + 1) + " : " + entry.getValue());
parseGraph(entry.getKey());
}
};
}
public static void main(String args[]) {
try {
new Euler();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}