HDU 3172 Virtual Friends(带权并查集)
来源:互联网 发布:尼尔森数据分析 编辑:程序博客网 时间:2024/05/29 06:54
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7434 Accepted Submission(s): 2133
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
Source
University of Waterloo Local Contest 2008.09
Recommend
chenrui | We have carefully selected several similar problems for you: 3038 3234 3047 2818 1558
这道题比较难处理的一个地方是需要处理的数据是字符串,这里看别人用了一个STL中的一个映射map是STL中的一个,映照容器,包含在#include<map>中,string是键值,int是数据,如果是map["happy"]=6,string str="happy",那么map[str]=6;
#include<stdio.h> #include<map>//注意 #include<string> using namespace std; int pre[100005],num[100005]; map<string,int>m;//STL中的一个映照容器,第一次接触 void init() { int i; for(i=0;i<=100005;i++) { pre[i]=i; num[i]=1; } } int find(int x) { if(x==pre[x]) return x; return pre[x]=find(pre[x]); } void merge(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fy!=fx) { pre[fx]=fy; num[fy]+=num[fx]; printf("%d\n",num[fy]); } else { printf("%d\n",num[fy]); } } int main() { int t; while(scanf("%d",&t)!=EOF)//坑 { while(t--) { int n; char a[200],b[200]; init(); m.clear(); //清空 scanf("%d",&n); int ans=1; while(n--) { scanf("%s %s",a,b); if(!m[a]) { m[a]=ans++; } if(!m[b]) { m[b]=ans++; } merge(m[a],m[b]); } } } }
0 0
- HDU 3172 Virtual Friends(带权并查集)
- HDU 3172 Virtual Friends 带权并查集 -秩
- 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(并查集+容器)
- hdu3172 Virtual Friends 带权并查集
- 带权并查集:HDU3172-Virtual Friends
- LEETCODE 226
- Map集合中value()与keySet()、entrySet()区别
- 如何让程序在Nexus上全屏显示
- HDOJ-----3635带权并查集
- IOS学习笔记 ——生命周期
- HDU 3172 Virtual Friends(带权并查集)
- 4个主要的map实现类介绍
- The following classes could not be found: - android.support.v7.internal.widget.ActionBarOverlayLayou
- 算法题的五种解法二——模式匹配法
- C提高——结构体
- Android群英传--自定义View详解(一)
- 业务--报文相关
- ssh 免密码登录
- 1012 incryptography