結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 72 ms
71,368 KB
testcase_02 AC 71 ms
70,948 KB
testcase_03 AC 71 ms
70,900 KB
testcase_04 AC 254 ms
78,836 KB
testcase_05 AC 222 ms
79,476 KB
testcase_06 AC 222 ms
78,700 KB
testcase_07 AC 230 ms
78,824 KB
testcase_08 AC 214 ms
91,160 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 219 ms
91,624 KB
testcase_12 AC 200 ms
99,872 KB
testcase_13 AC 195 ms
103,140 KB
testcase_14 WA -
testcase_15 AC 201 ms
100,224 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 333 ms
94,420 KB
testcase_21 AC 339 ms
95,020 KB
testcase_22 AC 301 ms
98,116 KB
testcase_23 AC 298 ms
98,372 KB
testcase_24 AC 288 ms
109,020 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 135 ms
80,468 KB
testcase_35 AC 221 ms
83,776 KB
testcase_36 AC 321 ms
95,068 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 318 ms
96,440 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 325 ms
94,332 KB
testcase_43 AC 234 ms
98,032 KB
testcase_44 WA -
testcase_45 AC 195 ms
105,460 KB
testcase_46 WA -
testcase_47 AC 132 ms
80,200 KB
testcase_48 AC 194 ms
92,860 KB
testcase_49 AC 200 ms
92,872 KB
testcase_50 AC 177 ms
86,100 KB
testcase_51 AC 174 ms
84,556 KB
testcase_52 AC 190 ms
92,888 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] = i
                stack.append(dest)
    else:
        print('No')
0