結果

問題 No.1610 She Loves Me, She Loves Me Not, ...
ユーザー __matyaki____matyaki__
提出日時 2022-03-28 10:23:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,152 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-24 17:06:07
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
11,008 KB
testcase_01 AC 28 ms
11,392 KB
testcase_02 AC 25 ms
11,008 KB
testcase_03 AC 25 ms
11,008 KB
testcase_04 AC 27 ms
11,008 KB
testcase_05 AC 154 ms
12,160 KB
testcase_06 AC 146 ms
12,416 KB
testcase_07 AC 143 ms
12,160 KB
testcase_08 AC 151 ms
12,288 KB
testcase_09 AC 149 ms
12,160 KB
testcase_10 AC 152 ms
12,160 KB
testcase_11 AC 153 ms
12,288 KB
testcase_12 AC 147 ms
12,288 KB
testcase_13 AC 148 ms
12,288 KB
testcase_14 AC 30 ms
11,008 KB
testcase_15 AC 30 ms
11,008 KB
testcase_16 AC 31 ms
11,008 KB
testcase_17 AC 29 ms
11,008 KB
testcase_18 AC 31 ms
10,880 KB
testcase_19 AC 30 ms
11,008 KB
testcase_20 AC 33 ms
11,008 KB
testcase_21 AC 31 ms
11,008 KB
testcase_22 AC 30 ms
11,008 KB
testcase_23 AC 26 ms
11,008 KB
testcase_24 AC 27 ms
11,008 KB
testcase_25 AC 26 ms
11,008 KB
testcase_26 AC 28 ms
11,008 KB
testcase_27 AC 28 ms
11,008 KB
testcase_28 AC 26 ms
11,008 KB
testcase_29 AC 26 ms
11,136 KB
testcase_30 AC 26 ms
11,008 KB
testcase_31 AC 27 ms
11,136 KB
testcase_32 AC 26 ms
11,008 KB
testcase_33 AC 26 ms
11,136 KB
testcase_34 AC 27 ms
11,008 KB
testcase_35 AC 26 ms
11,136 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)


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