博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3172 Virtual Friends (映射并查集)
阅读量:5296 次
发布时间:2019-06-14

本文共 2645 字,大约阅读时间需要 8 分钟。

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
 

 题意:  逐步给你一些关系网,对于每一步求所给出的两个人合并之后所构成的关系网。

代码:

      

1  #include
2 #include
3 #include
4 #include
5 #include
6 #include
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]
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
::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
(bb,posb));73 sac[bb[i]]=++cnt;74 }75 }76 init(cnt);77 for(int i=0;i
View Code

 

转载于:https://www.cnblogs.com/gongxijun/p/4147855.html

你可能感兴趣的文章
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
Chapter 3 Phenomenon——12
查看>>
C语言中求最大最小值的库函数
查看>>
js学习(精华帖)
查看>>
和小哥哥一起刷洛谷(1)
查看>>
jquery对id中含有特殊字符的转义处理
查看>>
获取元素样式信息于三中获取方式的区别
查看>>
遇麻烦,Win7+Ubuntu12.10+Archlinux12.10 +grub
查看>>
SqlBulkCopy大批量导入数据
查看>>
HTTP协议 (四) 缓存
查看>>
python学习之random
查看>>
使用onclick跳转到其他页面/跳转到指定url
查看>>
【转载】测试计划模板
查看>>
pandas 修改指定列中所有内容
查看>>
ubuntu18.04 复制或剪切某文件夹下的前x个文件到另一个文件夹下
查看>>
input的value中有特殊字符
查看>>
字符串压缩
查看>>
用Lua定制Redis命令
查看>>
小程序-canvas在IOS手机层级最高无法展示问题
查看>>