結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-10 22:27:26
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,127 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 843,468 KB
最終ジャッジ日時 2023-10-14 01:13:33
合計ジャッジ時間 11,013 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,432 KB
testcase_01 AC 74 ms
71,304 KB
testcase_02 AC 72 ms
71,380 KB
testcase_03 AC 75 ms
71,504 KB
testcase_04 AC 213 ms
78,996 KB
testcase_05 AC 218 ms
79,424 KB
testcase_06 AC 215 ms
78,888 KB
testcase_07 AC 225 ms
79,368 KB
testcase_08 AC 275 ms
102,004 KB
testcase_09 AC 537 ms
128,840 KB
testcase_10 AC 518 ms
129,924 KB
testcase_11 AC 538 ms
131,328 KB
testcase_12 AC 586 ms
140,192 KB
testcase_13 AC 544 ms
136,084 KB
testcase_14 AC 545 ms
136,088 KB
testcase_15 AC 556 ms
136,388 KB
testcase_16 MLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

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


def kosaraju(edges, reverse_edges):
    import sys
    from operator import itemgetter
    sys.setrecursionlimit(10**7)
    v_count = len(edges)
    order = [0]*v_count
    k = [1]

    def get_order(v):
        order[v] = 1
        for dest in edges[v]:
            if order[dest] == 0:
                get_order(dest)
        order[v] = k[0]
        k[0] += 1

    def get_components(v):
        order[v] = 0
        return [v] + [_v for dest in reverse_edges[v] if order[dest] > 0 for _v in get_components(dest)]

    for v in range(v_count):
        if order[v] == 0:
            get_order(v)

    return [get_components(v) for v, _ in sorted(enumerate(order), key=itemgetter(1), reverse=True) if order[v] > 0]


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)
        rev[par_v].add(par_u)

    res = kosaraju(adj, rev)
    if max(len(sc) for sc in res) > 1:
        print('Yes')
    else:
        print('No')
0