結果

問題 No.1023 Cyclic Tour
ユーザー rlangevinrlangevin
提出日時 2024-02-03 12:59:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,938 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 270,728 KB
最終ジャッジ日時 2024-09-28 10:50:19
合計ジャッジ時間 6,892 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
58,624 KB
testcase_01 AC 43 ms
52,736 KB
testcase_02 AC 43 ms
52,352 KB
testcase_03 AC 43 ms
52,608 KB
testcase_04 AC 274 ms
90,768 KB
testcase_05 AC 281 ms
91,008 KB
testcase_06 AC 304 ms
91,264 KB
testcase_07 AC 291 ms
91,136 KB
testcase_08 AC 240 ms
91,776 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

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]
        seen = [0] * N
        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
                if seen[u]:
                    continue
                seen[u]= 1
                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