Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6636    Accepted Submission(s): 1888

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
 格式坑爹,水死了;存根节点。。。。
代码:
 #include<iostream>
#include<stdio.h>
#include<map>
#include<string>
#define MAX(x,y) x>y?x:y
const int MAXN=;
using namespace std;
int fri[MAXN],num,now[MAXN];
void initial(){
for(int i=;i<=MAXN;++i)fri[i]=i,now[i]=;
}
int find(int x){
/*if(x==fri[x])return x;
return fri[x]=find(fri[x]);*/
int r=x;
while(r!=fri[r])r=fri[r];
int i=x,j;
while(i!=r)j=fri[i],fri[i]=r,i=j;
return r;
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2){
fri[f2]=f1;now[f1]+=now[f2];//now[f2]=now[f1];
//printf("now[%d]=%d\n",f1,now[f1]);
}
}
int main(){
int T,N;
string A,B;
while(~scanf("%d",&T)){
while(T--){map<string,int>m;initial();num=;
scanf("%d",&N);
while(N--){
cin>>A>>B;
if(m.find(A)==m.end())num++,m[A]=num;
if(m.find(B)==m.end())num++,m[B]=num;
// printf("%d %d\n",m[A],m[B]);
merge(m[A],m[B]);
printf("%d\n",now[find(m[A])]);
}
}
}
return ;
}
/*1
7
a b
a a
a c
c g
g h
n n
h n
*/

Virtual Friends(并查集+map)的更多相关文章

  1. HDU 2419 Boring Game(并查集+map)

    感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...

  2. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. Kattis - Virtual Friends(并查集)

    Virtual Friends These days, you can do all sorts of things online. For example, you can use various ...

  4. [Swust OJ 772]--Friend(并查集+map的运用)

    题目链接:http://acm.swust.edu.cn/problem/772/ Time limit(ms): 1000 Memory limit(kb): 65535    Descriptio ...

  5. TOJ3955: NKU ACM足球赛(并查集+map+细节题)

    时间限制(普通/Java):5000MS/15000MS     内存限制:65536KByte 描述 NKU ACM最近要举行足球赛,作为此次赛事的负责人,Lee要对报名人员进行分队.分队要遵循如下 ...

  6. Atcoder 2159 連結 / Connectivity(并查集+map乱搞)

    問題文N 個の都市があり.K 本の道路と L 本の鉄道が都市の間に伸びています. i 番目の道路は pi 番目と qi 番目の都市を双方向に結び. i 番目の鉄道は ri 番目と si 番目の都市を双 ...

  7. 【日常学习】【并查集+map】codevs2639 约会计划题解

    然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系 ...

  8. “美登杯”上海市高校大学生程序设计 C. 小花梨判连通 (并查集+map)

    Problem C C . 小 花梨 判连通 时间限制:2000ms 空间限制:512MB Description 小花梨给出

  9. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

随机推荐

  1. python字符集选择

    # coding=utf8 或者 # -*- coding:utf-8 -*- 在python2 中默认是ASCII码的字符集,但可以引入其他的字符集  这个需要在头信息中引入: 而在python3中 ...

  2. AC Milan VS Juventus(模拟)

    AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

  3. DM6437 dsp系列之启动过程全析(2)—AIS文件解析

    本文均属自己阅读源码的点滴总结,转账请注明出处谢谢. 欢迎和大家交流.qq:1037701636 email: gzzaigcn2009@163.com,gzzaigcn2012@gmail.com ...

  4. Why Hadoop2

    自从Hadoop2出现之后,其迅速代替了Hadoop1的地位,并丰富了Hadoop的应用场景.假设如今有公司使用Hadoop的话,往往直接採用Hadoop2了. Hadoop2能被如此广泛的使用,肯定 ...

  5. POJ-1118(超时,但未找到原因)

    #include<iostream> #include<map> #include<vector> using namespace std; //y=kx+z ty ...

  6. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  7. CCS v5 无法启动解决办法及Launchpad仿真器电脑无法识别解决方法

    安装ccs_setup_5.1.1.00028.exe后(无论是自己装eclipse还是在原来的基础上安装eclipse的插件),ccs5的应用无法打开,错误为:An error has occurr ...

  8. ARM指令和Thumb指令区别

    Thumb指令集 ]的问题而提出的,它具有16为的代码密度.Thumb不是一个完整的体系结构,不能指望处理程序只执行Thumb指令而不支持ARM指令集.因此,Thumb指令只需要支持通用功能,必要时, ...

  9. UVA11806Cheerleaders(容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目意思:在m行n列的矩形网格中放k个相同的石子,问有多少中方法?每个格子最多放一 ...

  10. boost vc编译

    0.下载:http://www.boost.org/ 1.编译b2.exe,bjam.exe. 双击根目录下面的bootstrap.bat文件,生成b2.exe,bjam.exe(或者使用vs的命令行 ...