結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-10 22:39:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,724 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 116,032 KB
最終ジャッジ日時 2023-10-14 01:32:59
合計ジャッジ時間 16,980 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,288 KB
testcase_01 AC 69 ms
71,332 KB
testcase_02 AC 71 ms
71,368 KB
testcase_03 AC 72 ms
71,184 KB
testcase_04 AC 225 ms
78,828 KB
testcase_05 AC 216 ms
79,532 KB
testcase_06 AC 213 ms
78,592 KB
testcase_07 AC 233 ms
78,940 KB
testcase_08 AC 218 ms
91,424 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 287 ms
102,496 KB
testcase_12 AC 262 ms
106,684 KB
testcase_13 AC 241 ms
103,336 KB
testcase_14 AC 218 ms
102,384 KB
testcase_15 AC 246 ms
100,724 KB
testcase_16 AC 316 ms
115,412 KB
testcase_17 WA -
testcase_18 AC 307 ms
115,888 KB
testcase_19 AC 263 ms
101,208 KB
testcase_20 AC 348 ms
96,064 KB
testcase_21 AC 355 ms
96,964 KB
testcase_22 AC 322 ms
98,164 KB
testcase_23 AC 348 ms
102,392 KB
testcase_24 AC 341 ms
109,428 KB
testcase_25 WA -
testcase_26 AC 328 ms
94,580 KB
testcase_27 AC 318 ms
94,336 KB
testcase_28 AC 307 ms
109,380 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 263 ms
96,208 KB
testcase_32 AC 269 ms
96,084 KB
testcase_33 AC 298 ms
104,496 KB
testcase_34 AC 132 ms
80,344 KB
testcase_35 AC 215 ms
83,980 KB
testcase_36 AC 353 ms
98,912 KB
testcase_37 AC 309 ms
98,288 KB
testcase_38 AC 263 ms
98,992 KB
testcase_39 AC 367 ms
103,276 KB
testcase_40 AC 301 ms
99,048 KB
testcase_41 WA -
testcase_42 AC 316 ms
94,428 KB
testcase_43 AC 227 ms
97,892 KB
testcase_44 AC 220 ms
81,416 KB
testcase_45 AC 223 ms
105,564 KB
testcase_46 AC 218 ms
105,672 KB
testcase_47 AC 130 ms
80,240 KB
testcase_48 AC 195 ms
93,076 KB
testcase_49 AC 200 ms
93,168 KB
testcase_50 AC 187 ms
93,208 KB
testcase_51 AC 170 ms
84,760 KB
testcase_52 AC 191 ms
92,700 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 = [0]*N
    for i in range(N):
        if visited[i]:
            continue
        visited[i] = 1
        stack = [i]
        while stack:
            v = stack.pop()
            for dest in adj[v]:
                if dest == i:
                    print('Yes')
                    exit()
                if visited[dest]:
                    continue
                visited[dest] = 1
                stack.append(dest)
    else:
        print('No')
0