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. Unity 单元测试(NUnit,UnityTestTools)

    在软件开发中单元测试是非常重要的一个环节, =.=盘子脸去了几家公司都没有单元测试这个概念. 我们的系统虽然从代码看上是分离的, 在多数情况下都需要依赖于其他模块来运行.(单元测试部分内容教我解决这个 ...

  2. C++中,访问字符串的三种方法

    1.用字符数组存放一个字符串 程序1:定义一个字符数组并初始化,然后输出其中的字符串. #include<iostream> using namespace std; int main() ...

  3. Android资源--颜色RGB值以及名称及样图

      颜  色    RGB值 英文名 中文名   #FFB6C1 LightPink 浅粉红   #FFC0CB Pink 粉红   #DC143C Crimson 深红/猩红   #FFF0F5 L ...

  4. iOS绘图教程

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...

  5. Android应用程序框架层和系统运行库层日志系统源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6598703 在开发Android应用程序时,少 ...

  6. maven报错cannot change version of project facet

    用Eclipse创建Maven结构的web项目的时候选择了默认的catalog,由于这个catalog比较老,用的servlet还是2.3,而现在最少也是2.5,所以经常会出现问题,在Projecdt ...

  7. EXPDP和IMPDP简单测试

    一.EXPDP和IMPDP使用说明  Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复.  ...

  8. java序列化ClassNotFoundException

    简单的想从保存的对象中重新解析出对象,用了逆序列化,可是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassL ...

  9. oracle11gr2 netca 无法启动 报错

    安装oracle软件后,必须要先配置listener才能dbca建库,但是netca却报下面的错误. Oracle Net Services Configuration: # # An unexpec ...

  10. 0118——UILabel和导入字体

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 1.设置文字颜色 label.textC ...