codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D
题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个集合,可以互相破解,先求有多少个不同集合的密码
思路:简单的并查集。首先一共有26个字母,对于每个密码,我们合并其所涵盖的字母,例如密码1:abcd,那么abcd合并起来,含有abcd的任意密码都同属于一个集合,所有我们把n个密码串先做一次合并。最终再扫一遍所有的密码串,用set维护集合的个数,最终输出set集合的大小即可。
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn =2e5+;
int fa[];
void init(){
for(int i = ;i<=;i++){
fa[i] = i;
}
}
int find(int x){
if(fa[x] == x) return x;
else return fa[x] = find(fa[x]);
}
void unite(int x,int y){
int tx = find(x),ty = find(y);
if(tx == ty) return ;
fa[y] = tx;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin>>n;
init();
set<int> ss;
vector<string> v;
while(n--){
string s;
cin>>s;
v.push_back(s);
int t = s[]-'a'+;
for(int i = ;i<s.length() ;i++){
int cur = s[i]-'a'+;
unite(t,cur);//合并当前字符串的所有字母
}
}
for(int i = ;i<v.size() ;i++){
for(int j =;j<v[i].length();j++){
ss.insert(find(v[i][j]-'a'+));//扫所有字符串的所有字母,判断是否是同一父亲
}
}
cout<<ss.size();
return ;
}
codeforces div2 603 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 #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- CodeForces 698B Fix a Tree (并查集应用)
当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
随机推荐
- Codeforces667D(spfa+dp)
题意: 给定一个带权有向图,若P(A,B)表示节点A到B的最短路长度,选择四个节点ABCD,使得P(A,B)+P(B,C)+P(C,D)最大. 节点数n在1,000以内,边数m在2,000以内. 思路 ...
- NumPy迭代数组
numpy.nditer是NumPy的一个迭代器对象,提供能够灵活的访问一个或者多个属猪元素的方式. # 迭代 z=np.arange(6).reshape(3,2) for x in np.ndit ...
- JavaDay3(上)
Java learning_Day3(上) 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> ...
- Tomcat启动命令行窗口中文乱码
找到Tomcat安装目录conf/logging.properties文件 添加语句:java.util.logging.ConsoleHandler.encoding = GBK 重启Tomcat
- Wannafly Camp 2020 Day 1E 树与路径 - 树上差分,LCA
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000005; vec ...
- How to do high impact research + 实事求是
1. develop a strong publications record early, so do what you can to make that happen. 2. 粗读:abstrac ...
- phpstorm格式化数组
如果我们想要的这样的数组格式的话,可以设置 <?php $arr = array["a" => 0, "b" => 1, "c&qu ...
- js监听页面copy事件添加版权信息
个人博客 地址:http://www.wenhaofan.com/article/20180921103346 1.介绍 当页面需要做版权保护时,比如当用户copy我们网站的文章时,我们会希望在他co ...
- eclipse中部署项目到tomcat启动,一直是starting状态
这个问题主要是在eclipse中设置了proxy代理导致的,将Network Connections中的Active Provider更改即可! 打赏
- 错误:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', ...