結果

問題 No.629 グラフの中に眠る門松列
ユーザー k_6101k_6101
提出日時 2019-12-19 00:41:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 223 ms / 4,000 ms
コード長 8,036 bytes
コンパイル時間 3,712 ms
コンパイル使用メモリ 97,644 KB
実行使用メモリ 59,600 KB
最終ジャッジ日時 2023-09-21 04:33:15
合計ジャッジ時間 12,557 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,984 KB
testcase_01 AC 121 ms
55,704 KB
testcase_02 AC 121 ms
55,996 KB
testcase_03 AC 120 ms
55,788 KB
testcase_04 AC 121 ms
55,480 KB
testcase_05 AC 119 ms
55,524 KB
testcase_06 AC 119 ms
55,736 KB
testcase_07 AC 121 ms
53,492 KB
testcase_08 AC 123 ms
55,516 KB
testcase_09 AC 122 ms
55,820 KB
testcase_10 AC 122 ms
55,960 KB
testcase_11 AC 121 ms
55,548 KB
testcase_12 AC 123 ms
55,680 KB
testcase_13 AC 126 ms
55,472 KB
testcase_14 AC 121 ms
55,712 KB
testcase_15 AC 124 ms
55,544 KB
testcase_16 AC 120 ms
55,872 KB
testcase_17 AC 123 ms
55,816 KB
testcase_18 AC 121 ms
55,704 KB
testcase_19 AC 121 ms
55,536 KB
testcase_20 AC 122 ms
55,768 KB
testcase_21 AC 192 ms
58,956 KB
testcase_22 AC 186 ms
58,336 KB
testcase_23 AC 185 ms
58,324 KB
testcase_24 AC 211 ms
59,144 KB
testcase_25 AC 217 ms
59,600 KB
testcase_26 AC 216 ms
59,412 KB
testcase_27 AC 219 ms
59,020 KB
testcase_28 AC 186 ms
56,676 KB
testcase_29 AC 205 ms
59,148 KB
testcase_30 AC 219 ms
59,040 KB
testcase_31 AC 218 ms
59,436 KB
testcase_32 AC 217 ms
59,280 KB
testcase_33 AC 223 ms
59,388 KB
testcase_34 AC 206 ms
58,968 KB
testcase_35 AC 202 ms
58,388 KB
testcase_36 AC 212 ms
59,284 KB
testcase_37 AC 210 ms
58,868 KB
testcase_38 AC 213 ms
59,380 KB
testcase_39 AC 204 ms
56,764 KB
testcase_40 AC 198 ms
59,024 KB
testcase_41 AC 182 ms
58,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

import static java.util.Comparator.*;

public class Main {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        Solver solver = new Solver(System.in, out);
        solver.solve();
        out.close();
    }
}
class Solver {
	Scanner sc;
	PrintWriter out;
    public Solver(InputStream in, PrintWriter out) {
    	sc = new Scanner(in);
    	this.out = out;
    }
    // ==================================================================
    Graph G = new Graph();
    int[] V = new int[1000];
    public void solve() {
    	int N = Integer.parseInt(sc.next());
    	int M = Integer.parseInt(sc.next());
    	for (int i = 0; i < N; i++) {
			V[i] = Integer.parseInt(sc.next());
		}
    	int u, v;
    	for (int i = 0; i < M; i++) {
			u = Integer.parseInt(sc.next()) - 1;
			v = Integer.parseInt(sc.next()) - 1;
			G.add(u, v);
			G.add(v, u);
		}
    	int l, m;
    	Set<Integer> set = new HashSet<>();
    	for (int i = 0; i < N; i++) {
    		l = m = 0;
    		if(G.get(i) == null)	continue;
    		set.clear();
			for(int to : G.get(i)) {
				if(set.contains(V[to]))	continue;
				set.add(V[to]);
//				out.println("頂点[" + (i+1) + "]  値 =" + V[i] 
//						+ "  繋がった頂点[" + (to+1) + "]  値 = " + V[to]);
				if(V[to] > V[i])			l++;
				else if(V[to] < V[i])		m++;
			}
//			out.println(" 大きかった数  = " + l + "   小さかった数 = " + m);
			if(l > 1 || m > 1) {
				out.println("YES");
				return;
			}
		}
    	out.println("NO");
    }
    // -----------------------------------------
    // 重みなし グラフのリンクリスト
    class Graph {
    	// 頂点に紐づく頂点のリスト
        private Map<Integer, List<Integer>> data = new HashMap<Integer, List<Integer>>();
//        // 全ての頂点のセット
//        private Set<Integer> point = new TreeSet<>();
        // 頂点と頂点の繋がりを追加する
        void add(int from, int to) {
        	List<Integer> list = data.get(from);
        	if(list == null) {
        		list = new ArrayList<Integer>();
        		data.put(from, list);
        	}
        	list.add(to);
//        	point.add(from);
//        	point.add(to);
        }
        // 始点から終点が繋がっていれば切る
        void del(int from, int to) {
        	List<Integer> list = data.get(from);
        	if(list == null) {
        		return;
        	}
        	if(list.contains(to)) {
        		list.remove((Object)to);
        	}
        }
        // 指定された頂点に紐づく、頂点のリストを返す
        List<Integer> get(int key) {
        	return data.get(key);
        }
        // 頂点 key が登録されているか?
        boolean contains(int key) {
        	return data.containsKey(key);
        }
        
