文章标题 HDU 3172 : Virtual Friends (并查集+map)
2017-01-19 22:51
411 查看
Virtual Friends
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
题意:有一种虚拟的朋友,比如你的朋友,你的朋友的朋友,朋友的朋友的朋友都是你的虚拟朋友,每次输入两个名字表示这两个人是虚拟朋友,然后每次输入要输出这两个人的虚拟朋友的数目。
分析:用并查集,每次将两个人名映射成一个整数,然后通过并查集求出这两个人的虚拟朋友 的数目。映射的话可以用map映射成一个数字。
代码:
#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<vector> #include<math.h> #include<map> #include<queue> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f; int F; int fa[100005*2];//乘以2因为每次有两个人名 int num[100005*2]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } void merge(int u,int v){ int fu=find (u); int fv=find (v); if (fu==fv)return ; fa[fu]=fv; num[fv]+=num[fu]; } map<string,int>mp; int main () { int t; string a,b; int cnt; while (cin>>t){//多组输入 while (t--){ cnt=1; mp.clear(); for (int i=0;i<=200000;i++){//初始化 fa[i]=i; num[i]=1; } cin>>F; while (F--){ cin>>a>>b; if (mp.find(a)==mp.end()){//判断人名a出现过没有,没有就给其附一个编号 mp[a]=cnt++; } if (mp.find(b)==mp.end()){//同上 mp[b]=cnt++; } merge(mp[a],mp[b]);//合并 cout<<num[find(mp[a])]<<endl;//求出数目 } } } return 0; }
相关文章推荐
- hdu 3172 Virtual Friends(并查集+map)
- hdu 3172 Virtual Friends 【并查集+map】
- HDU-3172 Virtual Friends 并查集+map
- HDU 3172 Virtual Friends 并查集+map
- 【 HDU 3172 Virtual Friends】 并查集+map指针优化
- HDU 3172 Virtual Friends
- HDU 3172 Virtual Friends(带权并查集,map)
- hdu 3172 Virtual Friends(并查集,字典树)
- hdu 3172 Virtual Friends
- HDU 3172 Virtual Friends (并查集与map的运用,同HDU1829)
- HDU 3172 Virtual Friends 并查集+字典树
- HDU 3172 Virtual Friends(带权并查集)
- HDU 3172 Virtual Friends
- HDU 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends(并查集)
- hdu 3172 virtual friends 并查集
- 【STL+并查集+卡格式卡时间】HDU 3172——Virtual Friends
- hdu 3172 Virtual Friends (并查集)