You are the boss of ACM (Association for Control over Minds), an upstanding company with a single goal of world domination.

Yesterday, you woke up, and saw that the weather was clear, and the birds were singing. “Another day, another world domination plan”, you sang to yourself as you devised your next world domination plan involving the illusory mind control potions.

There’s only one insignificant problem you have to solve before you can execute this perfection of a plan: you don’t know the correct recipe for the mind control potion. You asked the local Panda-breed brewmaster for the recipe, but unfortunately he didn’t know either. Instead, he gave you the mysterious tome titled The Root of all Evil (written by Halim the White). You read the evil book under candle light, and wrote down all the potion recipes contained inside the book. “One of them must be the formula for the mind control potion, I’m sure of it!”, you said to yourself. You numbered these recipes from 11 through NN. “I just need to try concocting all of these recipes!”, you hummed to yourself.

Today, you woke up, and saw that the weather was clear, and…, anyway. You have purchased all the utensils and ingredients from the local grocery — onion, carrot, broom, vials, cauldrons, bat wings, …, all common grocery items. Now, you are ready to begin your experiments, but then you notice that some of the recipes share common ingredients! Unfortunately, you only bought one of each ingredient with you. “Oh no! What should I do now?!”, you panicked.

“I’ll just create some of the potions today, and do the remaining ones later.”, you resolved. You consider all your recipes one by one in order by their number from recipe 11 through recipe NN. For each recipe, if you are not able to concoct this potion (explained in the next paragraph), you skip this recipe, and consider the next one, if any. Otherwise, even though it may cause some of the next potions to no longer be concoctable, you concoct this recipe. Thus, whether to concoct a potion is not a choice. It’s simply determined by whether it is possible or not to do so when you consider the potion.

In order to concoct a potion, you first prepare a new empty cauldron (you managed to procure an infinite number of cauldrons from the grocery store). Then, you combine all of the ingredients required for this potion and nothing else in this cauldron (that is, the cauldron cannot contain any ingredients not listed in the recipe). For the ingredients that have not been used for any of the previous potions that you’ve decided to concoct, you can simply put it into this cauldron. You can also pour the entire content of the cauldron used for one of your previous concoctions into this cauldron, thus mixing in all of the ingredients contained inside the cauldron (since you pour all of the content of the cauldron, this previous concoction can no longer be used for any of your next concoctions). Finally, you stir this cauldron with your broom and take a vial of the concoction to test on your minions later. The remaining concoction remains in this cauldron, and can be poured into another cauldron later.

“What is the number of recipes you will concoct this way today?”, you asked yourself.

Input

The first line contains a non-negative integer 2≤N≤2000002≤N≤200000, giving the total number of recipes you have. Thereafter follow NN lines, the ii-th line describes recipe number ii. Each of these lines is a single space separated list of integers. Each of these lines begins with an integer 1≤M1≤M, denoting the number of ingredients required to make this recipe. Then, MM integers follow, describing the required ingredients. The ingredients are identified by integers between 00 and 500000500000, inclusively, with different integers denoting different ingredients. For each recipe, all of its ingredients will be distinct. The sum of MM over all recipes will be no greater than 500000500000.

Output

Print the number of recipes you will concoct.

Sample Data Explanation

In the first example, the first potion can be concocted, since both ingredients were not used so far. Thus, you will concoct this potion. The second potion will also be concocted for the same reason. The third potion cannot be concocted, since ingredient 11 is no longer present, and is inside a cauldron mixed with another ingredient not present in the recipe. The fourth potion can be concocted by pouring the content of the cauldrons used for the first and second concoctions, and then adding ingredient 55, which has remained unused so far. The last potion cannot be concocted, since the content of the cauldron for the first potion has all been poured when making the fourth potion and thus is now mixed with other ingredients not present in the recipe.

For the second example, since the first potion can be concocted, it has to be concocted. Thus, the second and third potions can no longer be concocted.

Sample Input 1 Sample Output 1
5
2 1 2
2 3 4
2 1 5
5 1 2 3 4 5
2 1 2
3
Sample Input 2 Sample Output 2
3
2 1 2
1 1
1 2
1

题解:题目给出来n个食谱,我们从上往下遍历食谱,若能按照要求制造出该食物,则答案加1,且该食物的所有分量都成为一个联通块,否则跳过该食谱。

我们可以用以前用过的材料(包含该材料的连通块!!!),但是要保证不能有多余的材料出现,所以我们可以用STL中的vector容器,首先放入每种材料的”根“,然后排序去重,统计每个连通块的总数量,

看是否等于给出来的M。如果相等,那么答案加1,并且合并该连通块中的分量,否则跳过该食谱。

