結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-10 22:47:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,747 bytes
コンパイル時間 1,671 ms
コンパイル使用メモリ 86,216 KB
実行使用メモリ 109,216 KB
最終ジャッジ日時 2023-10-14 03:23:54
合計ジャッジ時間 17,779 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,916 KB
testcase_01 AC 73 ms
71,052 KB
testcase_02 AC 72 ms
71,036 KB
testcase_03 AC 73 ms
71,032 KB
testcase_04 AC 229 ms
78,564 KB
testcase_05 AC 217 ms
79,380 KB
testcase_06 AC 218 ms
78,556 KB
testcase_07 AC 227 ms
78,884 KB
testcase_08 AC 214 ms
91,168 KB
testcase_09 AC 241 ms
101,108 KB
testcase_10 AC 214 ms
91,788 KB
testcase_11 WA -
testcase_12 AC 267 ms
106,716 KB
testcase_13 AC 246 ms
103,300 KB
testcase_14 AC 218 ms
102,112 KB
testcase_15 AC 247 ms
100,300 KB
testcase_16 AC 261 ms
101,644 KB
testcase_17 AC 265 ms
100,456 KB
testcase_18 AC 258 ms
98,356 KB
testcase_19 AC 262 ms
100,716 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 326 ms
94,652 KB
testcase_26 AC 326 ms
94,296 KB
testcase_27 AC 313 ms
94,108 KB
testcase_28 AC 260 ms
96,356 KB
testcase_29 AC 256 ms
97,304 KB
testcase_30 AC 301 ms
105,948 KB
testcase_31 AC 271 ms
96,080 KB
testcase_32 AC 256 ms
95,900 KB
testcase_33 AC 295 ms
104,072 KB
testcase_34 AC 137 ms
79,948 KB
testcase_35 AC 223 ms
83,804 KB
testcase_36 WA -
testcase_37 AC 315 ms
98,004 KB
testcase_38 AC 267 ms
98,664 KB
testcase_39 WA -
testcase_40 AC 313 ms
98,444 KB
testcase_41 AC 307 ms
97,196 KB
testcase_42 AC 327 ms
94,352 KB
testcase_43 AC 235 ms
97,604 KB
testcase_44 AC 224 ms
81,248 KB
testcase_45 AC 226 ms
105,268 KB
testcase_46 AC 226 ms
105,332 KB
testcase_47 AC 135 ms
79,920 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 192 ms
92,976 KB
testcase_51 AC 171 ms
84,412 KB
testcase_52 AC 191 ms
92,408 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] != -1:
            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