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. .htaccess 基础教程(四)Apache RewriteCond 规则参数

    Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量的子规则以及附加条件规则的灵活而且强大的URL操作机制.此UR ...

  2. C++删除目录和复制目录函数

    BOOL DeleteFolder(LPCTSTR lpszPath) { SHFILEOPSTRUCT FileOp; ZeroMemory((void*)&FileOp,sizeof(SH ...

  3. 安卓APP关于切图标

    bin res drawable-hdpi drawable-ldpi drawable-mdpi drawable-nodpi drawable-xhdpi drawable-xxhdpi x越大代 ...

  4. iOS开发——高级篇——iPad开发、iPad开发中的modal

    一.iPad简介 1.什么是iPad一款苹果公司于2010年发布的平板电脑定位介于苹果的智能手机iPhone和笔记本电脑产品之间跟iPhone一样,搭载的是iOS操作系统 2.iPhone和iPadi ...

  5. Java Native Interface 编程系列一

    本文是<Java Native Interface Programmer's Guide and Specification>的读书笔记 Java Native Interface可以让编 ...

  6. mysql连结查询

    2016年4月13日 18:08:22 星期三 union 会生成临时表, 然后一同取出合并 join 或子查询, 会生成临时表进行嵌套循环 临时表, 缺点就是没有索引

  7. 这交互炸了:饿了么是怎么让Image变成详情页的

    这交互炸了:饿了么是怎么让Image变成详情页的 晚上叫外卖,打开饿了么,发现推了一个版本,更新以后,点开了个鸡腿,哇,交互炫炸了. 本文同步自wing的地方酒馆 不过还是有槽点.我是无意中才发现可以 ...

  8. 图片大小以及dp和px关系一览表,logo尺寸

    图片大小以及dp和px关系一览表 说明:根据上表,我们应该很容易算出一张图片在不同手机上的宽和高是多少. 结论 从上表可以得出如下结论 1. 图片放在drawable中,等同于放在drawable-m ...

  9. 第二章 编写与设置Servlet

    2.1 第一个Servlet package cc.openhome; import javax.servlet.ServletException; import javax.servlet.http ...

  10. ubuntu 16.04 设置位wifi热点 方法(手机可链接)亲测可用

    Ubuntu16.04里面可以直接创建热点,而不用像以前的版本,还要其他辅助工具. 具体步骤如下: 1. 点击有上角网络标志,点开编辑链接. 2. 选择 WiFi ,添加一个网络. 3.设置这个网络 ...