#include<bits/stdc++.h>
using namespace std;
vector<int>v;
const int maxn=;
int f[maxn],num[maxn];
int Find(int x)
{
return f[x]==x?x:f[x]=Find(f[x]);
}
void Merge(int u,int v)
{
u=Find(u);
v=Find(v);
if(u!=v){
num[u]+=num[v];
f[v]=u;
}
}
int main()
{
ios::sync_with_stdio();
for(int i=;i<=maxn;i++){//从0开始!!!
f[i]=i;
num[i]=;
}
int n,ans=;
cin>>n;
for(int i=;i<=n;i++){
int k;
cin>>k;
v.clear();
for(int j=;j<=k;j++){
int x;
cin>>x;
v.push_back(Find(x));//放进去的是x的父节点
}
sort(v.begin(),v.end());//排序去重
v.erase(unique(v.begin(),v.end()),v.end());
int Size=v.size();
int sum=;
for(int j=;j<Size;j++){
sum+=num[v[j]];
}
if(sum==k){//看是否每一块加起来是否正好可以组成该块
ans++;
for(int j=;j<Size;j++){
Merge(v[j],v[]);
}
}
}
cout<<ans<<endl;
return ;
}

D - Association for Control Over Minds Kattis - control (并查集+STL)的更多相关文章

  1. mfc中Button、Edit Control和MFC EditBrowse Control的用法

    [前(fei)言(hua)] 写LL(1)分析器被CString转string卡了一个多小时也是醉了. 趁着还算清醒写下这次用到的控件的使用方法好了. 这次实验的mfc用到了四个控件:Edit Con ...

  2. (WPF) 再议binding:点击User Control时,User Control变换颜色或做其他的处理。

    Binding 是前台UI(显示层)和后台代码(数据层)的桥梁.理论上当后台的数据变动时,显示的数据或样式应该随之而变.这些是动态的. 对于Binding的设置可以在前台Xaml,也可以在后台Code ...

  3. angular form set dynamic control(form动态设置control)

    实现效果 form表单控件的实时更新 效果如图 关键代码 validateForm: FormGroup; // 表单校验 constructor( private fb: FormBuilder ) ...

  4. control+Z的逆 control+Y

    接触过电脑的朋友一定知道control键加Z可以在大多时候撤销我们前一步的操作,相当于计算机系统里最“广谱”的后悔药. 然而,你有没有在编辑文本的时候却因为撤销的操作而后悔?输入文本之后撤销,发现少了 ...

  5. Kattis - flippingcards 【并查集】

    题意 给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的 思路 将每一对数字 连上一条边 然后 最后 判断每一个连通块里面 边的个数 是否 大 ...

  6. Kattis - Virtual Friends(并查集)

    Virtual Friends These days, you can do all sorts of things online. For example, you can use various ...

  7. Red Gate - SQL Source Control实现对SQL SERVER 的源代码控制

    原文地址:http://bbs.csdn.net/topics/350165431 SQL Server 一直没有一款很好的源码控制器,之前自己曾尝试自己写一个,将所有的 脚本 自动生成到某一目录下, ...

  8. Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程

    原文:Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列之二 SQL Source Co ...

  9. Optimistic Concurrency VS. Pessimistic Concurrency Control

    原创地址:http://www.cnblogs.com/jfzhu/p/4009918.html 转载请注明出处   (一)为什么需要并发控制机制 并发控制机制是为了防止多个用户同时更改同一条数据,也 ...

随机推荐

  1. POJ - 3661 Running(dp---背包)

    题意:Bessie要运动N分钟,已知每一分钟可以跑的距离,每一分钟可选择跑或者不跑,若选择跑,疲劳度加1,但疲劳度不能超过M:若选择不跑,则每过一分钟,疲劳度减1,且只有当疲劳度减为0时可以继续跑.求 ...

  2. 68.ORM查询条件:date,time,year,week_day等

    1. date: 首先查看数据库中article表的信息,由表中的create_time字段可以看出时间为2020.2.5 打印出查询的结果: <QuerySet []>:但是查询的结果为 ...

  3. 【每日Scrum】第二天冲刺

    一.计划会议内容 确定细化了每日任务 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图  

  4. .equal()和==的区别

    1.首先,equal和==最根本的区别在于equal是一个方法,而==是一个运算符. 2.一般来说,==运算符比较的是在内存中的物理地址,.equal()比较的是哈希算法值是否相等(即hashcode ...

  5. 干货 | MySQL数据库安全之审计

    干货 | MySQL数据库安全之审计 原创: 李勇 京东云开发者社区  今天 每家公司都希望业务高速增长,最好能出几个爆款产品或者爆款业务,从而带动公司营收高速攀升.但站在数据库管理员的角度,这却是实 ...

  6. bootstrap 网格

    实现原理 网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Bootst ...

  7. Django1.11模型类数据库操作

    django模型类数据库操作 数据库操作 添加数据 1,创建类对象,属性赋值添加 book= BookInfo(name='jack',pub_date='2010-1-1') book.save() ...

  8. Python说文解字_杂谈06

    1. 序列类型的分类: 容器类型:list.tuple,deque 扁平序列:str.bytes.bytearray.array.array 可变序列:list.dequte.bytearray.ar ...

  9. Hibernate(三)--关联映射

    1.多对一 product----category category.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibern ...

  10. Aras Innovator时间验证

    //方法名:bcs_Nexteer_CheckTime //功能描述:开始和结束日期对比 //原作者:joe //创建时间:20141226 //版权所有(C)JOE.FAN //debugger; ...