結果

問題 No.1023 Cyclic Tour
ユーザー rlangevinrlangevin
提出日時 2024-02-03 12:15:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,807 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 131,420 KB
最終ジャッジ日時 2024-09-28 10:49:18
合計ジャッジ時間 22,637 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,608 KB
testcase_01 AC 42 ms
52,608 KB
testcase_02 AC 41 ms
52,608 KB
testcase_03 AC 41 ms
52,864 KB
testcase_04 AC 258 ms
90,624 KB
testcase_05 AC 263 ms
91,136 KB
testcase_06 AC 266 ms
92,076 KB
testcase_07 AC 282 ms
91,136 KB
testcase_08 AC 225 ms
92,416 KB
testcase_09 AC 403 ms
116,888 KB
testcase_10 AC 389 ms
117,496 KB
testcase_11 WA -
testcase_12 AC 360 ms
116,908 KB
testcase_13 AC 384 ms
121,680 KB
testcase_14 AC 386 ms
122,168 KB
testcase_15 AC 398 ms
120,288 KB
testcase_16 AC 492 ms
117,736 KB
testcase_17 AC 469 ms
117,840 KB
testcase_18 AC 449 ms
114,560 KB
testcase_19 AC 420 ms
113,280 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 418 ms
102,616 KB
testcase_26 AC 418 ms
100,732 KB
testcase_27 AC 1,091 ms
106,816 KB
testcase_28 AC 502 ms
124,060 KB
testcase_29 AC 511 ms
131,040 KB
testcase_30 AC 461 ms
116,668 KB
testcase_31 AC 432 ms
116,412 KB
testcase_32 AC 424 ms
112,312 KB
testcase_33 AC 468 ms
113,384 KB
testcase_34 AC 142 ms
91,264 KB
testcase_35 AC 248 ms
92,928 KB
testcase_36 WA -
testcase_37 AC 490 ms
118,588 KB
testcase_38 AC 461 ms
119,168 KB
testcase_39 WA -
testcase_40 AC 492 ms
111,504 KB
testcase_41 AC 489 ms
112,916 KB
testcase_42 AC 321 ms
93,696 KB
testcase_43 AC 216 ms
92,288 KB
testcase_44 AC 323 ms
97,908 KB
testcase_45 AC 332 ms
131,420 KB
testcase_46 AC 197 ms
114,048 KB
testcase_47 AC 177 ms
95,024 KB
testcase_48 WA -
testcase_49 AC 249 ms
105,344 KB
testcase_50 AC 235 ms
110,800 KB
testcase_51 AC 157 ms
92,416 KB
testcase_52 AC 182 ms
105,472 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):
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                stack.append(~u)
                self.used1[u] = 1
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    stack.append(v)
            else:
                u = ~u
                self.order.append(u)
                    
    def rdfs(self, s, num):
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                self.used2[u] = 1
                self.group[u] = num
                for v in self.rG[u]:
                    if self.used2[v]:
                        continue
                    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