hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数。
分析:用并查集,其实就是求每个集合当前的人数。对于人名的处理用到了字典树。
注意:1、题目给出的n是指n对关系,上限有2*n个人。
2、题目很没有意思的既说了有多组数据,又要求输入组数。实际上还是多组数据。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN=; struct Node{
int c;
Node *child[];
Node(){
c=;
for(int i=;i<;i++)
child[i]=NULL;
}
}*root; int cnt;
int p[MAXN],num[MAXN];
char str1[],str2[]; void init(int n)
{
root=new Node();//
cnt=; for(int i=;i<n;i++)
{
p[i]=i;
num[i]=;
}
} int ID(char str[])
{
Node *p=root;
int len=strlen(str);
for(int i=,k;i<len;i++,p=p->child[k])
{
if(str[i]>='a'&&str[i]<='z')
k=str[i]-'a';
else
k=str[i]-'A'+;
if(p->child[k]==NULL)
p->child[k]=new Node();
}
if(p->c)
return p->c;
return p->c=++cnt;//
} int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
} int main()
{
int T,n;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d",&n); init(n*);//
for(int i=;i<n;i++)
{
scanf("%s%s",str1,str2);
int u=ID(str1);
int v=ID(str2); int fu=find(u);
int fv=find(v); if(fu==fv)
printf("%d\n",num[fu]);
else {
p[fv]=fu;
num[fu]+=num[fv];
printf("%d\n",num[fu]);
} }
}
}
return ;
}
hdu 3172 Virtual Friends(并查集,字典树)的更多相关文章
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- POJ-2513 Colored Sticks---欧拉回路+并查集+字典树
题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...
- 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线
这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题 坦克大战 struct node { int x,y,step; friend bool operator <(no ...
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- UVA1455 - Kingdom(并查集 + 线段树)
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...
随机推荐
- 阿里云服务器,tomcat启动,一直卡在At least one JAR was scanned for TLDs yet contained no TLDs就不动了
项目在本地是可以成功运行的,网上看到一堆各式各样的解决办法感觉都不适合我,于是绝望的删webapps,重新上传,一直不行. 重复了第3次还是第4次,居然就好了,这是什么操作.
- (15)C#集合
http://blog.csdn.net/hcw_peter/article/details/3980723 集合分为非泛型集合和泛型集合 ,泛型集合可以指定放入集合中的类型. 一.非泛性集合 引用命 ...
- Number Triangles
题目描述 Consider the number triangle shown below. Write a program that calculates the highest sum of nu ...
- Xamarin XAML语言教程Xamarin.Forms中构建进度条
Xamarin XAML语言教程Xamarin.Forms中构建进度条 ProgressBar被称为进度条,它类似于没有滑块的滑块控件.进度条总是水平放置的.本节将讲解如何使用进度条. 注意:进度条在 ...
- Git从入门到速成
什么是Git Git是Linux发明者Linus开发的一款新时代的版本控制系统,那什么是版本控制系统呢?怎么理解?网上一大堆详细的介绍,但是大多枯燥乏味,对于新手也很难理解,这里我只举几个例子. 熟悉 ...
- Moving Average from Data Stream -- LeetCode
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- COCOS2d 标准 android.MK
LOCAL_PATH := $(call my-dir) include$(CLEAR_VARS) LOCAL_MODULE := game_shared PP_CPPFLAGS := -frtti ...
- 四. Java继承和多态4. 多态和动态绑定
在Java中,父类的变量可以引用父类的实例,也可以引用子类的实例. 请读者先看一段代码: public class Demo { public static void main(String[] ar ...
- 每天一个liunx命令10之nohup和xargs
1上传jar包到服务器/edgewalk/springboot/下 2编写启动脚本start.sh #!/bin/sh APP_HOME=/edgewalk/springboot cd $APP_HO ...
- Android Rx Android实战
上一次捣蛋 RxAndroid 是今年二月份的事情了,当时 RxAndroid 还处于一个资料甚少交流难的状态,当时还特意建了一个交流群,让搞这个的人可以加进来讨论讨论,毕竟这玩意还是挺有意思的,于是 ...