HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址: pid=3172">HDU 3172
带权并查集水题。每次合并的时候维护一下权值。注意坑爹的输入。
。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL long long
int bin[210000], num[210000];
int find1(int x)
{
return bin[x]==x? x:bin[x]=find1(bin[x]);
}
void join(int x, int y)
{
int f1=find1(x);
int f2=find1(y);
if(f1!=f2)
{
bin[f2]=f1;
num[f1]+=num[f2];
printf("%d\n",num[f1]);
}
else
printf("%d\n",num[f2]);
}
int main()
{
int t, n, i, cnt;
char s1[30], s2[30];
map<string,int>q;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
for(i=1; i<210000; i++)
{
bin[i]=i;
num[i]=1;
}
q.clear();
cnt=1;
while(n--)
{
getchar();
scanf("%s%s",s1,s2);
if(!q[s1])
q[s1]=cnt++;
if(!q[s2])
q[s2]=cnt++;
join(q[s1],q[s2]);
}
}
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
HDU 3172 Virtual Friends(并用正确的设置检查)的更多相关文章
- POJ 1703:Find them, Catch them(并用正确的设置检查)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30702 Accepted: ...
- HDU 3172 Virtual Friends 并与正确集中检查 -秩
ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集 ...
- HDU 3172 Virtual Friends (map+并查集)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- hdu 3172 Virtual Friends
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...
- hdu 3172 Virtual Friends (映射并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3172 Virtual Friends(map+并查集)
Virtual Friends Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tot ...
- hdu 3172 Virtual Friends (并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
随机推荐
- poj 1789 Truck History(kruskal算法)
主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...
- FileStream:The process cannot access the file because it is being used by another process
先看下面一段代码(先以共享的方式打开文件读写,然后以只读的方式打开相同文件): FileStream fs = new FileStream(filePath, FileMode.Open, Fil ...
- top 查看资源使用
top:动态观察程序的变化 ? [root@linux ~]# top [-d] | top [-bnp] 参数: -d :后面可以接秒数,就是整个程序画面更新的秒数.预设是 5 秒: -b :以批次 ...
- Zookeeper实践方案:(4)命名服务
1.基本介绍 命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper非常easy创建一个全局的路径,而这个路径就能够作为一个名字.它能够指向集群中的集群.提供的服务的 ...
- 基于lua的网页脚本开发语言cgilua(转)
这里为大家介绍基于lua脚本实现的网页开发语言,cgilua 介绍 cgilua使用Lua是一个用于创建动态网页的服务器端脚本语言.纯LUA脚本和LUA页(LP)的支持,cgilua.Lua脚本是一个 ...
- Dictionary带来的一种隐式内存泄漏
当心Dictionary带来的一种隐式内存泄漏 最近在看Dictionary的源代码的时候, 突然想到Dictionary的不当使用中有一种隐含内存泄漏的可能. 简化使用场景 小A正在写一个简单的图书 ...
- Directx11学习笔记【十四】 使用最新的Effect框架和SDK
由于之前一直在看directx11龙书学习,因此sdk一直用的Microsoft DirectX SDK (June 2010) 版本,最近在stackoverflow上问dx11相关问题时,一直被大 ...
- (ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询!
(ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询! 直接在源代码: 定义全局变量: int index=0; /// & ...
- android使用XmlPullParser来解析XML文件
解析下面的一个XML: <?xml version="1.0" encoding="utf-8" ?> <rss><sid> ...
- 解决Virtual Box 安装Mac OS X当出现“hfs: summary table not allowed on FS with block size of 2048”问题
解决Virtual Box 安装Mac OS X当出现"hfs: summary table not allowed on FS with block size of 2048"问 ...