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;
}

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

最新文章

  1. 关于mybatis中mapper.xmlSQL语句书写的心得
  2. 在Ubuntu上安装Mysql For Python
  3. R语言学习
  4. 动态链接库(dll)简介(转)
  5. dom 关键字提示
  6. Python--类使用
  7. 用SQL描述树
  8. SDL 实现透明悬浮窗
  9. PHP记录点击数方法
  10. 在SharePoint 2010中部署RBS (转)
  11. Android模拟器
  12. python学习第三讲,python基础语法之注释,算数运算符,变量.
  13. 企业微信自建应用移动端动态获取li并给其事件问题总结
  14. Python丢弃返回值
  15. Python中Socket粘包问题的解决
  16. luogu1345 奶牛的电信 (最小割)
  17. 牛客网NOIP赛前集训营-普及组(第一场)C 括号
  18. 启用sa账号
  19. PHP 定界符
  20. 2018/03/10 每日一个Linux命令 之 cksum

热门文章

  1. Pandoc —— 标记语言转换工具(中文乱码问题)
  2. linux下FAT32格式u盘只读的问题及解决方法
  3. mac 系统 突破百度网盘网速限制
  4. Spring web 工具类 WebApplicationContextUtils
  5. Javascript 获取页面高度(多种浏览器)
  6. ios开发事件处理之:三 :寻找最合适的view
  7. BAT实习内推笔试卷(第一场)——个人答案以及分析
  8. Distribution download cancelled. Using distribution from &#39;https://services.gradle.org/distributions/
  9. BZOJ 2064 - 状压DP
  10. 【BZOJ 1014】 [JSOI2008]火星人prefix