Submission #2546172


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<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
            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.get(first).contains(i) && !path.contains(i)) {
                    path.addFirst(i);
                    flag = true;
                    break;
                }
            }
            int last = path.getLast();
            for (int i = 0; i < n; i++) {
                if (graph.get(last).contains(i) && !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 1641 Byte
Status TLE
Exec Time 2111 ms
Memory 353432 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 16
TLE × 1
MLE × 2
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 95 ms 21716 KB
sample_02.txt AC 96 ms 19796 KB
sample_03.txt AC 99 ms 21076 KB
subtask_1_01.txt AC 734 ms 109104 KB
subtask_1_02.txt AC 594 ms 62816 KB
subtask_1_03.txt MLE 1335 ms 292260 KB
subtask_1_04.txt AC 745 ms 106632 KB
subtask_1_05.txt AC 914 ms 177132 KB
subtask_1_06.txt MLE 1634 ms 350480 KB
subtask_1_07.txt AC 700 ms 109944 KB
subtask_1_08.txt AC 733 ms 113136 KB
subtask_1_09.txt TLE 2111 ms 353432 KB
subtask_1_10.txt AC 491 ms 63144 KB
subtask_1_11.txt AC 557 ms 68968 KB
subtask_1_12.txt AC 94 ms 16976 KB
subtask_1_13.txt AC 96 ms 21204 KB