[USACO13JAN]Party Invitations【模拟】

Pro

Luogu3068

Sol

这是一篇需要好多容器的题解: v e c t o r q u e u e s e t 。我们用 v e c t o r 存下与 i 有关的集合是多少,用 s e t 存下每一个集合,用 q u e u e 存下被邀请的奶牛。

第一次就是 1 入队,然后循环 v e c t o r ,把循环到的集合中的 1 都删去,判断删去后的集合大小是否为 1 ,如果是 1 ,就入队,重复操作。坑点就是,出来的可能会被重复做,加一个数组判断一下是否已经选了这头奶牛。

Code

#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
using namespace std;

int n , m , ans , vis[1000005];
set<int>s[250005];
vector<int>about[1000005];
queue<int>q;

int main() {
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++) {
        int t;
        scanf("%d",&t);
        for(int j=1; j<=t; j++) {
            int x;
            scanf("%d",&x);
            about[x].push_back(i);
            s[i].insert(x);
        }
    }
    q.push(1);
    vis[1] = 1;
    while(!q.empty()) {
        int now = q.front();
        q.pop();
        ans++;
        for(int i=0; i<about[now].size(); i++) {
            s[about[now][i]].erase(now);
            if(s[about[now][i]].size() == 1 && !vis[*s[about[now][i]].begin()]) {
                int t = *s[about[now][i]].begin();
                q.push(t);
                vis[t] = 1;
            }
        }
    }
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43061009/article/details/82114724
今日推荐