D - Association for Control Over Minds(Kattis - control )

2023-11-13

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 1 1 1 through N N N. “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 1 1 1 through recipe N N N. 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 ≤ 200   000 2 \leq N \leq 200\, 000 2N200000, giving the total number of recipes you have. Thereafter follow N N N lines, the i i i-th line describes recipe number i i i. Each of these lines is a single space separated list of integers. Each of these lines begins with an integer 1 ≤ M 1 \leq M 1M, denoting the number of ingredients required to make this recipe. Then, M M M integers follow, describing the required ingredients. The ingredients are identified by integers between 0 0 0 and 500   000 500\, 000 500000, inclusively, with different integers denoting different ingredients. For each recipe, all of its ingredients will be distinct. The sum of M M M over all recipes will be no greater than 500   000 500\, 000 500000.

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 1 1 1 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 5 5 5, 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
5
2 1 2
2 3 4
2 1 5
5 1 2 3 4 5
2 1 2
Sample Output 1
3
Sample Input 2
3
2 1 2
1 1
1 2
Sample Output 2
1
题意:题目很长,大体的意思就是配药过程,按照顺序配药,每次考虑的时候只需考虑当前情况下能不能配成这种药,如果能的话配药即可,如果不能的话,跳到下一个,配药的规则是如果当前所需的原料没有被使用过直接放入锅中(每种原料只可使用一次),当前的原料部分是之前配过的配方,也可直接拿来使用,下面分析一组样例。
样例 分析
2 1 2 1,2均未被使用过,直接放入ans++
2 3 4 同上 ,ans++
2 1 5 1已经被使用过,无法使用
5 1 2 3 4 5 5 未被使用,直接放入,12 34 分别都在上面配到过,可以使用
2 1 2 1 2 在上面已经被使用过,切当前仅有的配过的配方是12345,
所以不可使用。
题解:结题思路就是带权并查集+STL,我们可以把每次使用的配方都并起来,然后,每次判断的时候如果可以由多个或一个集合合并得到,就让ans++,然后将相应的集合合并。
具体操作:可以将输入的每一组数据中的每一个点的父节点放入一个容器中(这里建议用set,因为set可以自动去除重复节点),这里判断能否合并的方法就是看放到set里的根节点的权(这里指的是并查集合中元素的数量)之和,能否等于输入元素的数量。
这里可以将方法简单的证明一下:
1:
假设有一个父节点的部分元素(而不是全部元素)在集合,则结果是大于输入元素的数量的,例如:
假设当前的并查集中元素父节点:1:3 4 5 6
2:7 8 9
10:11 12
假设输入的元素为1 3 4 5 6 7 10 11 12 ,很明显7 这个并查集合中的元素仅有部分在输入的元素中,那么元素数量和就是大于输入元素数量的,是不对的,回到题意就是7这个元素已经在之前用到过,所以无法进行操作。
这里,明显的还有就是在set集合中的元素数量之和是一定大于等于输入元素的数量的。
2:如果数量等于的话,那么就是符合答案的。
注:这里要注意的是值从0开始,建立并查集的时候注意。
下面是AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define endl '\n'
#define ll long long int
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 7;

int pre[N], s[N];
int find(int x) {
    if (pre[x] == x)
        return x;
    return pre[x] = find(pre[x]);
}//并查集的查找操作,模板
void merge(int a, int b) {
    int p = find(a);
    int q = find(b);
    if (p != q) {
        pre[q] = p;
        s[p] += s[q];
    }
}//并查集的归并操作,这里归并的还有两个并查集元素数量也要归并
set<int> v;//set
signed main() {
    for (int i = 0; i <= N; i++)
        pre[i] = i, s[i] = 1;//初始化操作,从0开始
    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        v.clear();//每一次输入的时候要清空,因为不需要遗留元素
        int m;
        cin >> m;
        for (int j = 1; j <= m; j++) {
            int x;
            cin >> x;
            v.insert(find(x));//将各个元素的父节点放入集合中
        }
        int sum = 0;
        for (auto it = v.begin(); it != v.end(); it++)
            sum += s[*it];//加上父节点的所有元素个数
        if (sum == m) {
            cnt++;
            for (auto it = v.begin(); it != v.end(); it++)
                merge(*v.begin(), *it);//这是遍历容器合并的操作
        }
    }
    cout << cnt << endl;
    return 0;
}