        // 頂点のセットを返す
        Set<Integer> keySet() {
        	return data.keySet();
        }
        // 頂点 key_1 と 頂点 key_2 がつながっていれば true を返す
        boolean isConnect(int key_1, int key_2) {
        	List<Integer> list = data.get(key_1);
        	if(list == null)	return false;
        	else 				return list.contains(key_2);
        }
        // 指定された頂点から、すべての頂点への距離を返す(DFS O(N)) → 返り値が 終点と距離のペア のリスト
        List<PP> distList(int key) {
        	List<PP> dist = new ArrayList<>();		// 頂点と距離のペアのリスト
        	Set<Integer> mark = new HashSet<>();	// 処理したら入れる
			Stack<PP> stack = new Stack<>();		// スタックの宣言
			stack.push(new PP(key, 0));				// スタートをスタックに保存
			while(!stack.isEmpty()) {
				PP wk = stack.pop();				// スタックから次の頂点を取得
				int pp = wk.getKey();
				int dd = wk.getVal();
				mark.add(pp);						// 通過マーク
    			dist.add(new PP(pp, dd));			// 距離を登録
				List<Integer> list = get(pp);		// つながっている頂点のリストを取得
				for(int next : list) {
					if(mark.contains(next))	continue;
					stack.push(new PP(next, dd + 1));
				}
			}
			return dist;
        }
        // 指定された頂点から、すべての頂点への距離を返す(DFS O(N))	→ 返り値は配列
        int[] distV(int key) {
        	int[] dist = new int[data.keySet().size()+1];		// [頂点] = 距離
        	Arrays.fill(dist, -1);					// 届かない場合 -1
        	Set<Integer> mark = new HashSet<>();	// 処理したら入れる
			Stack<PP> stack = new Stack<>();		// スタックの宣言
			stack.push(new PP(key, 0));				// スタートをスタックに保存
			while(!stack.isEmpty()) {
				PP wk = stack.pop();				// スタックから次の頂点を取得
				int pp = wk.getKey();
				int dd = wk.getVal();
				mark.add(pp);						// 通過マーク
				dist[pp] = dd;						// 距離を登録
				List<Integer> list = get(pp);		// つながっている頂点のリストを取得
				for(int next : list) {
					if(mark.contains(next))	continue;
					stack.push(new PP(next, dd + 1));
				}
			}
			return dist;
        }
    	Map<Integer, Integer> mapCnt = new HashMap<>();		// 項番(何番目に訪れたか)
    	Map<Integer, Integer> mapLow = new HashMap<>();		// 初期値は項番、つながっている先の項番が小さければコピーする
															// mapCnt > mapLow なら閉路に含まれる頂点
    	Set<Integer> mark = new HashSet<>();				// 処理したら入れる
    	int number;
    	// mapCnt, mapLow を設定する
        int bridgeDfs(int now, int pre) {
//        	prn("bridgeDfs  now = " + now + " number = " + number);
        	mark.add(now);
        	mapCnt.put(now, number);
        	mapLow.put(now, number);
        	int low;
        	for(int next : get(now)) {
        		if(next == pre)		continue;
        		if(mark.contains(next)) {
        			if(mapLow.get(now) > mapLow.get(next)) {
//        				prn("  mapLow[" + now + "] = " + mapLow.get(now) 
//        						+ " > mapLow[" + next + "] = " + mapLow.get(next));
        				mapLow.put(now, mapLow.get(next));
        			}
        			continue;
        		}
        		number++;
        		low = bridgeDfs(next, now);
    			if(mapLow.get(now) > low) {
    				mapLow.put(now, low);
    			}
        	}
        	return mapLow.get(now);
        }
        // 橋の数を返す  先頭の頂点番号を引数とする
        int bridgeCnt(int start) {
        	mapCnt.clear();
        	mapLow.clear();
        	mark.clear();
        	number = 0;
        	bridgeDfs(start, start);
        	int ans = 0;
        	for(int key : mapCnt.keySet()) {
        		if(mapCnt.get(key) == mapLow.get(key)) {
        			ans++;
        		}
        	}
        	return ans - 1;
        }
        // ダンプ
        void dump(PrintWriter out) {
        	for(int key : data.keySet()) {
        		out.print(key + " : ");
        		for(int val : data.get(key)) {
        			out.print(val + " ");
        		}
        		out.println("");
        	}
        }
    }
    // Set に入れるなら PPKEY を使う!
    static class PP{
    	public int key, val;
    	public PP(int key, int val) {
    		this.key = key;
    		this.val = val;
    	}
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public int getVal() {
			return val;
		}
		public void setVal(int val) {
			this.val = val;
		}
    }
}

0