Virtual Friends
来源:互联网 发布:java基础面试题 编辑:程序博客网 时间:2024/06/05 02:54
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
解题思路:
此题一道简单的并查集问题。
题目大意是说A与B结成朋友,则此时包含A或者B的朋友圈有多少个人,B与C结成朋友,此时这个朋友圈就有A、B、C三个人,C与D结成朋友,此时这个圈就有4个人了。
所以,将每一个人的名字用map来判断属于第几个出现的名字,将名字转换成数字,然后运用f[]函数,和count[]函数来维护并查集每一个根节点的信息。
代码如下:
#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 a){ if(a==f[a]) { return a; } int t=find(f[a]); f[a]=t; return t;}int main(){ while(~scanf("%d",&t)) { while(t--) { scanf("%d",&n); string name1,name2; map<string,int>my_map; my_map.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>>name1>>name2; if(!my_map[name1]) { my_map[name1]=cnt++; } if(!my_map[name2]) { my_map[name2]=cnt++; } int a=my_map[name1]; int b=my_map[name2]; int x=find(a); int y=find(b); if(x!=y) { f[x]=y; count[y]+=count[x]; printf("%d\n",count[y]); } else { printf("%d\n",count[x]); } } } } return 0;}
0 0
- Virtual Friends
- Virtual Friends
- Virtual Friends
- UVa 11503 - Virtual Friends
- UVa 11503 - Virtual Friends
- HDU3172--Virtual Friends
- hdu3172 Virtual Friends
- hdu3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU3172 Virtual Friends
- hdu 3172:Virtual Friends
- hdu3172 Virtual Friends
- NOJ [1246] Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU 3172 Virtual Friends
- HDU3172 Virtual Friends
- 路径中 斜杠/和反斜杠\ 的区别
- JSP中的三大乱码问题
- springmvc和struts2的区别
- 开发团队里是否需要测试人员?
- Quartz Cron 表达式
- Virtual Friends
- 【OVS2.5.0源码分析】upcall处理线程分析(4)
- LeetCode之Intersection of Two Arrays
- jQuery 知识点 (四) —— jQuery 常用的的 DOM 函数
- SODBASE CEP学习(十五):常见场景EPL示例
- Nginx配置性能优化
- 一篇检讨
- MFC开发的软件文本框输入汉字变为一个个问号?
- 深入理解Android的startservice和bindservice