HDU 3172 Virtual Friends(带权并查集,map)
2014-10-04 23:21
295 查看
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.
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 <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; }
相关文章推荐
- HDU 3172 Virtual Friends (并查集与map的运用,同HDU1829)
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
- HDU 3172 Virtual Friends (并查集节点统计)
- hdu 3172 Virtual Friends(map)
- 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(并查集)
- 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(带权并查集)
- 杭电 3172 Virtual Friends【并查集 + map容器】