結果

問題 No.629 グラフの中に眠る門松列
ユーザー tails1434
提出日時 2020-02-06 07:40:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 537 ms / 4,000 ms
コード長 2,053 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,008 KB
実行使用メモリ 77,244 KB
最終ジャッジ日時 2024-09-25 00:03:25
合計ジャッジ時間 4,795 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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