結果

問題 No.1023 Cyclic Tour
ユーザー mkawa2mkawa2
提出日時 2021-04-09 13:20:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 364 ms / 2,000 ms
コード長 2,664 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 128,256 KB
最終ジャッジ日時 2024-06-24 20:36:59
合計ジャッジ時間 18,850 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,528 KB
testcase_01 AC 40 ms
54,272 KB
testcase_02 AC 39 ms
54,784 KB
testcase_03 AC 39 ms
54,528 KB
testcase_04 AC 247 ms
91,264 KB
testcase_05 AC 256 ms
91,264 KB
testcase_06 AC 277 ms
91,160 KB
testcase_07 AC 293 ms
91,776 KB
testcase_08 AC 238 ms
108,160 KB
testcase_09 AC 242 ms
108,544 KB
testcase_10 AC 238 ms
111,948 KB
testcase_11 AC 279 ms
112,640 KB
testcase_12 AC 267 ms
122,576 KB
testcase_13 AC 262 ms
119,708 KB
testcase_14 AC 232 ms
117,068 KB
testcase_15 AC 264 ms
120,508 KB
testcase_16 AC 302 ms
125,468 KB
testcase_17 AC 292 ms
122,896 KB
testcase_18 AC 288 ms
119,168 KB
testcase_19 AC 270 ms
115,232 KB
testcase_20 AC 364 ms
89,428 KB
testcase_21 AC 346 ms
90,112 KB
testcase_22 AC 316 ms
103,168 KB
testcase_23 AC 319 ms
102,400 KB
testcase_24 AC 343 ms
116,864 KB
testcase_25 AC 351 ms
89,876 KB
testcase_26 AC 341 ms
88,704 KB
testcase_27 AC 310 ms
88,448 KB
testcase_28 AC 334 ms
115,840 KB
testcase_29 AC 283 ms
119,412 KB
testcase_30 AC 307 ms
109,720 KB
testcase_31 AC 274 ms
103,248 KB
testcase_32 AC 302 ms
112,768 KB
testcase_33 AC 306 ms
108,160 KB
testcase_34 AC 260 ms
89,216 KB
testcase_35 AC 322 ms
89,636 KB
testcase_36 AC 323 ms
91,096 KB
testcase_37 AC 323 ms
104,704 KB
testcase_38 AC 289 ms
117,120 KB
testcase_39 AC 354 ms
104,064 KB
testcase_40 AC 319 ms
102,388 KB
testcase_41 AC 320 ms
101,232 KB
testcase_42 AC 348 ms
89,472 KB
testcase_43 AC 313 ms
117,376 KB
testcase_44 AC 260 ms
85,120 KB
testcase_45 AC 236 ms
128,244 KB
testcase_46 AC 240 ms
128,256 KB
testcase_47 AC 127 ms
90,112 KB
testcase_48 AC 221 ms
97,368 KB
testcase_49 AC 209 ms
94,912 KB
testcase_50 AC 203 ms
97,536 KB
testcase_51 AC 217 ms
95,104 KB
testcase_52 AC 200 ms
97,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
dij = [(0, 1), (1, 0), (1, 1), (1, -1)]
inf = 10**19
# md = 998244353
md = 10**9+7

class UnionFind:
    def __init__(self, n):
        self.state = [-1]*n
        self.size_table = [1]*n
        # cntはグループ数
        self.cnt = n

    def root(self, u):
        v = self.state[u]
        if v < 0: return u
        self.state[u] = res = self.root(v)
        return res

    def merge(self, u, v):
        ru = self.root(u)
        rv = self.root(v)
        if ru == rv: return
        du = self.state[ru]
        dv = self.state[rv]
        if du > dv: ru, rv = rv, ru
        if du == dv: self.state[ru] -= 1
        self.state[rv] = ru
        self.cnt -= 1
        self.size_table[ru] += self.size_table[rv]
        return

    # グループの要素数
    def size(self, u):
        return self.size_table[self.root(u)]

from collections import defaultdict

n, m = LI()
uf = UnionFind(n)

to = [[] for _ in range(n)]
deg = [0]*n
for _ in range(m):
    u, v, c = LI1()
    if c:
        to[u].append(v)
    else:
        uf.merge(u, v)
        deg[u] += 1
        deg[v] += 1

rtou = defaultdict(list)
for u in range(n):
    r = uf.root(u)
    rtou[r].append(u)

gtou = list(rtou.values())
utog = [0]*n
for g, uu in enumerate(gtou):
    e = 0
    for u in uu:
        utog[u] = g
        e += deg[u]
    if e//2 > len(uu)-1:
        print("Yes")
        exit()

gn = len(gtou)

to2 = [set() for _ in range(gn)]
for u in range(n):
    g = utog[u]
    for v in to[u]:
        to2[g].add(utog[v])

def dfs(g):
    stack = [g]
    while stack:
        g = stack.pop()
        if fin[g] == -1:
            fin[g] = 0
            stack.append(g)
            for v in to2[g]:
                if fin[v] == -1:
                    stack.append(v)
                elif fin[v] == 0:
                    return True
        elif fin[g] == 0:
            fin[g] = 1
    return False

fin = [-1]*gn
for g in range(gn):
    if fin[g] == -1 and dfs(g):
        print("Yes")
        exit()

print("No")
0