結果

問題 No.629 グラフの中に眠る門松列
ユーザー denderaKawazudenderaKawazu
提出日時 2018-02-15 10:23:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 369 ms / 4,000 ms
コード長 3,407 bytes
コンパイル時間 3,265 ms
コンパイル使用メモリ 75,384 KB
実行使用メモリ 61,476 KB
最終ジャッジ日時 2023-08-26 20:22:52
合計ジャッジ時間 12,790 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
55,856 KB
testcase_01 AC 120 ms
56,324 KB
testcase_02 AC 116 ms
55,864 KB
testcase_03 AC 110 ms
55,760 KB
testcase_04 AC 110 ms
55,680 KB
testcase_05 AC 108 ms
55,808 KB
testcase_06 AC 115 ms
55,864 KB
testcase_07 AC 120 ms
55,792 KB
testcase_08 AC 114 ms
56,024 KB
testcase_09 AC 118 ms
56,016 KB
testcase_10 AC 113 ms
55,840 KB
testcase_11 AC 111 ms
55,724 KB
testcase_12 AC 110 ms
55,664 KB
testcase_13 AC 111 ms
56,016 KB
testcase_14 AC 112 ms
55,876 KB
testcase_15 AC 118 ms
56,052 KB
testcase_16 AC 125 ms
55,736 KB
testcase_17 AC 122 ms
55,376 KB
testcase_18 AC 112 ms
56,152 KB
testcase_19 AC 110 ms
55,784 KB
testcase_20 AC 109 ms
55,620 KB
testcase_21 AC 363 ms
61,308 KB
testcase_22 AC 189 ms
58,704 KB
testcase_23 AC 364 ms
60,976 KB
testcase_24 AC 222 ms
60,012 KB
testcase_25 AC 369 ms
61,476 KB
testcase_26 AC 347 ms
60,948 KB
testcase_27 AC 228 ms
59,816 KB
testcase_28 AC 190 ms
58,860 KB
testcase_29 AC 213 ms
59,992 KB
testcase_30 AC 226 ms
59,740 KB
testcase_31 AC 222 ms
60,868 KB
testcase_32 AC 228 ms
60,084 KB
testcase_33 AC 254 ms
60,380 KB
testcase_34 AC 207 ms
59,516 KB
testcase_35 AC 200 ms
59,552 KB
testcase_36 AC 230 ms
60,200 KB
testcase_37 AC 234 ms
60,708 KB
testcase_38 AC 244 ms
59,980 KB
testcase_39 AC 209 ms
59,516 KB
testcase_40 AC 204 ms
59,544 KB
testcase_41 AC 202 ms
59,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        static boolean search(ArrayList<Task.Vertex> path) {
            if (path.size() == 3) {
                int v0 = path.get(0).getValue();
                int v1 = path.get(1).getValue();
                int v2 = path.get(2).getValue();
                return (v0 != v1 && v1 != v2 && v2 != v0) && ((v1 < v0 && v1 < v2) || (v1 > v0 && v1 > v2));
            }

            Task.Vertex v = path.get(path.size() - 1);
            for (int i = 0; i < v.getDegree(); i++) {
                ArrayList<Task.Vertex> next = new ArrayList<>();
                next.addAll(path);
                next.add(v.getEdge(i).getVertex());
                if (search(next)) {
                    return true;
                }
            }
            return false;
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            final int n = in.nextInt();
            final int m = in.nextInt();

            Task.Vertex[] v = new Task.Vertex[n];
            for (int i = 0; i < n; i++) {
                v[i] = new Task.Vertex(in.nextInt());
            }
            for (int i = 0; i < m; i++) {
                int index1 = in.nextInt() - 1;
                int index2 = in.nextInt() - 1;
                v[index1].setEdge(v[index2]);
                v[index2].setEdge(v[index1]);
            }

            boolean exist = false;
            for (int i = 0; i < n; i++) {
                ArrayList<Task.Vertex> path = new ArrayList<>();
                path.add(v[i]);
                if (search(path)) {
                    exist = true;
                    break;
                }
            }

            out.printf("%s", exist ? "YES" : "NO");
        }

        static class Vertex {
            private int value;
            private ArrayList<Task.Edge> edges = new ArrayList<Task.Edge>();

            Vertex() {
            }

            Vertex(int v) {
                this.value = v;
            }

            void setEdge(Task.Vertex v) {
                this.edges.add(new Task.Edge(v));
            }

            int getValue() {
                return this.value;
            }

            int getDegree() {
                return this.edges.size();
            }

            Task.Edge getEdge(int index) {
                return this.edges.get(index);
            }

        }

        static class Edge {
            private Task.Vertex v;
            private int cost;

            Edge() {
                this.v = v;
            }

            Edge(Task.Vertex v) {
                this.v = v;
            }

            Edge(Task.Vertex v, int c) {
                this.v = v;
                this.cost = c;
            }

            Task.Vertex getVertex() {
                return this.v;
            }

        }

    }
}

0