Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)
链接:
https://codeforces.com/contest/1263/problem/D
题意:
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of n passwords — strings, consists of small Latin letters.
Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:
two passwords a and b are equivalent if there is a letter, that exists in both a and b;
two passwords a and b are equivalent if there is a password c from the list, which is equivalent to both a and b.
If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.
For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if:
admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab";
admin's password is "d", then you can access to system by using only "d".
Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
思路:
set记录每个字符出现的字符串。
遍历一边并查集维护
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int Fa[MAXN];
string s[MAXN];
set<int> vec[26];
int n;
int GetF(int x)
{
if (Fa[x] == x)
return Fa[x];
Fa[x] = GetF(Fa[x]);
return Fa[x];
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1;i <= n;i++)
Fa[i] = i;
for (int i = 1;i <= n;i++)
{
cin >> s[i];
for (int j = 0;j < (int)s[i].size();j++)
vec[s[i][j]-'a'].insert(i);
}
for (int i = 0;i < 26;i++)
{
int h = *vec[i].begin();
int th = GetF(h);
for (auto v: vec[i])
{
int tr = GetF(v);
if (tr != th)
Fa[tr] = th;
}
}
int ans = 0;
for (int i = 1;i <= n;i++)
if (GetF(i) == i) ans++;
cout << ans << endl;
return 0;
}
Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)的更多相关文章
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...
- Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...
随机推荐
- C 编程环境搭建 Window 篇
前言 - 简介 我们在写代码的过程中, 不可避免的重度依赖所处的开发环境. 本文重点带大家在 Window 搭建 C 简单控制台项目. 当作存档, 用于记录项目搭建各种重复操作. 在详细过程之前, ...
- Kafka跨集群迁移方案MirrorMaker原理、使用以及性能调优实践
序言Kakfa MirrorMaker是Kafka 官方提供的跨数据中心的流数据同步方案.其实现原理,其实就是通过从Source Cluster消费消息然后将消息生产到Target Cluster,即 ...
- React 父/子窗体参数传递
1.父窗体 import Modal from './Modal' onModalRef = ref => { this.modal = ref } onCallback = msg => ...
- 如何在pycharm中设置环境变量
今天运行tensorflow的时候,发现在pycharm下,程序无法找到CUDA的libcupti.so文件.而在添加完环境变量: export LD_LIBRARY_PATH=$LD_LIBRARY ...
- Linux环境下:vmware安装Windows报错误-无人参与应答文件包含的产品密钥无效
最近在安装window server 2012 R2的时候,输入好密钥可以继续安装,但在后面又提示我“无人参与应答文件包含的产品密钥无效.删除无效的密钥或在无人参与应答文件中提供有效的产品密钥继续进行 ...
- N-gram理解
如何来理解这个概率呢? p( i love you) 如果是 =p(i)p(love)p(you) 就是只考虑单词出现的概率本身. 如果是 =p(i)p(love|i)p(you|love) 就是 ...
- English--状语从句
English|状语从句 现在开始讲述关于状语从句的内容,由于内容略难并且略多,我将换个简单的方式帮助理解,这一块有些东西是需要记忆的.虽然是记忆,但是我可以保证记忆的内容是知识. 前言 目前所有的文 ...
- python基础--初始数据结构
目录: 一.知识点1.IDE 集成开发环境2.字符格式化输出3.数据运算4.循环loop5.数据类型6.列表与元组 二.例子1.输入名字.年龄.工作.薪水,进行格式化的输出.2.for语句实现输入密码 ...
- Ext.bind函数说明
bind( fn, [scope], [args], [appendArgs] ) : FunctionCreate a new function from the provided fn, chan ...
- JDK8 stream用法
forEach举例 public static void main(String[] args) { // TODO Auto-generated method stub List<Person ...