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. Swift 语言概览 -自己在Xcode6 动手写2-tableView

    import UIKit class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource { va ...

  2. 用Apache Ivy实现项目里的依赖管理 分类: C_OHTERS 2014-07-06 18:11 564人阅读 评论(0) 收藏

    Apache Ivy是一个管理项目依赖的工具. 它与Maven  Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意.Maven 引入了 JAR 文件公共存储库的概念 ...

  3. 【C++竞赛 C】yyy的数学公式

    时间限制:1s 内存限制:32MB 问题描述 yyy遇到了一个数学问题如下: S_n=∑F(i) 其中F(i)表示i的最大奇因数 yyy的数学非常好,很快就得到了结果.现在他把问题交给你,你能解决吗? ...

  4. JavaEE 三层架构的浅谈

    三层架构 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL).区分层次的目的即为了“高内聚,低 ...

  5. Git使用经验小结

    2012春,开始正式使用SVN,2014年9月加入一起好后,开始使用Git.  有了一些使用经验,也看了下网上关于"Git全胜SVN"的言论. 结合自己的实际情况,简要写几句: 1 ...

  6. [Django] The admin interface

    Now let's see how to access admin interface. 1. Create a super user which can access admin interface ...

  7. [Postgres] Create a Postgres Table

    Learn how to create a table using the most widely-used data types (serial, varchar, integer, float, ...

  8. 编写ATL控件的简单做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 ATL并不像MFC库那样提供了很多的控件窗口类,因此要使用ATL的话需要自己去封装.封装的做法很简单.比如现在我需要一 ...

  9. js获取计算后的样式表

    在编写html时,使用dom对象的style属性可以获取标签里的style属性,但是不能获取单独css样式文件或者style标签的属性值 <div style="width:10px& ...

  10. Indy10 控件的使用(2)TidTCpServer组件学习

    以下来自英文原版帮助文件,文桓英语不好,翻译了老半天.有错误的地方见谅,别骂我. TIdTCPServer = class(TIdComponent) Description TIdTCPServer ...