洛谷 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最少可以邀请的牛的数量
输入输出样例
10 4
2 1 3
2 3 4
6 1 2 3 4 6 7
4 4 3 2 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 提供翻译
思路:模拟,stl的set维护一下就好。
#include<set>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 250100
#define M 1000100
using namespace std;
set<int>se[N];
queue<int>que;
int n,g;
int vis[M];
int tot,ans;
int to[M],net[M],head[M];
void add(int u,int v){
to[++tot]=v;net[tot]=head[u];head[u]=tot;
}
int main(){
scanf("%d%d",&n,&g);
for(int i=;i<=g;i++){
int opt,x;
scanf("%d",&opt);
for(int j=;j<=opt;j++){
scanf("%d",&x);
add(x,i);
se[i].insert(x);
}
}
que.push();
while(!que.empty()){
int now=que.front();
que.pop();
for(int i=head[now];i;i=net[i]){
se[to[i]].erase(now);
if(se[to[i]].size()==){
set<int>::iterator it=se[to[i]].begin();
if(!vis[*it]){
vis[*it]=;
que.push(*it);
ans++;
}
}
}
}
cout<<ans+;
}
洛谷 P3068 [USACO13JAN]派对邀请函Party Invitations的更多相关文章
- 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
- [luoguP3068] [USACO13JAN]派对邀请函Party Invitations(stl大乱交)
传送门 记录每一个编号在那些组中,可以用vector,这里选择链式前向星. 每一组用set 将被邀请的放到queue中 #include <set> #include <queue& ...
- [洛谷P3697]开心派对小火车
题目:洛谷P3697 题目大意是有各站停列车(慢车,相邻2站时间A)和特急列车(相邻2站时间B),特急列车在特定站点停靠. 现在加一种快速列车(相邻2站时间C,A>C>B),停靠K站(包括 ...
- [洛谷201704R1]开心派对小火车
OJ题号:洛谷P3697 思路: 贪心.首先从起点出发,开特急电车,对于每一个特急车站$s_{i}$,分别下一次车,计算从当前车站$s_{i}$出发坐各停电车在指定时限内$t$最远能够到达的车站$r_ ...
- 洛谷银牛派对SPFA
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...
- 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...
- 洛谷P2202 [USACO13JAN]方块重叠Square Overlap
P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...
- 洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
随机推荐
- 关联对象 AssociatedObject 完全解析
我们在 iOS 开发中经常需要使用分类(Category),为已经存在的类添加属性的需求,但是使用 @property 并不能在分类中正确创建实例变量和存取方法. 不过,通过 Objective-C ...
- 人工智能,你到底是天使or魔鬼?
人工智能的概念早在60多年前就被提出,但又一度沉寂.随着谷歌人工智能程序AlphaGo(阿尔法狗)战胜围棋世界冠军李世石,再次为世人瞩目.然而,与无限风光一起相伴而来的,还有关于人工智能的种种争议! ...
- BZOJ1567 [JSOI2008]Blue Mary的战役地图(二分+二维hash)
题意 问边长为n的两个正方形中最大的相等子正方形.(n<=50) 题解 用到了二维hash,感觉和一维的不太一样. 对于列行有两个不同的进制数然后也是通过类似前缀和的方法差分出一个矩形的hash ...
- CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...
- 洛谷 P1026 统计单词个数 (分组+子串预处理)(分组型dp再次总结)
一看完这道题就知道是划分型dp 有两个点要注意 (1)怎么预处理子串. 表示以i为开头,结尾在j之前(含),有没有子串,有就1,没有就0 (2)dp的过程 这种分成k组最优的题目已经高度模板化了,我总 ...
- 关于多线程lock-free代码
首先要理解JVM内存模型,可以参考我之前的文章. 然后C++里面其实有一样的指令排序的问题.虽然C++11里面针对happens-before规则做了一些语义上面的支持.但是普通C/C++没有做这些支 ...
- springMVC 配置jdbcTemplate连接Oracle数据库出错
springMVC 配置jdbcTemplate连接Oracle数据库出错 错误信息: log4j:WARN No appenders could be found for logger (org.s ...
- 2015百度之星初赛2 1005 序列变换(LIS变形)
LIS(非严格):首先我想到了LIS.然而总认为有点不正确:每一个数先减去它的下标.防止以下的情况发生:(转载) 3 增加序列是1,2,2,2,3,这样求上升子序列是3.也就是要改动2个,可是中间的两 ...
- MySQL 一台主机多实例root登录问题
假设在一台机子上起多个MySQL实例. 比方port号为 3306. 3307. 3308 登录时候要选择不同的 mysql.sock文件 mysql -uroot -p123456 这一句 登录的是 ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...