您的位置:首页 > 其它

[luoguP3068] [USACO13JAN]派对邀请函Party Invitations(stl大乱交)

2017-08-16 14:48 267 查看

传送门

 

记录每一个编号在那些组中,可以用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
, to
, next
;
queue <int> q;
set <int> s
;
set <int> :: iterator it;
bool b
;

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);
}
}
q.push(1);
while(!q.empty())
{
ans++;
u = q.front();
q.pop();
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;
q.push(*it);
}
}
}
printf("%d\n", ans);
return 0;
}

  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: