結果

問題 No.629 グラフの中に眠る門松列
ユーザー uafr_cs
提出日時 2018-01-05 21:42:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 334 ms / 4,000 ms
コード長 1,843 bytes
コンパイル時間 3,300 ms
コンパイル使用メモリ 79,708 KB
実行使用メモリ 47,816 KB
最終ジャッジ日時 2024-12-23 06:28:32
合計ジャッジ時間 12,525 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	
	public static boolean find(int deep, int limit, int[] history, int node, int parent, int[] as, ArrayList<HashSet<Integer>> adj){
		history[deep] = node;
		if(deep + 1 >= limit){ return true; }
		
		for(final int next : adj.get(node)){
			if(next == parent){ continue; }
			
			if(deep == 0){
				final int curr_as = as[history[deep]];
				final int next_as = as[next];
				if(curr_as == next_as){ continue; }
			}else if(deep == 1){
				final int prev_as = as[history[deep - 1]];
				final int curr_as = as[history[deep - 0]];
				final int next_as = as[next];
				
				if(prev_as == next_as || curr_as == next_as){ continue; }
				if(prev_as < curr_as && curr_as < next_as){ continue; }
				if(prev_as > curr_as && curr_as > next_as){ continue; }
			}
			
			if(find(deep + 1, limit, history, next, node, as, adj)){
				return true;
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int M = sc.nextInt();
		
		int[] as = new int[N];
		for(int i = 0; i < N; i++){ as[i] = sc.nextInt(); }
		
		ArrayList<HashSet<Integer>> adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){ adj.add(new HashSet<Integer>()); }
		
		for(int i = 0; i < M; i++){
			final int a = sc.nextInt() - 1;
			final int b = sc.nextInt() - 1;
			
			adj.get(a).add(b);
			adj.get(b).add(a);
		}
		
		boolean ret = false;
		for(int i = 0; i < N; i++){
			ret |= find(0, 3, new int[3], i, -1, as, adj);
		}
		System.out.println(ret ? "YES" : "NO");
	}
}
0