ll T;

while(~scanf("%d",&T)){

while(T--) {

= = ...

思路:

用秩合并,看了题解才发现 if(fx == fy)要输出当前集合的秩而不是0。。。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <queue>
#include <math.h>
#include <string>
#include <set>
using namespace std;
#define mod 1000000007
#define ll int
#define N 100100
ll r[N], f[N];
ll find(ll x){return x==f[x]?x:f[x] = find(f[x]);}
ll Union(ll x, ll y){
ll fx = find(x), fy = find(y);
if(fx==fy)return r[fx];
if(r[fx]>r[fy])
swap(fx,fy);
f[fx] = fy;
r[fy] += r[fx];
return r[fy];
}
ll n;
void init(){
for(ll i = 0; i < N; i++) r[i] = 1;
for(ll i = 0; i < N; i++) f[i] = i;
}
#define Word_Len 50500
#define Sigma_size 95 struct Trie{
ll ch[Word_Len][Sigma_size]; //Word_Len是字典树的节点数 若都是小写字母Sigma_size=26
ll Have_word[Word_Len]; //这个节点下有几个单词
ll val[Word_Len]; // 这个节点附带的信息。初始化为0表示这个节点不存在单词,所以节点带的信息必须!=0
ll sz ; //当前节点数
ll pre[Word_Len];
char he[Word_Len];
ll Newnode(){memset(ch[sz], 0, sizeof(ch[sz])); val[sz]=Have_word[sz]=0; return sz++;}
void init() //初始化字典树
{ sz = 0; Newnode();}//初始化
ll idx(char c){return c-32;} //字符串编号 ll insert(char *s){ //把v数字加给 s单词最后一个字母
ll u = 0, len = strlen(s);
for(ll i = 0; i < len; i++){
ll c = idx(s[i]);
if(!ch[u][c]) //节点不存在就新建后附加
{
he[sz] = s[i];
val[sz] = val[u]+1;
pre[sz] = u;
ch[u][c] = Newnode();
}
u = ch[u][c];
Have_word[u]++;
} //如今的u就是这个单词的最后一个位置
return u;
}
ll find_word(char *s){
ll u = 0, len = strlen(s);
for(ll i = 0; i < len; i++){
ll c = idx(s[i]);
if(!ch[u][c])return 0; //节点不存在
u = ch[u][c];
}
return Have_word[u];
}
void Have_name(char *s, ll now){
ll len = val[now];
s[len--] = 0;
ll cc = now;
while(cc)
{
s[len--] = he[cc];
cc = pre[cc];
}
}
};
Trie ac;
char a[1005], b[1005];
int main(){
ll T;
while(~scanf("%d",&T)){
while(T--) {
cin>>n;
init();
ac.init();
while(n--)
{
cin>>a>>b;
ll u, v;
u = ac.insert(a);
v = ac.insert(b);
cout<<Union(u,v)<<endl;
}
}
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 3172 Virtual Friends 并与正确集中检查 -秩的更多相关文章

  1. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  2. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  3. hdu 3172 Virtual Friends

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...

  4. hdu 3172 Virtual Friends (映射并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 3172 Virtual Friends(map+并查集)

    Virtual Friends Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tot ...

  6. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  8. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  9. hdu 3172 Virtual Friends (字符串的并查集)

    一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...

随机推荐

  1. WCF走你~异常篇(永久更新...)

    下面是我个人在进行WCF开发时,遇到的问题及相关的解决方法,供大家一起学习 1. ......HTTP 响应时发生错误.这可能是由于服务终结点绑定未使用 HTTP 协议造成的. 解决:把返回的实体类添 ...

  2. Spring Tool Suite(STS)加速

    Java开发首选技术是Spring,使用Spring技术首选的开发工具是STS,STS有许多加速spring开发的提示和快捷方式,并将spring的最新技术通过STS快速简单的传递给用户. 但是STS ...

  3. iOS开发ARC与MRC下单例的完整写法与通用宏定义

    #import "XMGTool.h" /** * 1:ARC下的完整的单例写法:alloc内部会调用+(instancetype)allocWithZone:(struct _N ...

  4. valgrind,arm-linux交叉编译

    1. 下载及解压valgrind-3.9.0 2.CC=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux ...

  5. jquery 多选框 checkbox 获取选中的框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. [Angular Unit Testing] Testing Services with dependencies

    import { Http, Response, ResponseOptions } from '@angular/http'; import { TestBed } from '@angular/c ...

  7. 浅谈python字符串存储形式

    http://blog.csdn.net/zhonghuan1992 钟桓 2014年8月31日 浅谈python字符串存储形式 记录一下自己今的天发现疑问而且给出自己现有知识有的回答. 长话短说,用 ...

  8. 妈蛋:kinMaxShow轮播图异常,WebUploader图片上传坑爹,图片被压缩了

    今天晚上在改造轮播图. 原来的代码是这样的: <div> <img src="${static}/image/index/banner/`.jpg" /> ...

  9. plist文件无法打包进.a静态库中

    问题: 之前一直在做静态库的编写与维护,也一直知道静态库的图片资源是没办法打进.a中的.可是突然有个想法.由于有非常多參数的配置是在一个plist文件里的.尽管也知道这是一个plist文件,可是想想和 ...

  10. iOS 第三方库(1)

    MKNETWORK 被广泛使用的第三方网络访问开源库.用于提供更加友好的网络访问接口.相信很多搞iOS开发的朋友都用过它 RegexKit RegexKit是一个正则表达式工具类.提供强大的正则表达式 ...