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> # ...
随机推荐
- for in 与for of
最近在项目中需要用到遍历对象,用ES6 for of对象后报如下错误 TypeError: [object Object] is not iterable!,网上查询阮大神的教程发现“ES6 的有些 ...
- Xammp修改端口
How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...
- python实现斐波那契查找
通过在网上找教程解释和看书,总结出一套比较简单易懂的代码实现. 斐波那契查找和二分查找一样,针对的是有序序列,在此前提下: # 先创建一个Fibonacci函数 fib = lambda n: n i ...
- WPF新手之如何将数据绑定到TreeView
看过许多例子,全是绑定到类的,没人说如何绑定到某个对象,偏偏我这个绝对的新手就是要绑定到一个对象,只能自己摸索了: 首先要将数据绑定到容器,有以下几个默认条件:①元数据必须包装在List或者Obser ...
- EnumChildWindows
EnumChildWindows 函数功能:枚举一个父窗口的所有子窗口. 函数原型:BOOL EnumChildWindows(HWND hWndParent,WNDENUMPROC lpEnumFu ...
- 谈谈Paxos一致性算法和一致性这个名词
转自:http://www.cnblogs.com/esingchan/p/3917718.html 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的& ...
- (C\C++)inline关键字
背景(C&C++中) inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义如: #define ExpressionName(Va ...
- vue开发总结(一)
vue使用快一个多月了,从移动端到PC端,踩过的坑也不少.项目的开发是基于element ui 与 mint ui 组件库,下面总结下项目中的一些知识点: 一.网页数据请求 首次进入页面,需要请求数据 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- XMLHttp.send()不传参时必须传null吗?
xmlhttp的send是传递参数用的,但是只有在使用post方式提交请求的时候才有用如下:xmlhttp.open("post",url,true); ...xmlhttp.se ...