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. Uninstall or Disable Java on a Mac

    You can run Java apps in two ways. The first is to run Java applets inside your Web browser with a p ...

  2. hdu 2822 Dogs(优先队列)

    题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...

  3. C# 让textbox 只能输入数字的方法

    使用textBox控件的KeyPress事件 private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.Key ...

  4. T4运行时模板

    可以通过Visual Studio运行时文本模板在您的应用程序在运行时生成文本字符串. 执行应用程序的计算机不必具有 Visual Studio. 运行库模板有时称为"预处理文本模板&quo ...

  5. PHP学习笔记十六【方法】

    <?php //给一个函数传递基本数据类型 $a=90; $b=90.8; $c=true; $d="hello world"; function test1($a,$b,$ ...

  6. .Net平台下ActiveMQ入门实例(转)

    1.ActiveMQ简介 先分析这么一个场景:当我们在网站上购物时,必须经过,下订单.发票创建.付款处理.订单履行.航运等.但是,当用户下单后,立即跳转到"感谢那您的订单" 页面. ...

  7. POJ 2140 Herd Sums

    http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded w ...

  8. 安装ubuntu14.10系统的那些瞎折腾

    前段时间自作孽,安装了ubuntu14.04的64位系统,而我的笔记本又是那种老古董,2G的内存所以装好之后各种不稳定,索性这个周末就重装一下吧,本来打算是直接装我以前的那个ubuntu12.04-i ...

  9. Kernel Regression from Nando's Deep Learning lecture 5

    require 'torch' require 'gnuplot' , , nData) ) print(xTrain) print(yTrain) local yTrain = yTrain + t ...

  10. jQuery源码学习:使用隐藏的new来创建对象

    在JQuery源码中发现,JQuery定义一个类,但不用new关键字去创建该类对象,而使用方法调用()方式去创建该对象. 很多时候我们是这样写类,然后使用new创建对象的: function Pers ...