結果

問題 No.629 グラフの中に眠る門松列
ユーザー CreativeGP1
提出日時 2018-02-23 23:31:35
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 1,494 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-10-10 03:51:20
合計ジャッジ時間 7,502 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 6
other WA * 15 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[args] = f(*args)
        return cache[args]
    return helper

import sys
import copy
from operator import itemgetter
import itertools
from time import sleep
import random


@memoize
def eval(a, b, c):
    if a == b or b == c or a == c: return False
    return max(a, b, c) == b or min(a, b, c) == b

l = sys.stdin.readline().split(' ')
N = int(l[0])
M = int(l[1])
a = []
l = sys.stdin.readline().split(' ')
path = {}
can = []
for i in xrange(N):
    a.append(int(l[i]))
    path[i+1] = []
    can.append([i+1])
u = []
v = []
for i in xrange(M):
    l = sys.stdin.readline().split(' ')
    u.append(int(l[0]))
    v.append(int(l[1]))


for i, j in zip(u, v):
    path[i].append(j)
    path[j].append(i)

print(path)
count = 0
while True:
    count += 1
    if len(can) == 0: break
    if count > N*N: break
    newcan = []
    for c in can:
        if len(c) == 3:
            if eval(a[c[0]-1], a[c[1]-1], a[c[2]-1]):
                print "YES"
                sys.exit(0)
        else:
            try:
                p = random.choice(path[c[-1]])
                c.append(p)
                newcan.append([p])
                # del path[c[-2]][0]
                newcan.append(c)
            except IndexError: pass
    newcan.sort()
    if can == newcan: break
    can = list(newcan for newcan,_ in itertools.groupby(newcan))

print "NO"
0