結果

問題 No.1023 Cyclic Tour
ユーザー H3PO4H3PO4
提出日時 2021-09-04 09:16:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 842 ms / 2,000 ms
コード長 2,082 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 10,844 KB
実行使用メモリ 60,592 KB
最終ジャッジ日時 2023-08-22 15:52:30
合計ジャッジ時間 31,535 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,788 KB
testcase_01 AC 19 ms
8,720 KB
testcase_02 AC 19 ms
8,792 KB
testcase_03 AC 18 ms
8,700 KB
testcase_04 AC 265 ms
13,868 KB
testcase_05 AC 269 ms
13,736 KB
testcase_06 AC 285 ms
13,996 KB
testcase_07 AC 284 ms
14,060 KB
testcase_08 AC 408 ms
38,072 KB
testcase_09 AC 389 ms
37,248 KB
testcase_10 AC 372 ms
37,472 KB
testcase_11 AC 420 ms
38,676 KB
testcase_12 AC 454 ms
48,948 KB
testcase_13 AC 419 ms
46,744 KB
testcase_14 AC 406 ms
46,508 KB
testcase_15 AC 430 ms
47,468 KB
testcase_16 AC 648 ms
60,040 KB
testcase_17 AC 665 ms
60,592 KB
testcase_18 AC 649 ms
56,788 KB
testcase_19 AC 636 ms
58,516 KB
testcase_20 AC 577 ms
25,944 KB
testcase_21 AC 578 ms
25,940 KB
testcase_22 AC 627 ms
35,572 KB
testcase_23 AC 653 ms
38,388 KB
testcase_24 AC 717 ms
52,476 KB
testcase_25 AC 576 ms
26,076 KB
testcase_26 AC 592 ms
26,864 KB
testcase_27 AC 565 ms
26,204 KB
testcase_28 AC 842 ms
51,168 KB
testcase_29 AC 710 ms
55,220 KB
testcase_30 AC 726 ms
50,164 KB
testcase_31 AC 659 ms
45,236 KB
testcase_32 AC 661 ms
48,852 KB
testcase_33 AC 705 ms
47,540 KB
testcase_34 AC 174 ms
19,372 KB
testcase_35 AC 396 ms
23,884 KB
testcase_36 AC 614 ms
29,848 KB
testcase_37 AC 645 ms
41,884 KB
testcase_38 AC 676 ms
51,888 KB
testcase_39 AC 657 ms
39,740 KB
testcase_40 AC 660 ms
42,664 KB
testcase_41 AC 670 ms
41,260 KB
testcase_42 AC 592 ms
26,296 KB
testcase_43 AC 676 ms
52,040 KB
testcase_44 AC 282 ms
14,000 KB
testcase_45 AC 444 ms
49,916 KB
testcase_46 AC 383 ms
49,824 KB
testcase_47 AC 279 ms
14,232 KB
testcase_48 AC 554 ms
28,636 KB
testcase_49 AC 537 ms
28,732 KB
testcase_50 AC 543 ms
28,196 KB
testcase_51 AC 543 ms
28,668 KB
testcase_52 AC 554 ms
25,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

    def issame(self, x, y):
        return self.find(x) == self.find(y)

    def group_size(self, x):
        return self.size[self.find(x)]

    def group_members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parent) if i == x]

    def group_count(self):
        return len(self.roots())


N, M = map(int, input().split())
directed = []
uf = UnionFind(N)
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c == 1:
        if uf.issame(a, b):
            # 無向辺のみで閉路アリ
            print('Yes')
            exit()
        uf.unite(a, b)
    else:
        # assert c == 2
        directed.append((a, b))

new_nodes_dict = {x: i for i, x in enumerate(uf.roots())}
L = len(new_nodes_dict)
G = [[] for _ in range(L)]
deg = [0] * L
for a, b in directed:
    an = new_nodes_dict[uf.find(a)]
    bn = new_nodes_dict[uf.find(b)]
    deg[bn] += 1
    G[an].append(bn)

d = deque([i for i, x in enumerate(deg) if not x])
while d:
    v = d.popleft()
    for x in G[v]:
        deg[x] -= 1
        if not deg[x]:
            d.append(x)
print('Yes' if max(deg) else 'No')
0