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. IOS 用drawRect 画表格

    自定义一个View DrawLine DrawLine.h #import <UIKit/UIKit.h> @protocol gridTouchDelete <NSObject&g ...

  2. vc10的C2664和C2065错误

    在vs2010中编译一个普通的C++程序(Win32 Console Application),都会出现这两个错误! 究其原因是:我们已经习惯了VC6的种种简陋和不规范! 例如,下列程序在VC6中编译 ...

  3. android学习--TabHost选项卡组件

    TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...

  4. 微软提供了三个核心服务:Windows+Office 365+Azure

    微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...

  5. 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)

    我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...

  6. bootstrap 常用类名

    一. 常用类1.container居中的内容展示2.row  行内容显示3.col 列内容显示, 列必须在row 中xs 宽度小于768 ,sm宽度小于990 大于768 ,md 宽度大于990,小于 ...

  7. Android PackageManager基础知识

    一.PackageManagerService启动过程 SystemServer首先启动,创建一个ServerThread线程来启动所有Android核心服务,其中PackageManagerServ ...

  8. ListView加载两种以上不同的布局

    不同的项目布局(item layout) Listview一种单一的item 布局有时候不能完全满足业务需求,我们需要加载两种或两种以上不同的布局,实现方法很简单: 重写 getViewTypeCou ...

  9. 0128——手势Gesture

    UIGestureRecognizer: 1.locationinView 获取手势在某个视图里面的坐标位置 2.delegate监听手势的行为 3.state状态 开始:UIGestureRecog ...

  10. css链接,列表,表格

    1.css链接 a:link - 正常,未访问过的链接 a:visited - 用户已访问过的链接 a:hover - 当用户鼠标放在链接上时 a:active - 链接被点击的那一刻 注意: a:h ...