总结:这道题的关键之处在于找到判断是否可以的方法,这里用到的带权并查集还是挺巧妙的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

D - Association for Control Over Minds(Kattis - control ) 的相关文章

  • Office Visio 2013安装

    哈喽 大家好 今天一起学习的是Visio 2013的安装 这是一个绘制流程图的软件 用有效的绘图表达信息 比任何文字都更加形象和直观 Office Visio 是office软件系列中负责绘制流程图和示意图的软件 便于IT和商务人员就复杂信
  • TokuDB性能测试报告

    作者介绍 吴双桥 腾讯云数据库工程师 本文首发腾云阁 TokuDB性能测试报告 一 背景介绍 近年来 TokuDB作为MySQL的大数据 Big Data 存储引擎受到人们的普遍关注 其架构的核心基于一种新的叫做分形树 Fractal Tr
  • 区块链技术概述

    什么是区块链 最通俗易懂的解释 哔哩哔哩 bilibili 区块链是随着比特币等数字加密货币的日益普及而逐渐兴起的一种全新的去中心化基础架构与分布式计算范式 区块链技术具有去中心化 时序数据 集体维护 可编程和安全可信等特点 特别适合构建可
  • 梅克尔树Merkle trees是什么?(以太坊)

    http www btckan com news topic 14827 梅克尔树 Merkle trees 是区块链的基本组成部分 虽说从理论上来讲 没有梅克尔树的区块链当然也是可能的 你只需创建直接包含每一笔交易的巨大区块头 block
  • 研究查阅资料所用到的网站备份

    1 liberary genesis http libgen is 免费下载各种论文 英文原版书 2 semantic scholar https www semanticscholar org 可查询英文论文的影响因子 引用信息 可根据一
  • ES6标准

    ECMAScript 6 0 以下简称 ES6 是 JavaScript 语言的下一代标准 前端es6是模块化开发 es6模块化规范就是浏览器端与服务器端通用的模块化开发规范 其中定义了每一个JavaScript文件都是一个独立的模块 导入
  • mysql 中null和default null,char和varchar,int和integer区别

    default null 和null 区别 default null 指的是 默认值为null int和integer 区别 int和integer 没有区别 char和varchar 区别 char和varchar都是用来存储字符串的 但
  • elementUI-dropdown点击非按钮区域,弹出下拉框

    如下代码 设计到的知识点 dropdown下拉框 点击按钮弹出 点击图片也要弹出 涉及到 js触发按钮点击事件 function load document getElementById target click 一行5个li 随之屏幕的宽

