Submission #2546178
Source Code Expand
import java.util.*; public class Main { static int[][] dp; static Scanner sc; static int n; static int m; static List<Set<Integer>> graph; public static void main(String[] args) { sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); int n1 = 0; int n2 = 0; graph = new ArrayList<>(n+1); for (int i = 0; i < n; i++) { graph.add(new HashSet<>(8)); } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; if(b < a) { int c = a; a = b; b = c; } graph.get(a).add(b); n1 = a; n2 = b; } LinkedList<Integer> path = new LinkedList<>(); path.add(n1); path.add(n2); boolean flag = true; while (flag) { flag = false; int first = path.getFirst(); for (int i = 0; i < n; i++) { if (((first < i && graph.get(first).contains(i)) || (first > i &&graph.get(i).contains(first)))&& !path.contains(i)) { path.addFirst(i); flag = true; break; } } int last = path.getLast(); for (int i = 0; i < n; i++) { if (((last < i &&graph.get(last).contains(i)) || (last > i &&graph.get(i).contains(last))) && !path.contains(i)) { path.addLast(i); flag = true; break; } } } System.out.println(path.size()); for (Integer i : path) { System.out.print((i + 1) + " "); } } }
Submission Info
Submission Time | |
---|---|
Task | B - Hamiltonish Path |
User | pytran |
Language | Java8 (OpenJDK 1.8.0) |
Score | 0 |
Code Size | 1845 Byte |
Status | TLE |
Exec Time | 2106 ms |
Memory | 348668 KB |
Judge Result
Set Name | Sample | All | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 500 | ||||||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | sample_01.txt, sample_02.txt, sample_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample_01.txt | AC | 92 ms | 20564 KB |
sample_02.txt | AC | 93 ms | 21588 KB |
sample_03.txt | AC | 98 ms | 21460 KB |
subtask_1_01.txt | AC | 662 ms | 100796 KB |
subtask_1_02.txt | AC | 652 ms | 66020 KB |
subtask_1_03.txt | MLE | 1563 ms | 285916 KB |
subtask_1_04.txt | AC | 722 ms | 99236 KB |
subtask_1_05.txt | AC | 863 ms | 161012 KB |
subtask_1_06.txt | MLE | 1900 ms | 348668 KB |
subtask_1_07.txt | AC | 644 ms | 103348 KB |
subtask_1_08.txt | AC | 679 ms | 101160 KB |
subtask_1_09.txt | TLE | 2106 ms | 288412 KB |
subtask_1_10.txt | AC | 499 ms | 49260 KB |
subtask_1_11.txt | AC | 507 ms | 58232 KB |
subtask_1_12.txt | AC | 94 ms | 19156 KB |
subtask_1_13.txt | AC | 93 ms | 23764 KB |