結果

問題 No.1023 Cyclic Tour
ユーザー rlangevinrlangevin
提出日時 2024-02-03 11:53:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,899 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 131,812 KB
最終ジャッジ日時 2024-09-28 10:47:20
合計ジャッジ時間 23,345 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,864 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 AC 44 ms
52,864 KB
testcase_03 AC 43 ms
52,864 KB
testcase_04 AC 295 ms
90,752 KB
testcase_05 AC 284 ms
91,008 KB
testcase_06 AC 283 ms
91,648 KB
testcase_07 AC 303 ms
91,264 KB
testcase_08 AC 255 ms
91,904 KB
testcase_09 AC 465 ms
116,232 KB
testcase_10 AC 468 ms
117,584 KB
testcase_11 WA -
testcase_12 AC 421 ms
116,516 KB
testcase_13 AC 430 ms
121,924 KB
testcase_14 AC 424 ms
121,892 KB
testcase_15 AC 424 ms
120,480 KB
testcase_16 AC 540 ms
117,868 KB
testcase_17 AC 531 ms
118,500 KB
testcase_18 AC 526 ms
111,564 KB
testcase_19 AC 504 ms
111,960 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 456 ms
102,996 KB
testcase_26 AC 445 ms
101,944 KB
testcase_27 AC 418 ms
98,840 KB
testcase_28 AC 591 ms
124,704 KB
testcase_29 AC 591 ms
131,116 KB
testcase_30 AC 555 ms
117,148 KB
testcase_31 AC 548 ms
116,024 KB
testcase_32 AC 540 ms
112,848 KB
testcase_33 AC 557 ms
114,968 KB
testcase_34 AC 156 ms
91,392 KB
testcase_35 AC 269 ms
93,184 KB
testcase_36 WA -
testcase_37 AC 561 ms
117,984 KB
testcase_38 AC 535 ms
119,488 KB
testcase_39 WA -
testcase_40 AC 579 ms
111,732 KB
testcase_41 AC 599 ms
112,096 KB
testcase_42 AC 367 ms
93,696 KB
testcase_43 AC 226 ms
92,160 KB
testcase_44 AC 341 ms
98,124 KB
testcase_45 AC 365 ms
131,812 KB
testcase_46 AC 217 ms
113,920 KB
testcase_47 AC 188 ms
94,092 KB
testcase_48 WA -
testcase_49 AC 275 ms
105,344 KB
testcase_50 AC 260 ms
111,032 KB
testcase_51 AC 174 ms
92,288 KB
testcase_52 AC 196 ms
105,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
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 dfs(self, s):
        self.used1[s] = 1
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                stack.append(~u)
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    self.used1[v] = 1
                    stack.append(v)
            else:
                u = ~u
                self.order.append(u)
                    
    def rdfs(self, s, num):
        self.group[s] = num
        self.used2[s] = 1
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                for v in self.rG[u]:
                    if self.used2[v]:
                        continue
                    self.used2[v] = 1
                    self.group[v] = num
                    stack.append(v)
            
    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