結果

問題 No.1023 Cyclic Tour
ユーザー ptotqptotq
提出日時 2020-04-13 22:25:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 789 ms / 2,000 ms
コード長 1,757 bytes
コンパイル時間 745 ms
コンパイル使用メモリ 11,928 KB
実行使用メモリ 65,340 KB
最終ジャッジ日時 2023-10-26 03:06:04
合計ジャッジ時間 31,285 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,128 KB
testcase_01 AC 27 ms
10,128 KB
testcase_02 AC 27 ms
10,128 KB
testcase_03 AC 28 ms
10,128 KB
testcase_04 AC 206 ms
11,632 KB
testcase_05 AC 209 ms
11,632 KB
testcase_06 AC 220 ms
11,632 KB
testcase_07 AC 210 ms
11,632 KB
testcase_08 AC 422 ms
44,560 KB
testcase_09 AC 424 ms
44,420 KB
testcase_10 AC 419 ms
44,352 KB
testcase_11 AC 446 ms
46,572 KB
testcase_12 AC 491 ms
49,112 KB
testcase_13 AC 456 ms
46,844 KB
testcase_14 AC 452 ms
46,480 KB
testcase_15 AC 462 ms
47,464 KB
testcase_16 AC 706 ms
61,844 KB
testcase_17 AC 731 ms
62,812 KB
testcase_18 AC 674 ms
62,124 KB
testcase_19 AC 640 ms
60,596 KB
testcase_20 AC 613 ms
51,304 KB
testcase_21 AC 642 ms
52,952 KB
testcase_22 AC 681 ms
58,256 KB
testcase_23 AC 705 ms
60,520 KB
testcase_24 AC 789 ms
65,340 KB
testcase_25 AC 786 ms
53,568 KB
testcase_26 AC 592 ms
50,168 KB
testcase_27 AC 540 ms
47,520 KB
testcase_28 AC 745 ms
63,700 KB
testcase_29 AC 748 ms
64,240 KB
testcase_30 AC 753 ms
64,672 KB
testcase_31 AC 693 ms
60,676 KB
testcase_32 AC 689 ms
62,364 KB
testcase_33 AC 749 ms
64,224 KB
testcase_34 AC 155 ms
17,452 KB
testcase_35 AC 337 ms
23,568 KB
testcase_36 AC 673 ms
55,884 KB
testcase_37 AC 681 ms
58,808 KB
testcase_38 AC 703 ms
61,644 KB
testcase_39 AC 701 ms
59,064 KB
testcase_40 AC 715 ms
59,948 KB
testcase_41 AC 716 ms
59,764 KB
testcase_42 AC 535 ms
50,104 KB
testcase_43 AC 492 ms
57,064 KB
testcase_44 AC 308 ms
36,176 KB
testcase_45 AC 487 ms
48,828 KB
testcase_46 AC 409 ms
48,828 KB
testcase_47 AC 299 ms
35,528 KB
testcase_48 AC 539 ms
52,116 KB
testcase_49 AC 529 ms
51,984 KB
testcase_50 AC 535 ms
51,880 KB
testcase_51 AC 471 ms
51,984 KB
testcase_52 AC 485 ms
48,836 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


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)]
    indeg = [0]*N
    outdeg = [0]*N
    for u, v in edges:
        par_u, par_v = uf.find(u), uf.find(v)
        if par_u == par_v:
            print('Yes')
            exit()
        if par_v not in adj[par_u]:
            adj[par_u].add(par_v)
            outdeg[par_u] += 1
            indeg[par_v] += 1

    stack = [i for i in range(N) if outdeg[i] and indeg[i] == 0]

    while stack:
        v = stack.pop()
        for dest in adj[v]:
            if indeg[dest] == 0:
                continue
            indeg[dest] -= 1
            if indeg[dest] == 0:
                stack.append(dest)

    if max(indeg) > 0:
        print('Yes')
    else:
        print('No')
0