*HDU3172 并查集
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8165 Accepted Submission(s): 2345
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.
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).
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.
//用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 并查集的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- CSS-dl+dt+dd的应用(非常实用)
http://smallpig301.blog.163.com/blog/static/9986093201010262499229/
- rails enum用于存储数据
http://api.rubyonrails.org/classes/ActiveRecord/Enum.html 新的项目中有一个字段是展示类型,可以用下拉框去做,用string存储具体的类型字段. ...
- uoj228 基础数据结构练习题
趁别人题解没有放出来赶快写一篇 整数序列,操作 区间加 区间变成sqrt(下取整) 区间和 考虑一下对于每个区间里所有sqrt不同的段操作,那么可以在O(段数logn)一次的时间内完成sqrt操作.考 ...
- mysql datatime 设置默认值为CURRENT_TIMESTAMP报错`Invalid default value`
环境: Ubuntu mysql 5.5 解决方法: 升级mysql至5.6 apt-cache search mysql-server sudo apt-add-repository ppa:ond ...
- C++库(Google Breakpad)
Google Breakpad是什么? 一个开源的多平台崩溃报告系统. Google breakpad是一个非常实用的跨平台的崩溃转储和分析模块,它支持Windows,Linux和Mac和Solari ...
- 继承的小DEMO
代码如下 Student.java package four.com; public class Student extends Person { // private String name; // ...
- python--基础学习(五)参数位置传递、关键字传递、包裹传递及解包裹
python系列均基于python3.4环境 1.位置传递和关键字传递 代码示例 #位置传递 def fun(a,b,c): print("a: {0}, b: {1}, c: {2}&qu ...
- 【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】
一.struts2系统验证 1.基于struts2系统验证的方式实际上就是通过配置xml文件的方式达到验证的目的. 2.实际上系统校验的方法和手工校验的方法在底层的基本实现是相同的.但是使用系统校验的 ...
- 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】
一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...