hdu 3172 Virtual Friends (映射并查集)
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5491 Accepted Submission(s): 1519
Problem Description
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends. Your task is to observe the interactions on such a website and keep track of the size of each person's network. Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
Input
Input file contains multiple test cases. The first line of each case indicates the number of test friendship nest. each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
Output
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.
Sample Input
1 3 Fred Barney Barney Betty Betty Wilma
Sample Output
2
3
4
Source
University of Waterloo Local Contest 2008.09
题意: 逐步给你一些关系网,对于每一步求所给出的两个人合并之后所构成的关系网。
代码:
1 #include<cstdio>
2 #include<cstring>
3 #include<cstdlib>
4 #include<iostream>
5 #include<map>
6 #include<string>
7 using namespace std;
8 const int maxn=100003;
9 int father[maxn];
10 int rank[maxn];
11 //初始化
12 void init( int n)
13 {
14 for(int i =0;i<=n ;i++){
15 father[i]=i;
16 rank[i]=1;
17 }
18 }
19 //搜索
20 int fin(int x)
21 {
22 int tem=x;
23 while(x!=father[x]){
24 x=father[x];
25 }
26 //进一步压缩
27 while(tem!=father[tem])
28 {
29 tem=father[tem];
30 father[tem]=x;
31 }
32
33 return x;
34 }
35 void Union(int a,int b){
36 a=fin(a);
37 b=fin(b);
38 if(a!=b){
39 if(rank[a]<rank[b]){
40 rank[b]+=rank[a];
41 father[a]=b;
42 }
43 else{
44 rank[a]+=rank[b];
45 father[b]=a;
46 }
47 }
48 }
49 map<string,int>sac;
50 char aa[maxn][21],bb[maxn][21];
51
52 int main()
53 {
54 int t,n;
55 while(scanf("%d",&t)!=EOF)
56 {
57 while(t--){
58 scanf("%d",&n);
59 if(!sac.empty()) sac.clear();
60 int cnt=0;
61 for(int i=0;i<n ;i++){
62 scanf("%s%s",aa[i],bb[i]);
63 // map<string,int>::iterator it;
64 //it=sac.find(aa[i]);
65 if(sac.find(aa[i])==sac.end())
66 sac[aa[i]]=++cnt;
67
68 // it=sac.find(bb[i]);
69 if(sac.find(bb[i])==sac.end())
70 {
71 // posb=sac.size();
72 // sac.insert(pair<string,int>(bb,posb));
73 sac[bb[i]]=++cnt;
74 }
75 }
76 init(cnt);
77 for(int i=0;i<n;i++)
78 {
79 Union(sac[aa[i]],sac[bb[i]]);
80 printf("%dn",rank[fin(sac[aa[i]])]);
81 }
82 }
83 }
84 return 0;
85 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Docker 入门到实战教程(六)Docker数据卷
- Docker 入门到实战教程(七)安装Redis
- Docker 入门到实战教程(八)安装Mysql
- Docker 入门到实战教程(九)安装Nginx
- Docker教程(九)部署Spring Boot项目
- Docker 入门到实战教程(十一)部署Vue+SpringBoot 前后端分离项目
- Docker 入门到实战教程(十二)ELK+Filebeat搭建日志分析系统
- Docker 入门到实战教程(十三)Docker Compose
- 解决IDEA2020.1版本的lombok插件问题
- 工具系列 | 视频监控RTSP转HLS解决方案
- Redis系列 |(一)六种基本数据结构
- 工具系列 | Jenkins 构建伟大,无所不能
- 工具系列 | H5自定义视频播放器实现
- 前端系列 |原生JS和jQuery循环遍历函数
- 工具系列 | H5如何实现人脸识别