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

洛谷传送门

JDOJ 2343: USACO 2013 Jan Silver 3.Party Invitations

JDOJ传送门

Description

Problem 3: Party Invitations [Travis Hance, 2012]

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).

Input

* 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).

Output

* Line 1: The minimum number of cows FJ can invite to his party.

Sample Input

10 4 2 1 3 2 3 4 6 1 2 3 4 6 7 4 4 3 2 1

Sample Output

4

HINT

INPUT DETAILS:

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

so on.

OUTPUT DETAILS:

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).

题目翻译:

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

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

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

输入:第一行:两个空格分隔的整数:N和G。分别表示奶牛的数量和朋友组的的数量 接下来2到G+1行:每一行描述一群牛。它始于一个整数,表示这个组的大小,接下来每一个数表示一只奶牛的编号(编号在1到n之间,表示这只奶牛在这个朋友组里)

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

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

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

题解:

Code:

#include<cstdio>
#include<set>
#include<queue>
#define ll long long
using namespace std;
const ll N=1e6+5;
struct cs
{
int to,nxt;
}e[N];
int head[N],tot;
bool v[N];
set<int> s[N];
queue<int> q;
int n,m,x,y,ans;
void add(int x,int y)
{
e[++tot].to=y;
e[tot].nxt=head[x];
head[x]=tot;
}
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);
add(y,i);
s[i].insert(y);
}
}
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int k=head[x];k;k=e[k].nxt)
{
s[e[k].to].erase(x);
if(s[e[k].to].size()==1)
{
set<int>::iterator it=s[e[k].to].begin();
if(!v[*it])
v[*it]=1,q.push(*it),ans++;
}
}
}
printf("%d",ans+1);
return 0;
}

USACO Party Invitations的更多相关文章

  1. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  2. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  3. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  4. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  5. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  6. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  7. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

  8. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  9. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

随机推荐

  1. 推荐一款移动端小视频App玲珑视频

    推荐一款移动端小视频App玲珑视频 一 应用描述 玲珑小视频,边看边聊![海量视频,刷个不停,还能找妹子语音聊天哦][随手拍一拍,记录美好生活,还能拿金币哦][看视频领金币.登录领金币.拍视频领金币. ...

  2. fiddler抓包-6-Copy与Save常用技巧

    前言 我们保存会话有什么用呢?很显然就是让开发知道整个接口的入参.出参的情况,不然你截个图我还得一个一个敲或者找回我的接口重新用postman请求一下,我不信会出错. 截图还不如复制粘贴接口url.参 ...

  3. HTTP之缓存是如何保持副本的新鲜的!

    缓存保持副本的新鲜 ========================摘自<HTTP权威指南>================================= 可能不是所有已缓存副本都与服 ...

  4. AtCoder Grand Contest 014

    AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...

  5. 《 .NET并发编程实战》阅读指南 - 第1章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  6. 【微信】微信小程序ISO上wx.scanCode BUG

    ================================================== BUG情况: 小程序在onLoad 主动调用wx.scanCode,安卓手机没有问题.iso调用失 ...

  7. 【Elasticsearch】【WEB】java web服务连接es elasticsearch始终报错,无法正常连接使用的错误解决历程

    前情提要: web服务往华为云上迁移 ================内网的好环境,相关配置=================== 1.web服务关于ES的集群配置如下: elasticAddress ...

  8. (一)RFB协议概述

    (文章是基于刚刚做过的一个项目,对相关知识点进行总结回顾.主要内容均是对之前收集资料的总结概括,很多内容转载自其它地方.因为时间比较长,没有一一记录转载地址,特此感谢!) 一.简介 RFB(远程帧缓冲 ...

  9. 开发工具--搭建python环境

    工具|搭建python环境 实现python2版本与python3版本的环境搭建. 正文 1.Python下载 官网: www.python.org 下载: ( 64位3.5.2Windows x86 ...

  10. Unity Ioc 依赖倒置及Untity AOP被动拦截/自动拦截

    各位博友金安,首先声明这是一篇转载的博客,原文链接:https://www.cnblogs.com/scottpei/archive/2013/01/08/2851087.html 十年河东,十年河西 ...