結果
問題 | No.629 グラフの中に眠る門松列 |
ユーザー | denderaKawazu |
提出日時 | 2018-02-15 10:23:16 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 408 ms / 4,000 ms |
コード長 | 3,407 bytes |
コンパイル時間 | 4,024 ms |
コンパイル使用メモリ | 77,720 KB |
実行使用メモリ | 59,960 KB |
最終ジャッジ日時 | 2024-06-07 15:51:48 |
合計ジャッジ時間 | 13,563 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 139 ms
53,940 KB |
testcase_01 | AC | 138 ms
54,180 KB |
testcase_02 | AC | 142 ms
54,056 KB |
testcase_03 | AC | 140 ms
53,720 KB |
testcase_04 | AC | 141 ms
54,180 KB |
testcase_05 | AC | 142 ms
54,248 KB |
testcase_06 | AC | 138 ms
53,812 KB |
testcase_07 | AC | 135 ms
54,056 KB |
testcase_08 | AC | 136 ms
53,732 KB |
testcase_09 | AC | 140 ms
54,252 KB |
testcase_10 | AC | 139 ms
54,052 KB |
testcase_11 | AC | 141 ms
53,656 KB |
testcase_12 | AC | 140 ms
53,896 KB |
testcase_13 | AC | 136 ms
53,900 KB |
testcase_14 | AC | 137 ms
54,276 KB |
testcase_15 | AC | 143 ms
54,220 KB |
testcase_16 | AC | 139 ms
56,112 KB |
testcase_17 | AC | 137 ms
53,916 KB |
testcase_18 | AC | 143 ms
53,964 KB |
testcase_19 | AC | 141 ms
54,212 KB |
testcase_20 | AC | 139 ms
53,796 KB |
testcase_21 | AC | 408 ms
59,428 KB |
testcase_22 | AC | 220 ms
56,508 KB |
testcase_23 | AC | 403 ms
59,644 KB |
testcase_24 | AC | 242 ms
57,228 KB |
testcase_25 | AC | 399 ms
59,960 KB |
testcase_26 | AC | 393 ms
59,724 KB |
testcase_27 | AC | 283 ms
59,012 KB |
testcase_28 | AC | 217 ms
56,224 KB |
testcase_29 | AC | 252 ms
57,688 KB |
testcase_30 | AC | 270 ms
58,368 KB |
testcase_31 | AC | 272 ms
58,588 KB |
testcase_32 | AC | 280 ms
58,276 KB |
testcase_33 | AC | 311 ms
58,480 KB |
testcase_34 | AC | 253 ms
57,244 KB |
testcase_35 | AC | 252 ms
57,820 KB |
testcase_36 | AC | 267 ms
58,156 KB |
testcase_37 | AC | 267 ms
58,004 KB |
testcase_38 | AC | 264 ms
58,320 KB |
testcase_39 | AC | 244 ms
57,840 KB |
testcase_40 | AC | 235 ms
57,628 KB |
testcase_41 | AC | 236 ms
57,780 KB |
ソースコード
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; } } } }