Language: C++

#include "bits/stdc++.h"

#define debug(x) cout<<#x<<": "<<x<<endl
#define rep(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef long long ll;

set<int> mergeSet(set<int> s1, set<int> s2) {
	for each (int ele in s2) {
		s1.insert(ele);
	}
	return s1;
}


void solve() {
#ifdef _WIN32
	istream &cin = ifstream("input.txt");
#endif

	int n, m;
	cin >> n >> m;

	vector<set<int>> lang(m);
	rep(i, n) {
		int k, tmp;
		cin >> k;
		rep(j, k) {
			cin >> tmp;
			lang[--tmp].insert(i);
		}
	}

	bool mergeFlag = false;
	set<int> canCommunicate;
	rep(i, m) {
		mergeFlag = false;
		if (canCommunicate.size() == 0 && lang[i].size() > 1) canCommunicate = mergeSet(lang[i], canCommunicate);
		if (lang[i].size() < 2) continue;
		for each (int e in lang[i]) {
			if (canCommunicate.find(e) != canCommunicate.end()) {
				mergeFlag = true;
				break;
			}
		}
		if (mergeFlag) canCommunicate = mergeSet(canCommunicate, lang[i]);
	}


	cout << (canCommunicate.size() == n ? "YES" : "NO") << endl;
}




int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	solve();
#ifdef _WIN32
	system("PAUSE");
#endif
	return 0;
}

Compiler output:
a.cc: In function 'std::set<int> mergeSet(std::set<int>, std::set<int>)':
a.cc:13:13: error: expected '(' before 'each'
   13 |         for each (int ele in s2) {
      |             ^~~~
      |             (
a.cc:13:19: error: expected primary-expression before 'int'
   13 |         for each (int ele in s2) {
      |                   ^~~
a.cc:13:13: error: 'each' was not declared in this scope
   13 |         for each (int ele in s2) {
      |             ^~~~
a.cc:16:9: error: expected primary-expression before 'return'
   16 |         return s1;
      |         ^~~~~~
a.cc:15:10: error: expected ';' before 'return'
   15 |         }
      |          ^
      |          ;
   16 |         return s1;
      |         ~~~~~~
a.cc:16:9: error: expected primary-expression before 'return'
   16 |         return s1;
      |         ^~~~~~
a.cc:15:10: error: expected ')' before 'return'
   15 |         }
      |          ^
      |          )
   16 |         return s1;
      |         ~~~~~~
a.cc:13:13: note: to match this '('
   13 |         for each (int ele in s2) {
      |             ^~~~
a.cc: In function 'void solve()':
a.cc:44:21: error: expected '(' before 'each'
   44 |                 for each (int e in lang[i]) {
      |                     ^~~~
      |                     (
a.cc:44:27: error: expected primary-expression before 'int'
   44 |                 for each (int e in lang[i]) {
      |                           ^~~
a.cc:44:21: error: 'each' was not declared in this scope
   44 |                 for each (int e in lang[i]) {
      |                     ^~~~
a.cc:50:17: error: expected primary-expression before 'if'
   50 |                 if (mergeFlag) canCommunicate = mergeSet(canCommunicate, lang[i]);
      |                 ^~
a.cc:49:18: error: expected ';' before 'if'
   49 |                 }
      |                  ^
      |                  ;
   50 |                 if (mergeFlag) canCommunicate = mergeSet(canCommunicate, lang[i]);
      |                 ~~
a.cc:50:17: error: expected primary-expression before 'if'
   50 |                 if (mergeFlag) canCommunicate = mergeSet(canCommunicate, lang[i]);
      |                 ^~
a.cc:49:18: error: expected ')' before 'if'
   49 |                 }
      |                  ^
      |                  )
   50 |                 if (mergeFlag) canCommunicate = mergeSet(canCommunicate, lang[i]);
      |                 ~~
a.cc:44:21: note: to match this '('
   44 |                 for each (int e in lang[i]) {
      |                     ^~~~
a.cc: In function 'std::set<int> mergeSet(std::set<int>, std::set<int>)':
a.cc:17:1: warning: control reaches end of non-void function [-Wreturn-type]
   17 | }
      | ^