結果

問題 No.1023 Cyclic Tour
ユーザー convexineqconvexineq
提出日時 2020-04-10 22:18:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 679 ms / 2,000 ms
コード長 2,368 bytes
コンパイル時間 1,216 ms
コンパイル使用メモリ 86,704 KB
実行使用メモリ 115,284 KB
最終ジャッジ日時 2023-10-14 01:00:06
合計ジャッジ時間 17,722 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,028 KB
testcase_01 AC 72 ms
71,092 KB
testcase_02 AC 71 ms
71,196 KB
testcase_03 AC 71 ms
71,008 KB
testcase_04 AC 176 ms
78,944 KB
testcase_05 AC 178 ms
78,928 KB
testcase_06 AC 178 ms
79,072 KB
testcase_07 AC 198 ms
79,360 KB
testcase_08 AC 241 ms
92,988 KB
testcase_09 AC 245 ms
93,500 KB
testcase_10 AC 236 ms
96,800 KB
testcase_11 AC 271 ms
95,472 KB
testcase_12 AC 267 ms
108,648 KB
testcase_13 AC 275 ms
100,752 KB
testcase_14 AC 231 ms
99,420 KB
testcase_15 AC 264 ms
106,648 KB
testcase_16 AC 260 ms
106,212 KB
testcase_17 AC 292 ms
115,284 KB
testcase_18 AC 288 ms
110,784 KB
testcase_19 AC 299 ms
109,420 KB
testcase_20 AC 293 ms
88,520 KB
testcase_21 AC 289 ms
88,708 KB
testcase_22 AC 329 ms
94,356 KB
testcase_23 AC 342 ms
96,584 KB
testcase_24 AC 357 ms
105,420 KB
testcase_25 AC 294 ms
88,540 KB
testcase_26 AC 282 ms
88,672 KB
testcase_27 AC 679 ms
89,100 KB
testcase_28 AC 348 ms
104,528 KB
testcase_29 AC 305 ms
110,012 KB
testcase_30 AC 345 ms
102,716 KB
testcase_31 AC 328 ms
98,800 KB
testcase_32 AC 350 ms
103,232 KB
testcase_33 AC 358 ms
101,556 KB
testcase_34 AC 154 ms
81,556 KB
testcase_35 AC 218 ms
85,872 KB
testcase_36 AC 329 ms
91,308 KB
testcase_37 AC 346 ms
98,872 KB
testcase_38 AC 320 ms
107,820 KB
testcase_39 AC 355 ms
97,148 KB
testcase_40 AC 329 ms
97,580 KB
testcase_41 AC 337 ms
96,192 KB
testcase_42 AC 287 ms
88,144 KB
testcase_43 AC 356 ms
107,872 KB
testcase_44 AC 201 ms
79,040 KB
testcase_45 AC 210 ms
114,000 KB
testcase_46 AC 196 ms
112,840 KB
testcase_47 AC 169 ms
78,808 KB
testcase_48 AC 270 ms
95,228 KB
testcase_49 AC 258 ms
90,120 KB
testcase_50 AC 242 ms
90,384 KB
testcase_51 AC 256 ms
95,228 KB
testcase_52 AC 267 ms
96,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]


n,m = [int(i) for i in readline().split()]

e1 = []
UF = UnionFind(n)
for _ in range(m):
    a,b,c = [int(i) for i in readline().split()]
    a -= 1
    b -= 1
    if c==1:
        if not UF.merge(a,b):
            print("Yes")
            exit()
    else:
        e1.append((a,b))
        

z = 0
d = {}
for i in range(n):
    if i == UF.root(i):
        d[i] = z
        z += 1

l = len(d)
g = [[] for _ in range(l)]
for a,b in e1:
    g[d[UF.root(a)]].append(d[UF.root(b)])



def find_cycle(g):
    n = len(g)
    used = [0]*n #0:not yet 1: visiting 2: visited
    for v in range(n): #各点でDFS
        if used[v] == 2: continue
        #初期化
        stack = [v]
        hist = [] #履歴
        while stack:
            v = stack[-1]
            if used[v] == 1:
                used[v] = 2 #帰りがけの状態に
                stack.pop()
                hist.pop()
                continue
            hist.append(v)
            used[v] = 1 #行きがけの状態に
            for c in g[v]:
                if used[c] == 2: continue
                elif used[c] == 1: # cを始点とするサイクル発見!
                    return "Yes"
                else:
                    stack.append(c)
    return "No"


print(find_cycle(g))








0