結果

問題 No.1023 Cyclic Tour
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-29 02:19:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,855 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 93,056 KB
最終ジャッジ日時 2024-04-15 01:53:27
合計ジャッジ時間 14,055 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 KB
testcase_01 AC 41 ms
52,224 KB
testcase_02 AC 40 ms
52,224 KB
testcase_03 AC 41 ms
52,608 KB
testcase_04 AC 198 ms
78,208 KB
testcase_05 AC 186 ms
77,952 KB
testcase_06 AC 196 ms
78,644 KB
testcase_07 AC 204 ms
77,552 KB
testcase_08 AC 172 ms
86,272 KB
testcase_09 AC 190 ms
89,192 KB
testcase_10 AC 161 ms
84,864 KB
testcase_11 WA -
testcase_12 AC 185 ms
91,904 KB
testcase_13 AC 166 ms
88,576 KB
testcase_14 AC 137 ms
88,416 KB
testcase_15 AC 167 ms
88,636 KB
testcase_16 AC 166 ms
91,296 KB
testcase_17 AC 165 ms
91,204 KB
testcase_18 AC 171 ms
92,288 KB
testcase_19 AC 160 ms
90,792 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 311 ms
88,576 KB
testcase_26 AC 279 ms
87,548 KB
testcase_27 AC 295 ms
88,132 KB
testcase_28 AC 180 ms
88,436 KB
testcase_29 AC 168 ms
90,560 KB
testcase_30 AC 197 ms
87,824 KB
testcase_31 AC 196 ms
87,768 KB
testcase_32 AC 176 ms
88,708 KB
testcase_33 AC 220 ms
93,056 KB
testcase_34 AC 100 ms
80,000 KB
testcase_35 AC 198 ms
82,176 KB
testcase_36 WA -
testcase_37 AC 241 ms
89,252 KB
testcase_38 AC 178 ms
91,736 KB
testcase_39 WA -
testcase_40 AC 266 ms
90,368 KB
testcase_41 AC 245 ms
88,832 KB
testcase_42 AC 312 ms
87,552 KB
testcase_43 AC 166 ms
91,264 KB
testcase_44 AC 211 ms
79,464 KB
testcase_45 AC 156 ms
92,096 KB
testcase_46 AC 147 ms
92,396 KB
testcase_47 AC 97 ms
78,976 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 150 ms
92,160 KB
testcase_51 AC 137 ms
83,968 KB
testcase_52 AC 143 ms
87,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


def main():
    N, M = map(int, input().split())
    uf = UF_tree(N)
    edge = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        if c == 1:
            if not uf.unite(a, b):
                return True
        else:
            edge.append((a, b))

    G = [[] for _ in range(N)]
    for a, b in edge:
        a = uf.find(a)
        b = uf.find(b)
        if a == b:
            return True
        G[a].append(b)

    group = [-1] * N
    g = 0
    for i in range(N):
        if group[i] != -1:
            continue
        group[i] = g
        que = [i]
        while que:
            s = que.pop()
            for t in G[s]:
                if group[t] == g:
                    return True
                if group[t] == -1:
                    group[t] = g
                    que.append(t)
        g += 1
    return False


if main():
    print("Yes")
else:
    print("No")
0