JZOI1062 【USACO2013JAN】invite

#include <bits/stdc++.h>
#define ll long long
#define INF 2147483647
#define ll_INF 9223372036854775807
using namespace std;
inline int read() {
	int x = 0,tmp = 1;char ch = getchar();
	while( ch < '0' || ch > '9' ) {if ( ch == '-' ) tmp = -1; ch = getchar();}
	while( ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar(); }
	return x * tmp;
}
multiset< int >_group[25100];
queue< int > Q;
bool _flag[1100000];
int main() {
	memset( _flag, 0, sizeof( _flag ) );
	int N = read(), G = read();
	for( int i = 1 ; i <= G ; ++ i ) {
		int k = read();
		for( int j = 1 ; j <= k; ++ j ) _group[i].insert( read() );
	}
	Q.push( 1 );
	int tot = 1;
	_flag[1] = 1;
	while( !Q.empty() ) {
		int _now = Q.front();
		Q.pop();
		for( int i = 1 ; i <= G ; ++ i ) if( !_group[i].empty() ) {
			auto _where = _group[i].lower_bound( _now );
			if( *_where != _now ) continue; else _group[i].erase( _where );
			if( _group[i].size() == 1 ) {
				auto the_only = _group[i].begin();
				Q.push( *the_only );
				if( !_flag[(int)*the_only] ) tot ++, _flag[(int)*the_only] = 1;
				_group[i].erase( *the_only );
			}
		}
	}
	printf( "%d\n", tot );

	return 0;
}
posted @ 2017-03-28 19:14  ARZhu  阅读(176)  评论(0编辑  收藏  举报