Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
people = [None] * N
from collections import defaultdict
lang = defaultdict(set)

for i in range(N):
  people[i] = list(map(int,input().split()))[1:]
  for l in people[i]:
    lang[l].add(i)
    
# 0番の人からたどって全員に行けるか
stack = [0]
seen = [False for i in range(N)]

while stack:
  p = stack.pop()
  if seen[p]:
    continue
  seen[p] = True
  la = people[p] # その人が話せる言語リスト
  for l in la:
    lang[l].remove(p)
    for pe in lang[l]:
      stack.append(pe)
  
for s in seen:
  if not s:
    print("NO")
    break
else:
  print("YES")


Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
people = [None] * N
from collections import defaultdict
lang = defaultdict(set)

for i in range(N):
  people[i] = list(map(int,input().split()))[1:]
  for l in people[i]:
    lang[l].add(i)
    
# 0番の人からたどって全員に行けるか
stack = [0]
seen = [False for i in range(N)]

while stack:
  p = stack.pop()
  if seen[p]:
    continue
  seen[p] = True
  la = people[p] # その人が話せる言語リスト
  for l in la:
    lang[l].remove(p)
    for pe in lang[l]:
      stack.append(pe)
  
for s in seen:
  if not s:
    print("NO")
    break
else:
  print("YES")


Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())

parent=[i for i in range(M)]
depth=[0 for i in range(M)]

def root(x):
  temp=x
  while x!=parent[x]:
    t=x
    x=parent[x]
    parent[t]=x
  parent[temp]=x
  return parent[x]
  
def same(x,y):
  return root(x)==root(y)

def union(x,y):
  x=root(x)
  y=root(y)
  if x==y:
    return
  if depth[x]>depth[y]:
    parent[y]=x
  else:
    parent[x]=y
    if depth[x]==depth[y]:
      depth[x]+=1
      
h=[None]*N
langs=set()
for i in range(N):
  k,*h=map(int,input().split())
  for j in range(k-1):
    langs.add(h[j]-1)
    union(h[j]-1,h[j+1]-1)
  langs.add(h[k-1]-1)
  
roots=set()
for l in langs:
  roots.add(root(l))
  
print(("NO","YES")[len(roots)==1])

Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())

parent=[i for i in range(M)]
depth=[0 for i in range(M)]

def root(x):
  temp=x
  while x!=parent[x]:
    t=x
    x=parent[x]
    parent[t]=x
  parent[temp]=x
  return parent[x]
  
def same(x,y):
  return root(x)==root(y)

def union(x,y):
  x=root(x)
  y=root(y)
  if x==y:
    return
  if depth[x]>depth[y]:
    parent[y]=x
  else:
    parent[x]=y
    if depth[x]==depth[y]:
      depth[x]+=1
      
h=[None]*N
langs=set()
for i in range(N):
  k,*h=map(int,input().split())
  for j in range(k-1):
    langs.add(h[j]-1)
    union(h[j]-1,h[j+1]-1)
  langs.add(h[k-1]-1)
  
roots=set()
for l in langs:
  roots.add(root(l))
  
print(("NO","YES")[len(roots)==1])

Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())

parent=[i for i in range(M)]
depth=[0 for i in range(M)]

def root(x):
  temp=x
  while x!=parent[x]:
    x=parent[x]
  parent[temp]=x
  return parent[x]
  
def same(x,y):
  return root(x)==root(y)

def union(x,y):
  x=root(x)
  y=root(y)
  if x==y:
    return
  if depth[x]>depth[y]:
    parent[y]=x
  else:
    parent[x]=y
    if depth[x]==depth[y]:
      depth[x]+=1
      
h=[None]*N
langs=set()
for i in range(N):
  k,*h=map(int,input().split())
  for j in range(k-1):
    langs.add(h[j]-1)
    union(h[j]-1,h[j+1]-1)
  langs.add(h[k-1]-1)
  
roots=set()
for l in langs:
  roots.add(root(l))
  
print(("NO","YES")[len(roots)==1])

Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())

parent=[i for i in range(M)]
depth=[0 for i in range(M)]

def root(x):
  while x!=parent[x]:
    x=parent[x]
  return parent[x]
  
def same(x,y):
  return root(x)==root(y)

def union(x,y):
  x=root(x)
  y=root(y)
  if x==y:
    return
  if depth[x]>depth[y]:
    parent[y]=x
  else:
    parent[x]=y
    if depth[x]==depth[y]:
      depth[x]+=1
      
h=[None]*N
langs=set()
for i in range(N):
  k,*h=map(int,input().split())
  for j in range(k-1):
    langs.add(h[j]-1)
    union(h[j]-1,h[j+1]-1)
  langs.add(h[k-1]-1)
  
roots=set()
for l in langs:
  roots.add(root(l))
  
print(("NO","YES")[len(roots)==1])

Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())

parent=[i for i in range(M)]
depth=[0 for i in range(M)]

def root(x):
  while x!=parent[x]:
    x=parent[x]
  return parent[x]
  
def same(x,y):
  return root(x)==root(y)

def union(x,y):
  x=root(x)
  y=root(y)
  if x==y:
    return
  if depth[x]>depth[y]:
    parent[y]=x
  else:
    parent[x]=y
    if depth[x]==depth[y]:
      depth[x]+=1
      
h=[None]*N
langs=set()
for i in range(N):
  k,*h=map(int,input().split())
  for j in range(k-1):
    langs.add(h[j]-1)
    union(h[j]-1,h[j+1]-1)
  langs.add(h[k-1]-1)
  
roots=set()
for l in langs:
  roots.add(root(l))
  
print(("NO","YES")[len(roots)==1])

Fixed submission:
# 人も言語もグラフの頂点と見なす
# ある人がある言語を話せる場合連結となる
# 頂点のキーは[言語 or 人][番号(人番号 or 言語番号)]
# 0:言語 1:人

N,M = map(int,input().split())
from collections import defaultdict
E = [[],[]]
E[0] = [[] for i in range(M)]
E[1] = [[] for i in range(N)]

for i in range(N):
  langs = list(map(int,input().split()))[1:]
  for lang in langs:
    E[1][i].append((0,lang-1))
    E[0][lang-1].append((1,i))
    
# 任意の頂点からBFSして、全ての人の頂点が往訪済みになればOK
from collections import deque
q = deque([])
# 人で、0番目からスタート
q.append((1,0))
seen = [[],[]]
seen[0] = [False for i in range(M)]
seen[1] = [False for i in range(N)]

while q:
  kind,v = q.popleft()
  if seen[kind][v]:
    continue
  seen[kind][v] = True
  children = E[kind][v]
  for child in children:
    q.append([child[0],child[1]])
    
for s in seen[1]:
  if not s:
    print("NO")
    break
else:
  print("YES")