hdu 3172
http://acm.hdu.edu.cn/showproblem.php?pid=3172
题意:输出每对朋友的关系网大小
并查集的时候维护一个数组记录根节点的大小即可,水题,这题坑在T组数据这个也要读到EOF,开始莫名其妙wa...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <set> using namespace std; int fa[],sum[],cnt[]; int find(int x){
if(fa[x]!=x){
int pre=fa[x];
fa[x]=find(fa[x]);
sum[x]+=sum[pre];
}
return fa[x];
} int main(){
int T;
while(~scanf("%d",&T)){
while(T--){
int n;
scanf("%d",&n);
for(int i=;i<;i++){
fa[i]=i;
cnt[i]=;
}
memset(sum,,sizeof(sum));
map <string,int> mp;
int st=;
while(n--){
char s1[],s2[];
scanf("%s%s",s1,s2);
if(!mp[s1])mp[s1]=st++;
if(!mp[s2])mp[s2]=st++;
int pa=find(mp[s1]);
int pb=find(mp[s2]);
if(pa!=pb){
fa[pa]=pb;
cnt[pb]+=cnt[pa];
}
printf("%d\n",cnt[pb]);
}
}
}
return ;
}
hdu 3172的更多相关文章
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- hdu 3172 Virtual Friends
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...
- 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 (映射并查集)
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(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(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
随机推荐
- 使用Astah繪製UML圖形(转)
http://www.dotblogs.com.tw/clark/archive/2015/02/12/149483.aspx
- AppSettings和ConnectionStrings的区别
AppSettings是ASP.NET1.1时期用的,在.NET Framework 2.0中,新增了ConnectionStrings. 1.<connectionStrings> &l ...
- Objective-C(二、类和对象)
类和对象 #import是include的升级版,可以自动防止重复包含,所以注意:大家以后在引入头文件的时候都使用import Foundation是一个框架,Foundation.h是Founda ...
- double int char 数据类型
贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...
- spring mvc设置字符集过滤器
<filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...
- C# 检测网络链接
;//Local system uses a modem to connect to the Internet. ; //Local system uses a local area network ...
- Tomcat6.0总是运行不了 总是出现Unable to open the service 'Tomcat6'
如果配置没有问题的话,如果你是win7系统,在开始菜单运行Tomcat,运行提示"Unable to open the service ‘tomcat6’"的话,应该是win7的安 ...
- NSString asscii格式(2进制) 转 utf8格式——解决iOS自己处理http socket数据,遇到Transfer-Encoding: chunked时
因为需要实现自己的http客户端,就要自己模拟http 的socket通讯: 上行不难,自己处理好http即可. 但下行时,服务器端的动态语言返回数据有可能会是这种格式: http头 16进制表示的数 ...
- php pthreads 多线程扩展的使用:一个较为稳定例子。
今天研究了worker stackable的配合方法,写了两种形式,虽然能工作,但是都会出现内存不听增长的问题: 于是把第一个方法的代码邮件给了作者,到现在他没有回复我. 我最后放弃两者配合的方式,直 ...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...