Virtual Friends

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

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
 
Source
 
题意:
朋友的朋友是朋友,每输入一对朋友后问一共有几对朋友。
代码:
 //用map把字符串转化为数字。初始化时初始2*n个会超时。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int t,n;
int num[],fat[];
map<string,int>m;
int find(int x)
{
int rt=x;
while(fat[rt]!=rt)
{
rt=fat[rt];
}
int i=x,j;
while(i!=rt)
{
j=fat[i];
fat[i]=rt;
i=j;
}
return rt;
}
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
{
fat[xx]=yy;
num[yy]+=num[xx];
printf("%d\n",num[yy]);
}
else printf("%d\n",num[yy]);
}
int main()
{
char ch1[],ch2[];
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d%d",&n);
for(int i=;i<=;i++) //开大了会超时
{
fat[i]=i;
num[i]=;
}
m.clear();
int cnt=;
for(int i=;i<=n;i++)
{
scanf("%s%s",ch1,ch2);
if(!m[ch1]) m[ch1]=cnt++;
if(!m[ch2]) m[ch2]=cnt++;
connect(m[ch1],m[ch2]);
}
}
}
return ;
}

*HDU3172 并查集的更多相关文章

  1. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  2. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  5. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  6. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. 如何学习JavaScript

    Javascript是我大学里面做网站兴趣,加上进一年维护公司javascript相关的框架. 顺便回顾一下自己学习 javascript 的相关方法和技巧,分享给需要的朋友. 1.base 基础.兼 ...

  2. silverlight_容器

    Canvas絕對佈局 定位Canvas.Top.Canvas.Left ZIndex屬性 若制定2個控制項重疊,那麼後聲明控制項會覆蓋先聲明的控制項.此時可以使用Canvas.ZIndex屬性來改變它 ...

  3. PHP简单 对象(object) 与 数组(array) 的转换

    数组是PHP的灵魂,非常强大,但有时候面向对象编程也是挺方便的,数组 与 对象 之间切换也是常有的事: /** * 数组 转 对象 * * @param array $arr 数组 * @return ...

  4. touch移动触屏滑动事件

    移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件.处理touch事件 ...

  5. MySql连接数据库和操作(java)

    package org.wxd.weixin.util; import java.sql.Connection;import java.sql.DriverManager;import java.sq ...

  6. asp.net mvc5 伪静态

    asp.net mvc5 伪静态 WebForm Mvc4和5通用 1.背景:老项目WebForm开发 需要 融合到新项目Mvc5开发 2.需求:Url地址TruckDetail.aspx?id=45 ...

  7. 整合Spring、SpringMVC、MyBatis

    spring+springmvc+mybatis集成 一个核心:将对象交给spring管理. 1新建web项目 2添加项目jar包 spring包见上一篇博客 3建立项目的目录结构 4完成Mapper ...

  8. Swift - UIColor16进制编码与RGB格式互相转换

    Swift UIColor 16进制编码转换RGB : 由于UI出图的时候,通常给的是16进制的编码颜色,我们在开发的时候需要将它转换为RGB格式,现在给出两种代码片段. 一.对UIColor进行扩展 ...

  9. 基于Struts2CRUD的质量属性

    基于struts2框架开发的<学生管理系统>的质量属性 我们经常重新设计系统,可能不是因为该系统在功能上有缺陷,而是由于:系统运行速度太慢.系统容易受到外界攻击.用另外的一句话说:我们修改 ...

  10. CozyRSS开发记录3-标题栏再加强

    CozyRSS开发记录3-标题栏再加强 1.更精炼的标题栏 接下来,我们把窗口的边框和默认的标题栏给去掉,让Cozy看起来更像一个平板应用. 在主窗口的属性里,修改下列两个属性: 效果一目了然: 2. ...