洛谷P3068 [USACO13JAN]派对邀请函Party Invitations

P3068 [USACO13JAN]派对邀请函Party Invitations

题目描述

Farmer John is throwing a party and wants to invite some of his cows to show them how much he cares about his herd. However, he also wants to invite the smallest possible number of cows, remembering all too well the disaster that resulted the last time he invited too many cows to a party.

Among FJ's cows, there are certain groups of friends that are hard to separate. For any such group (say, of size k), if FJ invites at least k-1 of the cows in the group to the party, then he must invite the final cow as well, thereby including the entire group. Groups can be of any size and may even overlap with each-other, although no two groups contain exactly the same set of members. The sum of all group sizes is at most 250,000.

Given the groups among FJ's cows, please determine the minimum number of cows FJ can invite to his party, if he decides that he must definitely start by inviting cow #1 (his cows are conveniently numbered 1..N, with N at most 1,000,000).

FJ正在举行派对,并想邀请他的一些奶牛参加以显示FJ多么关心他们,同时,他也希望邀请奶牛的数量最少,有了上一次派对的后果,他不要邀请过多的奶牛参加派对。

在FJ的奶牛中,有一些奶牛不能分开。 对于任何这样的奶牛群,(如果某个群的奶牛数量为k),当FJ邀请组中的至少k-1个奶牛参加派对时,他必须邀请最后的一头奶牛,从而包括整个组。 组可以是任何大小,并且甚至可以彼此重叠,但是没有两个组包含完全相同的奶牛。组的总数<=250,000。

考虑到FJ的牛群中的朋友关系群体的情况下,请你确定FJ可以邀请参加他的派对的奶牛的最小数量,开始前他必须先邀请编号为1的奶牛(为了方便,FJ的的奶牛方编号为1..N,N<=1,000,000)

输入输出格式

输入格式:
  • Line 1: Two space-separated integers: N (the number of cows), and G (the number of groups).

  • Lines 2..1+G: Each line describes a group of cows. It starts with an integer giving the size S of the group, followed by the S cows in the group (each an integer in the range 1..N).

第一行:两个空格分隔的整数:N和G。分别表示奶牛的数量和朋友组的的数量

接下来2到G+1行:每一行描述一群牛。它始于一个整数,表示这个组的大小,接下来每一个数表示一只奶牛的编号(编号在1到n之间,表示这只奶牛在这个朋友组里)

输出格式:
  • Line 1: The minimum number of cows FJ can invite to his party.

共一行,一个数:表示FJ最少可以邀请的牛的数量

输入输出样例

输入样例#1: 复制
10 4 
2 1 3 
2 3 4 
6 1 2 3 4 6 7 
4 4 3 2 1 
输出样例#1: 复制
4 

说明

There are 10 cows and 4 groups. The first group contains cows 1 and 3, and so on.

In addition to cow #1, FJ must invite cow #3 (due to the first group constraint), cow #4 (due to the second group constraint), and also cow #2 (due to the final group constraint).

样例一共10头牛和4组。 第一组包含奶牛1和3,接下来的组也类似

除了奶牛#1,FJ还必须邀请牛#3(由于第一组约束),牛#4(由于第二组约束)和牛#2(由于最终组约束)。

感谢mangoyang 提供翻译

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#define maxn 1000001
using namespace std;
int num,head[maxn];
struct node{int to,pre;}e[maxn];
bool b[maxn];
queue<int>q;
set<int>s[maxn];
int n,m,x,y,ans;
void Insert(int from,int to){
    e[++num].to=to;
    e[num].pre=head[from];
    head[from]=num;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d",&x);
        for(int j=1;j<=x;j++){
            scanf("%d",&y);
            Insert(y,i);
            s[i].insert(y);
        }
    }
    q.push(1);
    while(!q.empty()){
        int now=q.front();q.pop();
        for(int i=head[now];i;i=e[i].pre){
            int to=e[i].to;
            s[to].erase(now);
            if(s[to].size()==1){
                set<int>::iterator it=s[to].begin();
                if(!b[*it])b[*it]=1,q.push(*it),ans++;
            }
        }
    }
    printf("%d",ans+1);
}
原文地址:https://www.cnblogs.com/thmyl/p/7709902.html