結果

問題 No.1023 Cyclic Tour
ユーザー chineristACchineristAC
提出日時 2020-10-08 17:01:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 906 ms / 2,000 ms
コード長 2,273 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 85,688 KB
実行使用メモリ 115,840 KB
最終ジャッジ日時 2023-09-27 11:52:57
合計ジャッジ時間 28,918 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,680 KB
testcase_01 AC 92 ms
71,656 KB
testcase_02 AC 94 ms
71,432 KB
testcase_03 AC 94 ms
71,536 KB
testcase_04 AC 536 ms
96,208 KB
testcase_05 AC 493 ms
97,572 KB
testcase_06 AC 496 ms
96,472 KB
testcase_07 AC 576 ms
99,112 KB
testcase_08 AC 454 ms
106,280 KB
testcase_09 AC 432 ms
103,976 KB
testcase_10 AC 458 ms
104,980 KB
testcase_11 AC 457 ms
106,500 KB
testcase_12 AC 350 ms
106,372 KB
testcase_13 AC 342 ms
103,444 KB
testcase_14 AC 333 ms
102,092 KB
testcase_15 AC 341 ms
105,516 KB
testcase_16 AC 405 ms
104,356 KB
testcase_17 AC 405 ms
103,888 KB
testcase_18 AC 426 ms
103,176 KB
testcase_19 AC 411 ms
102,020 KB
testcase_20 AC 898 ms
115,840 KB
testcase_21 AC 901 ms
115,780 KB
testcase_22 AC 637 ms
110,744 KB
testcase_23 AC 616 ms
108,472 KB
testcase_24 AC 539 ms
109,748 KB
testcase_25 AC 867 ms
114,048 KB
testcase_26 AC 819 ms
113,828 KB
testcase_27 AC 814 ms
112,756 KB
testcase_28 AC 540 ms
108,856 KB
testcase_29 AC 478 ms
108,300 KB
testcase_30 AC 522 ms
109,108 KB
testcase_31 AC 531 ms
108,440 KB
testcase_32 AC 501 ms
106,656 KB
testcase_33 AC 544 ms
109,540 KB
testcase_34 AC 332 ms
93,900 KB
testcase_35 AC 716 ms
102,960 KB
testcase_36 AC 782 ms
112,880 KB
testcase_37 AC 643 ms
110,308 KB
testcase_38 AC 480 ms
106,692 KB
testcase_39 AC 646 ms
110,144 KB
testcase_40 AC 643 ms
110,688 KB
testcase_41 AC 638 ms
110,584 KB
testcase_42 AC 906 ms
114,756 KB
testcase_43 AC 486 ms
108,572 KB
testcase_44 AC 547 ms
105,648 KB
testcase_45 AC 298 ms
107,532 KB
testcase_46 AC 247 ms
102,484 KB
testcase_47 AC 251 ms
100,816 KB
testcase_48 AC 363 ms
107,752 KB
testcase_49 AC 362 ms
106,396 KB
testcase_50 AC 351 ms
108,904 KB
testcase_51 AC 361 ms
107,624 KB
testcase_52 AC 344 ms
109,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.edge = [[] for i in range(N)]
        self.deg = [0 for i in range(N)]

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
            self.edge[gy] += self.edge[gx]
            self.edge[gx] = []
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]
            self.edge[gx] += self.edge[gy]
            self.edge[gy] = []

    def add_edge(self,x,y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        self.edge[gx].append(gy)

    def get_size(self, x):
        return self._size[self.find_root(x)]

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

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

    def rebuild(self):
        for i in range(N):
            if self.edge[i]:
                self.edge[i] = [self.find_root(v) for v in self.edge[i]]
                for v in self.edge[i]:
                    self.deg[v] += 1

import sys
from collections import deque

input = sys.stdin.readline

N,M = map(int,input().split())
uf = UnionFindVerSize(N)
uf_un = UnionFindVerSize(N)
for i in range(M):
    a,b,c = map(int,input().split())
    if c==1:
        uf.unite(a-1,b-1)
        if uf_un.is_same_group(a-1,b-1):
            print("Yes")
            exit()
        uf_un.unite(a-1,b-1)
    else:
        uf.add_edge(a-1,b-1)

uf.rebuild()

ans = [v for v in range(N) if uf.deg[v]==0]
deq = deque(ans)
used = [0]*N

while deq:
    v = deq.popleft()
    for nv in uf.edge[v]:
        uf.deg[nv] -= 1
        if uf.deg[nv] == 0:
            deq.append(nv)
            ans.append(nv)

if len(ans)==N:
    print("No")
else:
    print("Yes")
0