結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-10 22:55:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,371 ms / 2,000 ms
コード長 2,004 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 99,652 KB
最終ジャッジ日時 2023-10-14 03:50:35
合計ジャッジ時間 48,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,096 KB
testcase_01 AC 19 ms
8,068 KB
testcase_02 AC 18 ms
8,312 KB
testcase_03 AC 17 ms
8,028 KB
testcase_04 AC 225 ms
9,944 KB
testcase_05 AC 215 ms
9,772 KB
testcase_06 AC 225 ms
10,136 KB
testcase_07 AC 219 ms
9,848 KB
testcase_08 AC 643 ms
63,336 KB
testcase_09 AC 803 ms
68,096 KB
testcase_10 AC 773 ms
68,316 KB
testcase_11 AC 817 ms
71,988 KB
testcase_12 AC 862 ms
74,144 KB
testcase_13 AC 824 ms
70,100 KB
testcase_14 AC 732 ms
69,548 KB
testcase_15 AC 818 ms
70,924 KB
testcase_16 AC 1,263 ms
91,680 KB
testcase_17 AC 1,314 ms
93,148 KB
testcase_18 AC 1,371 ms
92,164 KB
testcase_19 AC 1,327 ms
89,696 KB
testcase_20 AC 1,003 ms
80,468 KB
testcase_21 AC 1,021 ms
84,204 KB
testcase_22 AC 1,146 ms
91,432 KB
testcase_23 AC 1,195 ms
93,684 KB
testcase_24 AC 1,331 ms
97,268 KB
testcase_25 AC 1,020 ms
85,148 KB
testcase_26 AC 970 ms
78,544 KB
testcase_27 AC 874 ms
73,208 KB
testcase_28 AC 1,240 ms
95,056 KB
testcase_29 AC 1,227 ms
93,764 KB
testcase_30 AC 1,241 ms
97,216 KB
testcase_31 AC 1,178 ms
93,824 KB
testcase_32 AC 1,342 ms
99,152 KB
testcase_33 AC 1,342 ms
99,652 KB
testcase_34 AC 160 ms
15,832 KB
testcase_35 AC 352 ms
21,972 KB
testcase_36 AC 1,138 ms
88,352 KB
testcase_37 AC 1,199 ms
93,724 KB
testcase_38 AC 1,332 ms
93,132 KB
testcase_39 AC 1,178 ms
90,512 KB
testcase_40 AC 1,182 ms
91,424 KB
testcase_41 AC 1,166 ms
91,332 KB
testcase_42 AC 839 ms
70,636 KB
testcase_43 AC 771 ms
75,984 KB
testcase_44 AC 635 ms
63,676 KB
testcase_45 AC 1,006 ms
91,500 KB
testcase_46 AC 1,136 ms
93,704 KB
testcase_47 AC 603 ms
63,832 KB
testcase_48 AC 887 ms
80,364 KB
testcase_49 AC 887 ms
80,400 KB
testcase_50 AC 884 ms
80,308 KB
testcase_51 AC 751 ms
71,864 KB
testcase_52 AC 767 ms
68,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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 scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N

    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)

    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group


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)

    labels, _ = scc(N, adj, rev)
    if labels < N:
        print('Yes')
    else:
        print('No')
0