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. Javascript 中的变量

    var a; console.log("The value of a is " + a); // The value of a is undefined console.log(& ...

  2. URLConnection的连接、超时、关闭用法总结

    Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET与POST请求.二者的区别在于:      a:) get请求可以获取静态页面,也可以把 ...

  3. [HeadFirst-HTMLCSS学习笔记][第十二章HTML5标记]

    考虑HTML结构 HTML5即是把原来<div>换成一些更特定的元素.能够更明确指示包含什么内容. (页眉,导航,页脚,文章) article nav 导航 header footer t ...

  4. 用cflow工具生成代码函数调用关系

    1. 安装 sudo apt-get install cflow 2.使用 cflow [options...] [file]... 例: cflow main.c 生成main.c文件例的函数调用关 ...

  5. 面试前的准备---C#知识点回顾----02

    经过昨天大量的简历投递,今天陆续收到面试邀约,明日准备大战一场,是死是活一试便知 1.数据库的范式 这算入门问题了吧,但凡是个数据库类的,都得问吧, 但我们在回答的时候开始背书啦 第一范式(1NF)无 ...

  6. 带CheckBox的TreeView网上出错问题解决办法

    问题描述:TreeView上传到服务器,预览效果发现节点图片不显示.展开合并功能缺失.解决办法: 以下是我个人的解决办法,最终的效果实现了,但是还有一点点小遗憾,就是页面上有基于微软的调用js的报错信 ...

  7. LinearLayout使用简单实例

    1.代码 import android.annotation.SuppressLint; import android.app.Activity; import android.app.ActionB ...

  8. 微软TTS,Neospeech TTS 简单使用

    今天搞了下微软的TTS,逛了好多网页.博客,拼拼凑凑搞了点东西吧. 首先添加类库调用,系统自带的system.speech using System.Speech.Synthesis; 然后就能调用方 ...

  9. CentOS安装配置ganglia

    1.     下载ganglia源码包并解压 wget http://sourceforge.net/projects/ganglia/files/ganglia%20monitoring%20cor ...

  10. Js 日期处理

    Js获取当前日期时间 var myDate = new Date(); myDate.getFullYear();    //获取完整的年份(4位,1970-????)  2014myDate.get ...