Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5192 Accepted Submission(s): 1447
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.
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).
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 <stdio.h> #include <map> #include <string> #include <string.h> using namespace std; int f[111111]; int count[111111]; int t,n; int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } int main() { while(~scanf("%d",&t)) { while(t--) { scanf("%d",&n); string a,b; map<string,int>m; m.clear(); int i; for(i=1; i<=100000; i++) { f[i]=i; count[i]=1; } int cnt=1; for(i=0; i<n; i++) { cin>>a>>b; if(!m[a]) { m[a]=cnt++; } if(!m[b]) { m[b]=cnt++; } int x=find(m[a]); int y=find(m[b]); if(x!=y) { f[x]=y; count[y]+=count[x]; printf("%d\n",count[y]); } else { printf("%d\n",count[x]); } } } } return 0; }
本文探讨了在线虚拟交友网站上个人社交网络的形成过程及其影响力。通过输入多个友谊关系,输出不同阶段社交网络的规模变化,展示了社交网络扩展的动态特性。

被折叠的 条评论
为什么被折叠?



