結果

問題 No.1023 Cyclic Tour
ユーザー rlangevinrlangevin
提出日時 2024-02-03 01:38:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 976 ms / 2,000 ms
コード長 3,046 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 261,760 KB
最終ジャッジ日時 2024-09-28 10:44:04
合計ジャッジ時間 27,213 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,608 KB
testcase_01 AC 39 ms
52,480 KB
testcase_02 AC 39 ms
52,828 KB
testcase_03 AC 38 ms
52,864 KB
testcase_04 AC 257 ms
90,780 KB
testcase_05 AC 277 ms
91,520 KB
testcase_06 AC 265 ms
92,672 KB
testcase_07 AC 283 ms
91,904 KB
testcase_08 AC 230 ms
91,648 KB
testcase_09 AC 445 ms
118,656 KB
testcase_10 AC 426 ms
107,392 KB
testcase_11 AC 457 ms
111,708 KB
testcase_12 AC 455 ms
127,592 KB
testcase_13 AC 423 ms
119,680 KB
testcase_14 AC 417 ms
118,432 KB
testcase_15 AC 427 ms
115,224 KB
testcase_16 AC 976 ms
211,904 KB
testcase_17 AC 967 ms
209,352 KB
testcase_18 AC 847 ms
158,000 KB
testcase_19 AC 790 ms
160,256 KB
testcase_20 AC 416 ms
110,976 KB
testcase_21 AC 426 ms
111,232 KB
testcase_22 AC 482 ms
108,416 KB
testcase_23 AC 518 ms
119,424 KB
testcase_24 AC 541 ms
119,296 KB
testcase_25 AC 422 ms
112,384 KB
testcase_26 AC 418 ms
108,032 KB
testcase_27 AC 385 ms
104,960 KB
testcase_28 AC 542 ms
122,880 KB
testcase_29 AC 558 ms
132,096 KB
testcase_30 AC 529 ms
124,032 KB
testcase_31 AC 591 ms
123,136 KB
testcase_32 AC 657 ms
168,192 KB
testcase_33 AC 652 ms
151,296 KB
testcase_34 AC 153 ms
91,264 KB
testcase_35 AC 260 ms
92,672 KB
testcase_36 AC 466 ms
112,384 KB
testcase_37 AC 608 ms
134,784 KB
testcase_38 AC 558 ms
127,232 KB
testcase_39 AC 557 ms
122,752 KB
testcase_40 AC 564 ms
120,960 KB
testcase_41 AC 565 ms
124,416 KB
testcase_42 AC 343 ms
94,592 KB
testcase_43 AC 229 ms
92,928 KB
testcase_44 AC 315 ms
99,712 KB
testcase_45 AC 588 ms
232,448 KB
testcase_46 AC 671 ms
261,760 KB
testcase_47 AC 165 ms
97,792 KB
testcase_48 AC 246 ms
122,496 KB
testcase_49 AC 267 ms
118,144 KB
testcase_50 AC 225 ms
126,208 KB
testcase_51 AC 173 ms
92,580 KB
testcase_52 AC 180 ms
102,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
class DirectedGraph():
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.rG = [[] for i in range(N)]
        self.order = []
        self.used1 = [0] * N
        self.used2 = [0] * N
        self.group = [-1] * N
        self.label = 0
        
    def add_edge(self, u, v):
        self.G[u].append(v)
        self.rG[v].append(u)
        
    def dfs(self, s):
        self.used1[s] = 1
        for u in self.G[s]:
            if self.used1[u]:
                continue
            self.dfs(u)
        self.order.append(s)
        
    def rdfs(self, s, num):
        self.group[s] = num
        self.used2[s] = 1
        for u in self.rG[s]:
            if self.used2[u]:
                continue
            self.rdfs(u, num)
            
    def scc(self):
        for i in range(self.N):
            if self.used1[i]:
                continue
            self.dfs(i)
        for s in reversed(self.order):
            if self.used2[s]:
                continue
            self.rdfs(s, self.label)
            self.label += 1
        return self.label, self.group
    
    def construct(self):
        nG = [set() for _ in range(self.label)]
        mem = [[] for i in range(self.label)]
        for s in range(self.N):
            now = self.group[s]
            for u in self.G[s]:
                if now == self.group[u]:
                    continue
                nG[now].add(self.group[u])
            mem[now].append(s)
        return nG, mem
    
    
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

    
N, M = map(int, input().split())
G = DirectedGraph(N)
A, B, C = [-1] * M, [-1] * M, [-1] * M
U = UnionFind(N)
for i in range(M):
    A[i], B[i], C[i] = map(int, input().split())
    A[i], B[i] = A[i] - 1, B[i] - 1
    if C[i] == 1:
        if U.is_same(A[i], B[i]):
            print("Yes")
            exit()
        U.union(A[i], B[i])
        
for i in range(M):
    if C[i] == 1:
        continue
    a, b = U.find(A[i]), U.find(B[i])
    if a == b:
        print("Yes")
        exit()
    G.add_edge(a, b)
    
G.scc()
nG, mem = G.construct()
for m in mem:
    if len(m) >= 2:
        print("Yes")
        exit()
        
print("No")
0