Submission #2546176


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;
            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 1846 Byte
Status TLE
Exec Time 2110 ms
Memory 350896 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 93 ms 23892 KB
sample_02.txt AC 94 ms 21332 KB
sample_03.txt AC 94 ms 21844 KB
subtask_1_01.txt AC 697 ms 100400 KB
subtask_1_02.txt AC 650 ms 66648 KB
subtask_1_03.txt MLE 1590 ms 289144 KB
subtask_1_04.txt AC 741 ms 100088 KB
subtask_1_05.txt AC 907 ms 167676 KB
subtask_1_06.txt MLE 1808 ms 345164 KB
subtask_1_07.txt AC 652 ms 107116 KB
subtask_1_08.txt AC 669 ms 106940 KB
subtask_1_09.txt TLE 2110 ms 350896 KB
subtask_1_10.txt AC 495 ms 49464 KB
subtask_1_11.txt AC 552 ms 51596 KB
subtask_1_12.txt AC 93 ms 19156 KB
subtask_1_13.txt AC 92 ms 18644 KB