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

传送门

 

记录每一个编号在那些组中,可以用vector,这里选择链式前向星。

每一组用set

将被邀请的放到queue中

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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);
        }
    }
    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;
}

  

posted @   zht467  阅读(181266)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示