[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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步