n, k = list(map(int, input().split()))
ans = 0
t = []
for _ in range(n):
l = list(map(int, input().split()))
if l[0] == 0:
ans += 1
else:
t += [set(l[1:])]
i = 0
while i < len(t):
for j in t[i + 1:]:
if t[i] & j:
t[i] |= j
t.remove(j)
break
else:
i += 1
print(max(0, len(t) - 1) + ans)
#include <iostream>
using namespace std;
int pre[105],vis[105];
void init(int n)
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int x)
{
if(pre[x]==x) return x;
else return pre[x]=find(pre[x]);
}
int main()
{
int n,m,num,x1,x2,cnt=0,tcnt;
cin>>n>>m;
tcnt=m;
init(m);
for(int i=1;i<=n;i++)
{
cin>>num;
if(num==0) cnt++;
if(num!=0)
{
cin>>x1;
if(vis[x1]==0)
{
vis[x1]=1;tcnt--;
}
}
for(int j=2;j<=num;j++)
{
cin>>x2;
if(vis[x2]==0)
{
vis[x2]=1;tcnt--;
}
pre[find(x1)]=find(x2);
x1=x2;
}
}
for(int i=1;i<=m;i++)
if(pre[i]==i) cnt++;
if(tcnt==m) cout<<n;
else cout<<cnt-1-tcnt;
return 0;
}
566. Reshape the Matrix | 167. Two Sum II - Input array is sorted |
387. First Unique Character in a String | 383. Ransom Note |
242. Valid Anagram | 141. Linked List Cycle |
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |
136. Single Number | 169. Majority Element |
119. Pascal's Triangle II | 409. Longest Palindrome |
1574A - Regular Bracket Sequences | 1574B - Combinatorics Homework |
1567A - Domino Disaster | 1593A - Elections |
1607A - Linear Keyboard | EQUALCOIN Equal Coins |