一开始一直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 (字符串的并查集)的更多相关文章

  1. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  2. HDU 3172 Virtual Friends(map+并查集)

    Virtual Friends Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tot ...

  3. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  4. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  5. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  6. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  8. hdu 3172 Virtual Friends

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...

  9. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

随机推荐

  1. centos下部署项目问题

    最近写了商品管理的后台完成了一部分,用node+koa+mongodb搭建,现在想部署到自己的服务器上,部署的过程中遇到了一些坑.首先就是各种环境的搭建,搭建好了之后要把后台的代码传到服务器上运行,运 ...

  2. XHTML中button加入超链接以及使插入图片与屏幕一样大

    1.button加入超链接 (1)假设是在本页跳转到新页面.用 <span style="font-size:18px;"><input type="b ...

  3. DTD复习笔记(复习资料为菜鸟教程里的DTD教程)

    DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 为什么使用 DTD? 通过 DTD,您的每一个 XML 文件均可携带 ...

  4. RedHat 安装Hadoop并运行wordcount例子

    1.安装 Red Hat 环境 2.安装JDK 3.下载hadoop2.8.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/had ...

  5. 登录日志的访问日志的 统计 MapReduce

    登录日志的访问日志的 统计    MapReduce <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-commo ...

  6. Struts2的工作原理(图解)详解

    Struts2的工作原理 上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:https://github.com ...

  7. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  8. linux下如何用php读取word

    在实际的工作中遇到到要导入word格式的文件,经过努力,终于成功了. 在linux上用PHP读取WORD文档,其实是使用了 antiword程序把word文档转化为txt文档. 再使用php执行系统命 ...

  9. react native 之页面布局

     第一章 flexbox 布局 1.flexDirection:'row', 水平 flexDirection:'column',垂直 需要在父元素上设置这种属性才能实现flex. flex:1 会撑 ...

  10. access函数的使用检查文件的权限【学习笔记】

    #include "apue.h" #include <fcntl.h> int main(int argc,char **argv) { ) err_quit(&qu ...