結果

問題 No.1023 Cyclic Tour
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-03-31 08:52:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 109,060 KB
最終ジャッジ日時 2024-04-28 02:24:24
合計ジャッジ時間 16,229 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
54,144 KB
testcase_01 AC 34 ms
54,272 KB
testcase_02 AC 33 ms
53,888 KB
testcase_03 AC 33 ms
53,888 KB
testcase_04 AC 209 ms
87,932 KB
testcase_05 AC 218 ms
88,488 KB
testcase_06 AC 216 ms
89,336 KB
testcase_07 AC 234 ms
89,032 KB
testcase_08 AC 206 ms
92,508 KB
testcase_09 AC 205 ms
92,112 KB
testcase_10 AC 214 ms
93,612 KB
testcase_11 AC 220 ms
97,256 KB
testcase_12 AC 191 ms
97,536 KB
testcase_13 AC 173 ms
91,372 KB
testcase_14 AC 177 ms
91,200 KB
testcase_15 AC 193 ms
96,896 KB
testcase_16 AC 239 ms
101,060 KB
testcase_17 AC 239 ms
101,240 KB
testcase_18 AC 252 ms
104,716 KB
testcase_19 AC 237 ms
100,480 KB
testcase_20 AC 353 ms
102,616 KB
testcase_21 AC 352 ms
102,332 KB
testcase_22 AC 291 ms
101,476 KB
testcase_23 AC 296 ms
104,452 KB
testcase_24 AC 287 ms
107,392 KB
testcase_25 AC 320 ms
104,024 KB
testcase_26 AC 310 ms
102,684 KB
testcase_27 AC 312 ms
104,064 KB
testcase_28 AC 272 ms
101,600 KB
testcase_29 AC 260 ms
106,292 KB
testcase_30 AC 289 ms
107,392 KB
testcase_31 AC 277 ms
105,628 KB
testcase_32 AC 278 ms
104,184 KB
testcase_33 AC 299 ms
107,136 KB
testcase_34 AC 200 ms
95,488 KB
testcase_35 AC 244 ms
96,536 KB
testcase_36 AC 299 ms
102,024 KB
testcase_37 AC 303 ms
105,884 KB
testcase_38 AC 264 ms
105,344 KB
testcase_39 AC 312 ms
102,532 KB
testcase_40 AC 301 ms
103,808 KB
testcase_41 AC 316 ms
103,620 KB
testcase_42 AC 339 ms
103,552 KB
testcase_43 AC 262 ms
105,244 KB
testcase_44 AC 233 ms
89,860 KB
testcase_45 AC 147 ms
88,596 KB
testcase_46 AC 135 ms
88,556 KB
testcase_47 AC 146 ms
88,704 KB
testcase_48 AC 228 ms
107,516 KB
testcase_49 AC 235 ms
107,704 KB
testcase_50 AC 230 ms
107,648 KB
testcase_51 AC 231 ms
107,392 KB
testcase_52 AC 236 ms
109,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

n,m = map(int,input().split())
R = [[int(x)-1 for x in input().split()] for i in range(m)]
uf = Unionfind(n)
for a,b,c in R:
    if c == 0:
        if uf.same(a,b):
            print("Yes")
            exit()
        uf.union(a,b)

e = [[] for i in range(n)]
out = [0]*n
for a,b,c in R:
    if c:
        e[uf.find(b)].append(uf.find(a))
        out[uf.find(a)] += 1
q = deque([])
for i in range(n):
    if out[i]:
        continue
    q.append(i)

while q:
    now = q.popleft()
    for nex in e[now]:
        out[nex] -= 1
        if out[nex] == 0:
            q.append(nex)
print("Yes" if sum(out) else "No")

0