随机推荐

  • redis整体删除,整个hash删除,批量删除,单个删除,正则删除

    对于redis的hash数据结构的删除 pool redis ConnectionPool host 127 0 0 1 port 6381 db 0 decode responses True r redis Redis connecti
  • stm32通过I2C实现温湿度(AHT20)采集

    文章目录 一 环境配置 二 I2C总线通信协议 1 I2C介绍 2 I2C物理层 3 I2C协议层 4 软件IIC和硬件IIC 三 实现AHT20采集程序 1 硬件连接 2 代码实现 四 效果展示 五 总结 六 参考资料 一 环境配置 软件
  • 【LeetCode刷题】 27 移除元素 -java

    题目 给你一个数组 nums 和一个值 val 你需要 原地 移除所有数值等于 val 的元素 并返回移除后数组的新长度 不要使用额外的数组空间 你必须仅使用 O 1 额外空间并 原地 修改输入数组 元素的顺序可以改变 你不需要考虑数组中超
  • Axure插件axure-chrome-extension安装

    chrome浏览器打开axure生成的HTML静态文件页面预览打开如下图显示 这是因为chrome浏览器没有安装Axure插件axure chrome extension导致的 方式一 先下载Axure谷歌浏览器插件 然后在浏览器中添加扩展
  • 使用charles map remote host

    应用场景 a 某个后端Dev在他本地分支有一些代码改动 Bug fix 在未部署的情况下 通过remote map可以提前测试验证其个人分支 b APP进入prod测试阶段 有一些H5页面Prod环境一经部署会直接影响线上用户 因此H5 前
  • 锤子手机系统位置服务器,两种锤子系统安装方法【图文详解】

    很多用安卓手机的人都知道 锤子 系统界面和其他 苹果 和安卓系统的界面是不一样的 锤子 系统界面应用在安卓手机上显示的是重新画的应用图标 整体上还是很好看的 完全比的上苹果系统界面 大家如果看烦了安卓原桌面不防去刷个锤子系统来玩玩 下面我告
  • [python学习笔记] - Pandas的SettingwithCopy分析

    警告信息 当我尝试修改dataframe或者对其赋值时 出现了警告信息 A value is trying to be set on a copy of a slice from a DataFrame Try using loc row
  • grafana导入prometheus

    grafana 简介 grafana是用于可视化大型测量数据的开源程序 他提供了强大和优雅的方式去创建 共享 浏览数据 dashboard中显示了你不同metric数据源中的数据 Grafana是一个开源的 拥有丰富dashboard和图表
  • 3D变形几何体匹配

    文章目录 Halcon 3D匹配之变形几何体匹配 算子说明 1 变形几何体匹配过程中 需要指定参考点 作为变形体匹配参考 2 将示例的形变特征添加到可变性几何体上 3 将刚性几何体转变为可变性几何体 曲面 4 在3D场景中找到一个可变性几何
  • 《C++ primer》练习3.20:输出vector相邻元素的和&输出vector头尾对象的和

    最近看 C primer 有这样一个题目 输出vector相邻元素的和 读入一组整数并把它们存入一个vector对象 将每对相邻整数的和输出出来 这里要注意输入的奇数个和偶数个的数的区别 偶数个整数的话刚好数全部用完 奇数个整数最后一个数空
  • CCNA课程之 交换机划分VLAN

    拓扑 需求 1 设置SW1和SW2的设备名分别为SW1和SW2 2 按拓扑图所示配置PC1 4的IP地址 3 交换机按图示配置各终端所属的相应vlan 并且进行合理的配置使得同vlan间PC可以相互访问 不同vlan间PC不可以相互访问 不
  • 【华为OD统一考试B卷

    在线OJ 本题通过率100 已购买本专栏用户 请私信博主开通账号 在线刷题 运行出现 Runtime Error 0Aborted 请忽略 华为OD统一考试A卷 B卷 新题库说明 2023年5月份 华为官方已经将的 2022 0223Q 1
  • 【HTML】前端必须要知道的html中的meta标签,有哪些属性?

    概览 meta标签一般放在整个html页面的head部分 是在head区域中的一个辅助性标签 不包含任何内容 用于提供有关页面的元信息 比如针对搜索引擎和更新频度的描述和关键词 meta标签的属性定义了与文档相关联的名称 值对 在MDN中对
  • MicroPython基础知识总汇

    MicroPython的系统结构 MicroPython系统的经典结构由三部分组成 分别是微控制器硬件 MicroPython固件 用户程序 MicroPython支持的其它类型开发板 需要自己编译源代码 产生固件 并将固件下载到微控制器中
  • 计量模型、实证stata代码合集,附顶刊示例

    超强整理 计量实证常用代码合集 1 指标说明 包含以下资料 中介效应 三步回归 Sobel检验 Bootstrap自抽样检验 Heckman两阶段回归结果 分组回归 组间系数检验 工具变量回归模型 2SLS模型 调节效应 包含画图分析 中位
  • 结构体与函数

    1 结构体 1 1 为什么有结构体 数组只能存储相同类型数据项的变量 实际生活中一类物体的各个数据参数类型大概率不相同 结构体使我们描述物体更加全面准确 1 2 什么是结构体 结构体是一种用户自定义的可用的数据类型 它允许用户存储不同类型的
  • 阿里云智能编码插件,Cosy文档搜索上新了

    大家好 我们来自阿里云云效代码团队 上一集我们说到 我们的星辰大海是打造最Cosy的开发体验 更早下班 历时一个月我们功能上新了 为了和这样的情况 Say Bye Bye 我们推出了 全新参考文档功能 1 IDE内置社区问答搜索 Cosy侧
  • 2. SQL——DataGrip DML “表 ”中字段数据 更新(修改)与删除

    update student xingx xi set name 傻狗 where id 1 1 将ID为1的字段中的name属性值改为 傻狗 update student xingx xi set name 傻猪 age 3 xb 女 w
  • RISC-V指令集

    1 寄存器 RV32I有32个通用寄存器 以及一个PC寄存器 其中有一个通过硬件设置的值恒为 0 的 x0 寄存器 注 RISC V的32个寄存器x0 x31是用0 31这些数字来表示 2 基础指令 RISC V有六种基本指令格式 每个字段
  • D - Association for Control Over Minds(Kattis - control )

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