hdu 3172 Virtual Friends (字符串的并查集)
一开始一直wa,因为第一个数字t也是多组输入。
然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后:
std::ios::sync_with_stdio(false); //是用来禁用cin这个兼容性的特性,禁用后就相差无几了
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
map<string, string>fa;
map<string, int>Rank;
const int maxn = ;
string a[maxn];
string b[maxn]; string Find(string x){
if (x == fa[x])
return x;
else
return fa[x] = Find(fa[x]);
} void set_union(string x, string y){
string xx = Find(x);
string yy = Find(y);
//cout << "fa: " << xx << " " << yy << endl;
if (xx == yy){
cout << Rank[xx] << endl;
}
else{
fa[yy] = xx;
Rank[xx] += Rank[yy];
cout << Rank[xx] << endl;
}
/*
if (Rank[xx] > Rank[yy]){
fa[yy] = xx;
Rank[xx] += Rank[yy];
cout << Rank[xx] << endl;
}
else if (Rank[xx]==Rank[yy]){
fa[yy] = xx;
Rank[xx]+=Rank[yy];
cout << Rank[xx] << endl;
}
else{
fa[yy] = xx;
Rank[yy] += Rank[xx];
cout << Rank[yy] << endl;
}
*/
} void init(int sum){
for (int i = ; i < sum; i++){
fa[a[i]] = a[i];
fa[b[i]] = b[i];
Rank[a[i]] = Rank[b[i]] = ;
}
} int main(){
std::ios::sync_with_stdio(false);
int t;
while (cin>>t){
while (t--){
int n;
cin >> n;
for (int i = ; i < n; i++){
cin >> a[i];
cin >> b[i];
}
init(n);
for (int i = ; i < n; i++){
//cout << "#" << i << endl;
//cout << a[i] << " " << b[i] << endl;
set_union(a[i], b[i]);
}
}
}
//system("pause");
return ;
}
hdu 3172 Virtual Friends (字符串的并查集)的更多相关文章
- HDU 3172 Virtual Friends (map+并查集)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- HDU 3172 Virtual Friends(map+并查集)
Virtual Friends Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tot ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- hdu 3172 Virtual Friends
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
随机推荐
- Qt、C++ 简易计算器
Qt.C++实现简易计算器: 以下内容是我实现这个简易计算器整个过程,其中包括我对如何实现这个功能的思考.中途遇到的问题.走过的弯路 整个实现从易到难,计算器功能从简单到复杂,最开始设计的整个实现步骤 ...
- VMWare 14 Workstation Pro 下载与安装
1.双击安装运行 2.下一步 3.接受 下一步 4.自定义安装路径,下一步 5.下一步,取消勾选加入vmware客户体验 6.下一步 7.安装 8.安装中 9.完成 10.点击许可证安装 输入:FF3 ...
- 杭电1596find the safest road(spfa)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...
- HDU 6061 RXD and functions NTT
RXD and functions Problem Description RXD has a polynomial function f(x), f(x)=∑ni=0cixiRXD has a tr ...
- vscode部分文件夹无法打开
vscode部分文件夹无法打开,无法正常显示 解决方案:关闭该IDE.找到C:\Users\XX\AppData\Roaming\Code,将Code文件夹删除.重新打开vsCode,即可恢复.但是以 ...
- Eos的Wasm智能合约的局限性
官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...
- python split space
发现自己写python的空格split还挺多坎的,尤其是最后一个是空格的情形: def split(s): i = 0 ans = [] while i < len(s): start = i ...
- python二维数组初始化
>>> a=[[0]*3 for i in range(3)]>>> a[[0, 0, 0], [0, 0, 0], [0, 0, 0]]>>> ...
- RESTful 架构与 RESTful 服务
风格 ⇒ 标准 ⇒ 协议 ⇒ 实现 Representational State Transfer,是一种软件架构风格,既然是风格,就非标准或协议,而是定义了一组设计原则和约束条件.具有如下特点: 适 ...