結果

問題 No.1023 Cyclic Tour
ユーザー tamatotamato
提出日時 2020-04-10 22:01:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 2,125 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 137,748 KB
最終ジャッジ日時 2023-10-14 00:32:39
合計ジャッジ時間 24,500 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,112 KB
testcase_01 AC 76 ms
71,044 KB
testcase_02 AC 75 ms
71,152 KB
testcase_03 AC 80 ms
71,400 KB
testcase_04 AC 223 ms
89,436 KB
testcase_05 AC 200 ms
89,472 KB
testcase_06 AC 202 ms
90,784 KB
testcase_07 AC 202 ms
89,992 KB
testcase_08 AC 372 ms
119,200 KB
testcase_09 AC 417 ms
118,744 KB
testcase_10 AC 367 ms
118,204 KB
testcase_11 AC 380 ms
120,292 KB
testcase_12 AC 342 ms
127,196 KB
testcase_13 AC 327 ms
121,972 KB
testcase_14 AC 329 ms
120,860 KB
testcase_15 AC 336 ms
125,136 KB
testcase_16 AC 443 ms
137,748 KB
testcase_17 AC 479 ms
135,976 KB
testcase_18 AC 459 ms
131,760 KB
testcase_19 AC 434 ms
133,760 KB
testcase_20 AC 344 ms
113,468 KB
testcase_21 AC 359 ms
114,892 KB
testcase_22 AC 472 ms
117,956 KB
testcase_23 AC 478 ms
119,472 KB
testcase_24 AC 516 ms
130,156 KB
testcase_25 AC 350 ms
115,520 KB
testcase_26 AC 347 ms
110,220 KB
testcase_27 AC 305 ms
105,008 KB
testcase_28 AC 492 ms
128,156 KB
testcase_29 AC 488 ms
130,728 KB
testcase_30 AC 499 ms
126,552 KB
testcase_31 AC 469 ms
123,612 KB
testcase_32 AC 472 ms
128,532 KB
testcase_33 AC 496 ms
126,632 KB
testcase_34 AC 185 ms
93,588 KB
testcase_35 AC 228 ms
92,600 KB
testcase_36 AC 436 ms
115,320 KB
testcase_37 AC 501 ms
124,712 KB
testcase_38 AC 487 ms
128,540 KB
testcase_39 AC 487 ms
119,060 KB
testcase_40 AC 499 ms
123,128 KB
testcase_41 AC 495 ms
120,708 KB
testcase_42 AC 324 ms
108,932 KB
testcase_43 AC 494 ms
127,672 KB
testcase_44 AC 217 ms
95,568 KB
testcase_45 AC 284 ms
137,200 KB
testcase_46 AC 264 ms
137,224 KB
testcase_47 AC 189 ms
102,024 KB
testcase_48 AC 260 ms
106,504 KB
testcase_49 AC 270 ms
107,552 KB
testcase_50 AC 251 ms
105,792 KB
testcase_51 AC 258 ms
107,232 KB
testcase_52 AC 217 ms
104,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.buffer.readline

    def yes():
        print('Yes')
        exit()

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

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

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

    N, M = map(int, input().split())
    adj = [[] for _ in range(N+1)]
    edge1 = []
    edge2 = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c == 1:
            edge1.append((a, b))
        else:
            edge2.append((a, b))
    UF = UnionFind(N)
    for a, b in edge1:
        if UF.isSameGroup(a, b):
            yes()
        else:
            UF.unite(a, b)
    adj_new = [set() for _ in range(N+1)]
    adj_new_rev = [set() for _ in range(N+1)]
    for a, b in edge2:
        ar = UF.find_root(a)
        br = UF.find_root(b)
        adj_new[ar].add(br)
        adj_new_rev[br].add(ar)

    cnt = 0
    st = []
    in_num = [0] * (N+1)
    for v in range(1, N+1):
        if len(adj_new_rev[v]) == 0:
            st.append(v)
        else:
            in_num[v] = len(adj_new_rev[v])
    while st:
        v = st.pop()
        cnt += 1
        for u in adj_new[v]:
            in_num[u] -= 1
            if in_num[u] == 0:
                st.append(u)
    if cnt == N:
        print('No')
    else:
        print('Yes')


if __name__ == '__main__':
    main()
0