結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-10 22:45:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,741 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,288 KB
実行使用メモリ 109,164 KB
最終ジャッジ日時 2023-10-14 03:18:07
合計ジャッジ時間 16,557 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 73 ms
71,192 KB
testcase_02 AC 74 ms
70,784 KB
testcase_03 AC 71 ms
70,996 KB
testcase_04 AC 279 ms
78,912 KB
testcase_05 AC 223 ms
79,368 KB
testcase_06 AC 219 ms
78,388 KB
testcase_07 AC 227 ms
78,660 KB
testcase_08 AC 222 ms
91,060 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 213 ms
91,692 KB
testcase_12 AC 206 ms
99,816 KB
testcase_13 AC 201 ms
103,032 KB
testcase_14 WA -
testcase_15 AC 202 ms
100,188 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 339 ms
94,352 KB
testcase_21 AC 348 ms
95,088 KB
testcase_22 AC 302 ms
97,644 KB
testcase_23 AC 313 ms
98,020 KB
testcase_24 AC 293 ms
109,164 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 134 ms
80,060 KB
testcase_35 AC 227 ms
83,784 KB
testcase_36 AC 328 ms
94,904 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 323 ms
96,288 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 327 ms
94,272 KB
testcase_43 AC 236 ms
97,776 KB
testcase_44 WA -
testcase_45 AC 196 ms
105,408 KB
testcase_46 WA -
testcase_47 AC 129 ms
79,980 KB
testcase_48 AC 193 ms
92,560 KB
testcase_49 AC 200 ms
92,948 KB
testcase_50 AC 175 ms
86,268 KB
testcase_51 AC 172 ms
84,440 KB
testcase_52 AC 186 ms
92,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


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

    def find(self, x: int) -> int:
        if self.nodes[x] < 0:
            return x
        else:
            self.nodes[x] = self.find(self.nodes[x])
            return self.nodes[x]

    def unite(self, x: int, y: int) -> bool:
        root_x, root_y, nodes = self.find(x), self.find(y), self.nodes

        if root_x != root_y:
            if nodes[root_x] > nodes[root_y]:
                root_x, root_y = root_y, root_x
            nodes[root_x] += nodes[root_y]
            nodes[root_y] = root_x

        return root_x != root_y


if __name__ == '__main__':
    N, M = map(int, input().split())
    edges = []
    uf = UnionFind(N)

    for u, v, c in (map(int, input().split()) for _ in range(M)):
        u -= 1
        v -= 1
        if c == 1:
            if not uf.unite(u, v):
                print('Yes')
                exit()
        else:
            edges.append((u, v))

    adj = [set() for _ in range(N)]
    rev = [set() for _ in range(N)]
    for u, v in edges:
        par_u, par_v = uf.find(u), uf.find(v)
        if par_u == par_v:
            print('Yes')
            exit()
        adj[par_u].add(par_v)

    visited = [-1]*N
    for i in range(N):
        if visited[i]:
            continue
        visited[i] = i
        stack = [i]

        while stack:
            v = stack.pop()
            for dest in adj[v]:
                if visited[dest] == i:
                    print('Yes')
                    exit()
                if visited[dest] != -1:
                    continue
                visited[dest] = 1
                stack.append(dest)
    else:
        print('No')
0