結果

問題 No.1023 Cyclic Tour
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-03-31 08:46:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,346 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 107,904 KB
最終ジャッジ日時 2024-04-28 02:17:52
合計ジャッジ時間 17,172 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
54,016 KB
testcase_01 AC 37 ms
54,064 KB
testcase_02 AC 39 ms
53,944 KB
testcase_03 AC 35 ms
54,144 KB
testcase_04 AC 229 ms
88,016 KB
testcase_05 AC 258 ms
88,480 KB
testcase_06 AC 239 ms
89,464 KB
testcase_07 AC 261 ms
89,152 KB
testcase_08 AC 195 ms
92,856 KB
testcase_09 AC 218 ms
93,336 KB
testcase_10 AC 204 ms
94,720 KB
testcase_11 WA -
testcase_12 AC 229 ms
96,976 KB
testcase_13 AC 205 ms
95,500 KB
testcase_14 AC 191 ms
91,648 KB
testcase_15 AC 208 ms
96,252 KB
testcase_16 AC 248 ms
101,376 KB
testcase_17 AC 238 ms
100,560 KB
testcase_18 AC 252 ms
97,536 KB
testcase_19 AC 238 ms
100,180 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 328 ms
101,588 KB
testcase_26 AC 340 ms
102,936 KB
testcase_27 AC 329 ms
100,988 KB
testcase_28 AC 265 ms
101,720 KB
testcase_29 AC 235 ms
97,640 KB
testcase_30 AC 276 ms
101,632 KB
testcase_31 AC 259 ms
100,096 KB
testcase_32 AC 243 ms
97,408 KB
testcase_33 AC 267 ms
101,612 KB
testcase_34 AC 214 ms
95,488 KB
testcase_35 AC 266 ms
96,848 KB
testcase_36 WA -
testcase_37 AC 289 ms
100,720 KB
testcase_38 AC 260 ms
100,028 KB
testcase_39 WA -
testcase_40 AC 305 ms
100,944 KB
testcase_41 AC 306 ms
100,984 KB
testcase_42 AC 345 ms
102,652 KB
testcase_43 AC 247 ms
99,872 KB
testcase_44 AC 243 ms
89,736 KB
testcase_45 AC 176 ms
88,704 KB
testcase_46 AC 158 ms
88,744 KB
testcase_47 AC 195 ms
91,648 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 262 ms
107,456 KB
testcase_51 AC 230 ms
103,704 KB
testcase_52 AC 246 ms
107,648 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)]
for a,b,c in R:
    if c:
        e[uf.find(a)].append(uf.find(b))
use = [-1]*n
for i in range(n):
    if use[i] != -1:
        continue
    use[i] = i
    q = deque([i])
    while q:
        now = q.pop()
        for nex in e[now]:
            if use[nex] == i:
                print("Yes")
                exit()
            if use[nex] == -1:
                use[nex] = i
                q.append(nex)
print("No")
0