hdu3172 Virtual Friends
来源:互联网 发布:看不起培训机构程序员 编辑:程序博客网 时间:2024/05/29 04:47
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
13Fred BarneyBarney BettyBetty Wilma
Sample Output
234这题可以用并查集+map来做,注意可能输入的两个字符串相同。#include<stdio.h>#include<string.h>#include<algorithm>#include<map>#include<string>using namespace std;int pre[100010],num[100010];int find(int x){int i,j=x,r=x;while(r!=pre[r])r=pre[r];while(j!=pre[j]){i=pre[j];pre[j]=r;j=i;}return r;}int main(){int T,n,m,i,j,index,a,b,t1,t2,sum;char s1[30],s2[30];while(scanf("%d",&T)!=EOF){while(T--){map<string,int>hash;hash.clear();index=1;scanf("%d",&n);for(i=1;i<=100005;i++){pre[i]=i;num[i]=1;}for(i=1;i<=n;i++){scanf("%s%s",s1,s2);if(hash[s1]==0)hash[s1]=index++;if(hash[s2]==0)hash[s2]=index++;t1=find(hash[s1]);t2=find(hash[s2]);if(t1==t2)printf("%d\n",num[t1]);else{pre[t1]=t2;num[t2]=num[t2]+num[t1];printf("%d\n",num[t2]);}}}}return 0;}
0 0
- HDU3172--Virtual Friends
- hdu3172 Virtual Friends
- hdu3172 Virtual Friends
- HDU3172 Virtual Friends
- hdu3172 Virtual Friends
- HDU3172 Virtual Friends
- hdu3172 Virtual Friends
- HDU3172---Virtual Friends
- hdu3172:Virtual Friends
- HDU3172-Virtual Friends
- HDU3172 UVA11503 Virtual Friends
- HDU3172-Virtual Friends
- HDU3172 Virtual Friends 并查集
- Hdu3172 - Virtual Friends - 并查集
- hdu3172 Virtual Friends--并查集
- hdu3172 Virtual Friends 并查集 map映射
- hdu3172 Virtual Friends 带权并查集
- 带权并查集:HDU3172-Virtual Friends
- 在Android项目中引用其他Library工程的一些问题
- 使用C# + Xamarin开发应用-- 使用json.net 遇到错误 Object non reference
- hadoop MapReduce实例解析
- 二维数组
- Qt学习——分割窗口QSplitter类 .
- hdu3172 Virtual Friends
- 如何用CDR“克隆”简单图形
- NSURLSession下载图片的简单例子
- android 将整个布局设置为不可点击状态
- js 贪吃蛇 大部分显而易见的问题已解决 但是代码过于复杂 欢迎纠错
- [leetcode][回溯] Letter Combinations of a Phone Number
- Hadoop示例程序WordCount详解及实例
- erlang 实现并行快速排序
- JQuery选择器大全