結果

問題 No.2202 贅沢てりたまチキン
ユーザー roarisroaris
提出日時 2023-02-03 21:51:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 581 ms / 2,000 ms
コード長 1,666 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 114,760 KB
最終ジャッジ日時 2023-09-15 17:35:07
合計ジャッジ時間 6,851 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,672 KB
testcase_01 AC 89 ms
71,708 KB
testcase_02 AC 89 ms
71,740 KB
testcase_03 AC 89 ms
71,844 KB
testcase_04 AC 89 ms
71,424 KB
testcase_05 AC 91 ms
71,744 KB
testcase_06 AC 89 ms
71,800 KB
testcase_07 AC 86 ms
71,560 KB
testcase_08 AC 89 ms
71,776 KB
testcase_09 AC 88 ms
71,640 KB
testcase_10 AC 127 ms
114,760 KB
testcase_11 AC 87 ms
71,848 KB
testcase_12 AC 89 ms
71,576 KB
testcase_13 AC 87 ms
71,728 KB
testcase_14 AC 242 ms
105,052 KB
testcase_15 AC 248 ms
105,092 KB
testcase_16 AC 256 ms
114,228 KB
testcase_17 AC 260 ms
114,156 KB
testcase_18 AC 185 ms
96,620 KB
testcase_19 AC 192 ms
113,448 KB
testcase_20 AC 265 ms
109,324 KB
testcase_21 AC 268 ms
109,328 KB
testcase_22 AC 211 ms
91,160 KB
testcase_23 AC 321 ms
92,260 KB
testcase_24 AC 274 ms
95,476 KB
testcase_25 AC 581 ms
108,696 KB
testcase_26 AC 262 ms
105,488 KB
testcase_27 AC 251 ms
105,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N, M = map(int, input().split())
G = [[] for _ in range(N)]
uf = Unionfind(N)

for _ in range(M):
    u, v = map(int, input().split())
    G[u-1].append(v-1)
    G[v-1].append(u-1)
    uf.unite(u-1, v-1)

rs = set(uf.root(v) for v in range(N))
color = [0]*N

for r in rs:
    q = deque([r])
    color[r] = 1
    odd_cycle = False
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if color[nv]==0:
                color[nv] = -color[v]
                q.append(nv)
            else:
                if color[v]==color[nv]:
                    odd_cycle = True
    
    if not odd_cycle:
        exit(print('No'))

print('Yes')
0