Submission #2546163
Source Code Expand
import java.util.LinkedList; import java.util.Scanner; public class Main { static int[][] dp; static Scanner sc; static int n; static int m; static int[][] graph; public static void main(String[] args) { sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); graph = new int[n][n]; int n1 = 0; int n2 = 0; for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph[a][b] = 1; graph[b][a] = 1; 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 (graph[i][first] == 1 && !path.contains(i)) { path.addFirst(i); flag = true; break; } } int last = path.getLast(); for (int i = 0; i < n; i++) { if (graph[i][last] == 1 && !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 | 1544 Byte |
Status | MLE |
Exec Time | 804 ms |
Memory | 938752 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 | 96 ms | 19668 KB |
sample_02.txt | AC | 92 ms | 20052 KB |
sample_03.txt | AC | 96 ms | 19796 KB |
subtask_1_01.txt | MLE | 735 ms | 870204 KB |
subtask_1_02.txt | AC | 609 ms | 204656 KB |
subtask_1_03.txt | MLE | 804 ms | 908620 KB |
subtask_1_04.txt | MLE | 666 ms | 932324 KB |
subtask_1_05.txt | MLE | 731 ms | 931300 KB |
subtask_1_06.txt | MLE | 673 ms | 938752 KB |
subtask_1_07.txt | MLE | 651 ms | 869580 KB |
subtask_1_08.txt | MLE | 634 ms | 876240 KB |
subtask_1_09.txt | MLE | 607 ms | 869588 KB |
subtask_1_10.txt | AC | 487 ms | 46780 KB |
subtask_1_11.txt | AC | 494 ms | 46048 KB |
subtask_1_12.txt | AC | 95 ms | 20820 KB |
subtask_1_13.txt | AC | 93 ms | 19796 KB |