传送门
记录每一个编号在那些组中,可以用vector,这里选择链式前向星。
每一组用set
将被邀请的放到queue中
#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000001
using namespace std;
int n, g, cnt, ans;
int head[N], to[N], next[N];
queue <int> q;
set <int> s[N];
set <int> :: iterator it;
bool b[N];
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
}
int main()
{
int i, j, k, x, u, v;
n = read();
g = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= g; i++)
{
k = read();
for(j = 1; j <= k; j++)
{
x = read();
s[i].insert(x);
add(x, i);
}
}
(1);
while(!())
{
ans++;
u = ();
();
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
s[v].erase(u);
it = s[v].begin();
if(s[v].size() == 1 && !b[*it])
{
b[*it] = 1;
(*it);
}
}
}
printf("%d\n", ans);
return 0;
}