Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
650 B
9
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

// 277 A

const int MAXN = 1e2 + 5;

bool vis[MAXN];
vector <int> adj[MAXN];

void dfs(int v)
{
	vis[v] = true;
	for (auto u : adj[v])
		if (!vis[u])
			dfs(u);
}

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		int k;
		cin >> k;
		for (int j = 0; j < k; j++)
		{
			int x;
			cin >> x;
			x--;
			know[x].push_back(i);
		}
	}	
	for (int i = 0; i < m; i++)
	{
		for (auto u : know[i])
			for (auto v : know[i])
				if (u != v)
					adj[u].push_back(v);
	}
	int comp = 0;
	for (int i = 0; i < n; i++)
	{
		if (!vis[i])
		{
			comp++;
			dfs(i);
		}
	}
	cout << comp << endl;
}
Leave a Comment