原题链接: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. 感受机房管理化繁为简-新款KVM使用心得

    感受机房管理化繁为简-新款KVM使用心得 一. 背景 随着网络应用的不断增多,各地机房服务器数量也随之增加,利用多传统主机切换器的方式已经无法满足目前这种区域广.设备多人员紧缺的现状,而且即使是使用了 ...

  2. SQL:认识数据库约束

    讲了关于数据库的很多内容,也讲了很多约束,对唯一和主键.核查和规则.外键约束很感兴趣. 一.唯一和主键(实体完整性) 区别: 数量:一张表只可以有一个主键约束,却可以有多个唯一约束. 是否空值:主键不 ...

  3. cordova 日曆 聯系人 插件使用

    日曆插件 聯係人插件 我用聯係人插件, function onSuccess(contact) { alert("Save Success"); }; function onErr ...

  4. flask-admin

    初始化 class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=No ...

  5. .net orm比较之dapper和Entity Framework6的简单测试比较

    .net orm比较之dapper和Entity Framework6的简单测试比较

  6. Datatable导出Excel

    ; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...

  7. 理解DOM中的事件流

    浏览器发展到第四代时(IE4和Netscape Communicator 4),浏览器团队遇到一个很有意思的问题:页面的哪一部分会拥有特定的事件?想象下在一张纸上有一组同心圆,如果你把手指放在圆心上, ...

  8. 创建parameter id

    Custom Parameter-id Creation By Abhijit Daptary, Capgemini India Step1: Creation of parameter ID.  P ...

  9. 高仿QQ的即时通讯应用带服务端软件安装

    Android 基于xmpp协议,smack包,openfire服务端(在下面)的高仿QQ的即时通讯实现.实现了注册,登录,读取好友列表,搜索好友,添加分组,添加好友,删除好友,修改心情,两个客户端之 ...

  10. MySQL 5.7.9 免安装配置

    MySQL 5.7.9 免安装配置 环境:win10 64位 mysql版本:mysql-5.7.9-winx64.zip (http://dev.mysql.com/downloads/mysql/ ...