結果

問題 No.1023 Cyclic Tour
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-29 02:38:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 1,828 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 105,696 KB
最終ジャッジ日時 2024-04-15 02:07:32
合計ジャッジ時間 13,659 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,344 KB
testcase_01 AC 35 ms
53,660 KB
testcase_02 AC 36 ms
53,944 KB
testcase_03 AC 36 ms
53,196 KB
testcase_04 AC 178 ms
77,976 KB
testcase_05 AC 169 ms
77,888 KB
testcase_06 AC 184 ms
78,416 KB
testcase_07 AC 185 ms
78,108 KB
testcase_08 AC 159 ms
87,148 KB
testcase_09 AC 191 ms
94,476 KB
testcase_10 AC 183 ms
94,160 KB
testcase_11 AC 194 ms
97,532 KB
testcase_12 AC 143 ms
94,228 KB
testcase_13 AC 149 ms
94,676 KB
testcase_14 AC 146 ms
93,820 KB
testcase_15 AC 152 ms
94,300 KB
testcase_16 AC 165 ms
92,892 KB
testcase_17 AC 166 ms
92,160 KB
testcase_18 AC 168 ms
92,952 KB
testcase_19 AC 164 ms
91,496 KB
testcase_20 AC 283 ms
101,116 KB
testcase_21 AC 276 ms
102,176 KB
testcase_22 AC 225 ms
100,540 KB
testcase_23 AC 231 ms
99,672 KB
testcase_24 AC 210 ms
99,040 KB
testcase_25 AC 301 ms
101,472 KB
testcase_26 AC 267 ms
99,988 KB
testcase_27 AC 288 ms
99,388 KB
testcase_28 AC 208 ms
100,112 KB
testcase_29 AC 203 ms
100,752 KB
testcase_30 AC 220 ms
101,664 KB
testcase_31 AC 214 ms
99,156 KB
testcase_32 AC 199 ms
96,944 KB
testcase_33 AC 220 ms
101,796 KB
testcase_34 AC 88 ms
79,976 KB
testcase_35 AC 172 ms
82,468 KB
testcase_36 AC 250 ms
101,632 KB
testcase_37 AC 235 ms
97,580 KB
testcase_38 AC 195 ms
98,540 KB
testcase_39 AC 254 ms
98,628 KB
testcase_40 AC 261 ms
99,840 KB
testcase_41 AC 249 ms
100,112 KB
testcase_42 AC 286 ms
88,560 KB
testcase_43 AC 145 ms
92,300 KB
testcase_44 AC 197 ms
92,768 KB
testcase_45 AC 132 ms
96,980 KB
testcase_46 AC 106 ms
92,724 KB
testcase_47 AC 95 ms
91,772 KB
testcase_48 AC 152 ms
105,696 KB
testcase_49 AC 155 ms
102,268 KB
testcase_50 AC 140 ms
105,144 KB
testcase_51 AC 123 ms
83,544 KB
testcase_52 AC 124 ms
88,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

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


def main():
    N, M = map(int, input().split())
    uf = UF_tree(N)
    edge = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        if c == 1:
            if not uf.unite(a, b):
                return True
        else:
            edge.append((a, b))

    G = [[] for _ in range(N)]
    deg = [0] * N
    for a, b in edge:
        a = uf.find(a)
        b = uf.find(b)
        if a == b:
            return True
        deg[b] += 1
        G[a].append(b)

    # トポロジカル順が出来れば閉路なし
    que = []
    for i, d in enumerate(deg):
        if not d:
            que.append(i)
    topo = []
    while que:
        s = que.pop()
        topo.append(s)
        for t in G[s]:
            deg[t] -= 1
            if not deg[t]:
                que.append(t)
    return len(topo) != N


if main():
    print("Yes")
else:
    print("No")
0