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

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<map>
using std::map;
using std::string;
const int Max_N = ;
struct UnionFind {
map<string, int> rec;
int num[Max_N], par[Max_N];
void init() {
for (int i = ; i < Max_N; i++) {
par[i] = i;
num[i] = ;
}
rec.clear();
}
inline int find(int x) {
while (x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline void unite(int x, int y) {
x = find(x), y = find(y);
if (x != y){
par[x] = y;
num[y] += num[x];
printf("%d\n", num[y]);
} else {
printf("%d\n", num[y]);
}
}
inline void gogo() {
int n, cnt = ;
char s1[], s2[];
scanf("%d", &n);
while (n--) {
scanf("%s %s", s1, s2);
if (!rec[s1]) rec[s1] = cnt++;
if (!rec[s2]) rec[s2] = cnt++;
unite(rec[s1], rec[s2]);
}
}
}ans;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
while (~scanf("%d", &t)) {
while (t--) ans.init(), ans.gogo();
}
return ;
}

hdu 3172 Virtual Friends的更多相关文章

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

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

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

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

  3. hdu 3172 Virtual Friends (映射并查集)

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

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

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

  5. hdu 3172 Virtual Friends (并查集)

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

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

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

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

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

  8. hdu 3172 Virtual Friends (字符串的并查集)

    一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...

  9. HDU 3172 Virtual Friends 并与正确集中检查 -秩

    ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集 ...

随机推荐

  1. Operator overloading

    By defining other special methods, you can specify the behavior of operators on user-defined types. ...

  2. Eclipse中Jsp页面警告的解决方法小结

    恩,只要是开发人员,这样的小事情总会遇到的,对于这其中的某些警告性的错误是不影响代码的运行的,对应的功能也是能实现的,不过总给人一种不太好看的感觉!如果代码写的比较符合规范,这些问题也就自然而然的消失 ...

  3. Andriod项目开发实战(2)——JSON和XML的区别

    详情见: 1.http://www.cnblogs.com/SanMaoSpace/p/3139186.html 2.http://www.cnblogs.com/yank/p/4028266.htm ...

  4. prototype原型模式中的问题

    对于每个构造函数来说,都有一个prototype属性.对于每个对象实例来说,都有_proto_属性. 参看下面代码: function Person(){} Person.prototype={ na ...

  5. html5 搖一搖

    <script> // 首先在页面上要监听运动传感事件 function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 ...

  6. MyBatis学习系列二——增删改查

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...

  7. [原]Hrbust1053 Warcraft III (完全背包)

    本文出自:http://blog.csdn.net/svitter 原题:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProbl ...

  8. 【转】Linux Kernel __setup(str, fn)解析

    __setup这条宏在Linux Kernel中使用最多的地方就是定义处理Kernel的启动参数的函数及数据结构,宏定义如下: #define __setup(str, fn) \ __setup_p ...

  9. Ninject依赖注入——构造函数的注入

    1.Ninject简介 Ninject是基于.Net平台的依赖注入框架,它能够将应用程序分离成一个个高内聚.低耦合(loosely-coupled, highly-cohesive)的模块,然后以一种 ...

  10. POJ C++程序设计 编程题#2 魔兽世界之二:装备

    编程题#2: 魔兽世界之二:装备 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB ...