結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,352 KB
testcase_01 AC 38 ms
52,864 KB
testcase_02 AC 38 ms
52,864 KB
testcase_03 AC 39 ms
52,864 KB
testcase_04 AC 248 ms
91,008 KB
testcase_05 AC 249 ms
91,040 KB
testcase_06 AC 251 ms
91,520 KB
testcase_07 AC 270 ms
91,264 KB
testcase_08 AC 215 ms
91,776 KB
testcase_09 AC 385 ms
116,240 KB
testcase_10 AC 395 ms
116,860 KB
testcase_11 WA -
testcase_12 AC 364 ms
115,744 KB
testcase_13 AC 376 ms
121,324 KB
testcase_14 AC 372 ms
121,972 KB
testcase_15 AC 375 ms
120,140 KB
testcase_16 AC 471 ms
117,680 KB
testcase_17 AC 506 ms
118,016 KB
testcase_18 AC 477 ms
116,992 KB
testcase_19 AC 453 ms
115,660 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 405 ms
101,436 KB
testcase_26 AC 426 ms
100,428 KB
testcase_27 AC 1,090 ms
105,616 KB
testcase_28 AC 482 ms
124,068 KB
testcase_29 AC 500 ms
130,812 KB
testcase_30 AC 468 ms
116,604 KB
testcase_31 AC 465 ms
116,212 KB
testcase_32 AC 431 ms
112,340 KB
testcase_33 AC 480 ms
113,716 KB
testcase_34 AC 134 ms
91,648 KB
testcase_35 AC 234 ms
92,544 KB
testcase_36 WA -
testcase_37 AC 489 ms
117,472 KB
testcase_38 AC 463 ms
118,708 KB
testcase_39 WA -
testcase_40 AC 498 ms
110,944 KB
testcase_41 AC 493 ms
112,532 KB
testcase_42 AC 317 ms
93,872 KB
testcase_43 AC 203 ms
92,416 KB
testcase_44 AC 313 ms
97,108 KB
testcase_45 AC 347 ms
131,520 KB
testcase_46 AC 190 ms
114,192 KB
testcase_47 AC 165 ms
94,512 KB
testcase_48 WA -
testcase_49 AC 246 ms
105,872 KB
testcase_50 AC 237 ms
110,672 KB
testcase_51 AC 155 ms
92,928 KB
testcase_52 AC 174 ms
104,832 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, s]
        while stack:
            u = stack.pop()
            if u >= 0:                
                self.used1[u] = 1
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    stack.append(~v)
                    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