結果

問題 No.1610 She Loves Me, She Loves Me Not, ...
ユーザー __matyaki____matyaki__
提出日時 2022-03-28 10:22:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 78,976 KB
最終ジャッジ日時 2024-04-24 17:04:58
合計ジャッジ時間 4,393 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,888 KB
testcase_01 AC 46 ms
59,776 KB
testcase_02 AC 43 ms
54,272 KB
testcase_03 AC 43 ms
54,528 KB
testcase_04 AC 44 ms
54,656 KB
testcase_05 AC 149 ms
78,544 KB
testcase_06 AC 152 ms
78,540 KB
testcase_07 AC 149 ms
78,452 KB
testcase_08 AC 149 ms
78,464 KB
testcase_09 AC 147 ms
78,300 KB
testcase_10 AC 153 ms
78,976 KB
testcase_11 AC 146 ms
78,592 KB
testcase_12 AC 154 ms
78,556 KB
testcase_13 AC 150 ms
78,852 KB
testcase_14 AC 58 ms
66,588 KB
testcase_15 AC 58 ms
66,688 KB
testcase_16 AC 58 ms
66,176 KB
testcase_17 AC 57 ms
66,048 KB
testcase_18 AC 58 ms
66,176 KB
testcase_19 AC 59 ms
66,560 KB
testcase_20 AC 58 ms
66,176 KB
testcase_21 AC 57 ms
66,176 KB
testcase_22 AC 58 ms
66,176 KB
testcase_23 AC 45 ms
54,656 KB
testcase_24 AC 43 ms
54,528 KB
testcase_25 AC 43 ms
54,016 KB
testcase_26 AC 43 ms
54,272 KB
testcase_27 AC 43 ms
54,528 KB
testcase_28 AC 44 ms
54,272 KB
testcase_29 AC 45 ms
54,528 KB
testcase_30 AC 45 ms
54,784 KB
testcase_31 AC 46 ms
54,784 KB
testcase_32 AC 44 ms
54,400 KB
testcase_33 AC 44 ms
54,260 KB
testcase_34 AC 43 ms
54,784 KB
testcase_35 AC 44 ms
54,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * n

  def find(self, x):#xが属するグループの根を返す
    if self.parents[x] < 0:
      return x
    else:
      self.parents[x] = self.find(self.parents[x])
      return self.parents[x]

  def union(self, x, y):#xのグループとyのグループを結合
    x = self.find(x)
    y = self.find(y)

    if x == y:
      return

    if self.parents[x] > self.parents[y]:
      x, y = y, x

    self.parents[x] += self.parents[y]
    self.parents[y] = x

  def size(self, x):#xの属するグループの要素数を返す
    return -self.parents[self.find(x)]

  def same(self, x, y):#xとyが同じグループに属するかを返す
    return self.find(x) == self.find(y)

  def members(self, x):#xが属するグループの要素をリストで返す
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def roots(self):#すべての根の要素をリストで返す
    return [i for i, x in enumerate(self.parents) if x < 0]

  def group_count(self):#グループ数を返す
    return len(self.roots())

  def all_group_members(self):
    group_members = defaultdict(list)
    for member in range(self.n):
      group_members[self.find(member)].append(member)
    return group_members

  def __str__(self):
    return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

import sys
sys.setrecursionlimit(10**9)

import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

n,m=map(int,input().split())

G=[[] for i in range(n)]

uf=UnionFind(n)

heiro=set()

for i in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  G[a].append(b)
  G[b].append(a)
  if uf.same(a,b):
    heiro.add(a)
    heiro.add(b)
  else:
    uf.union(a,b)

heiro2=list(heiro)

cant=set()

def dfs(now,pre,used):
  global cant
  if pre!=-1 and now in heiro:
    cant=cant|used
    return
  else:
    for nxt in G[now]:
      if nxt!=pre and (now,nxt) not in cant and (nxt,now) not in cant:
        dfs(nxt,now,used|{(now,nxt)})


for edge in heiro2:
  dfs(edge,-1,set())

can=m-len(cant)

if can%2==1:print('Yes')
else:print('No')

    
0