結果

問題 No.629 グラフの中に眠る門松列
ユーザー tails1434tails1434
提出日時 2020-02-06 07:40:43
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 549 ms / 4,000 ms
コード長 2,053 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 81,652 KB
実行使用メモリ 76,744 KB
最終ジャッジ日時 2023-10-25 04:38:36
合計ジャッジ時間 5,780 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,536 KB
testcase_01 AC 40 ms
55,536 KB
testcase_02 AC 41 ms
55,536 KB
testcase_03 AC 40 ms
55,536 KB
testcase_04 AC 40 ms
55,536 KB
testcase_05 AC 41 ms
55,536 KB
testcase_06 AC 41 ms
55,536 KB
testcase_07 AC 42 ms
55,536 KB
testcase_08 AC 40 ms
55,536 KB
testcase_09 AC 40 ms
55,536 KB
testcase_10 AC 40 ms
55,536 KB
testcase_11 AC 40 ms
55,536 KB
testcase_12 AC 41 ms
55,536 KB
testcase_13 AC 41 ms
55,536 KB
testcase_14 AC 40 ms
55,536 KB
testcase_15 AC 41 ms
55,536 KB
testcase_16 AC 40 ms
55,536 KB
testcase_17 AC 39 ms
55,536 KB
testcase_18 AC 41 ms
55,536 KB
testcase_19 AC 41 ms
55,536 KB
testcase_20 AC 40 ms
55,536 KB
testcase_21 AC 60 ms
68,100 KB
testcase_22 AC 59 ms
68,104 KB
testcase_23 AC 58 ms
66,048 KB
testcase_24 AC 205 ms
76,744 KB
testcase_25 AC 200 ms
76,404 KB
testcase_26 AC 549 ms
76,332 KB
testcase_27 AC 90 ms
76,404 KB
testcase_28 AC 44 ms
57,584 KB
testcase_29 AC 96 ms
76,700 KB
testcase_30 AC 94 ms
76,580 KB
testcase_31 AC 89 ms
76,512 KB
testcase_32 AC 97 ms
76,712 KB
testcase_33 AC 95 ms
76,692 KB
testcase_34 AC 92 ms
76,536 KB
testcase_35 AC 86 ms
76,428 KB
testcase_36 AC 99 ms
76,616 KB
testcase_37 AC 92 ms
76,560 KB
testcase_38 AC 89 ms
76,548 KB
testcase_39 AC 98 ms
76,600 KB
testcase_40 AC 86 ms
76,432 KB
testcase_41 AC 82 ms
76,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.readline

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

def isCheck(a,b,c):
    if a != b and b != c and c != a:
        if max(a,b,c) == b or min(a,b,c) == b:
            return True
    return False

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    edge = [[] for _ in range(N)]
    uni = UnionFind(N)
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)
        edge[v].append(u)
        uni.union(u,v)

    Q = deque([])
    for i in uni.roots():
        Q.append((i,-1))
    flag = False
    visited = [False] * N
    while Q:
        s, p = Q.popleft()
        visited[s] = True
        for n in edge[s]:
            if p == n:
                continue
            
            for m in edge[s]:
                if n == m:
                    continue
                if isCheck(A[m], A[s], A[n]):
                    flag = True
                    break
            if not visited[n]:
                Q.append((n,s))

    if flag:
        print('YES')
    else:
        print('NO')

            


if __name__ == "__main__":
    main()
0