HDU 3172 Virtual Friends 并与正确集中检查 -秩
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 并与正确集中检查 -秩的更多相关文章
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- 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.题目 ...
- hdu 3172 Virtual Friends (字符串的并查集)
一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...
随机推荐
- Behavioral模式之Memento模式
1.意图 在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. 2.别名 Token 3.动机 有时候有必要记录一个对象的内部状态.为 ...
- [Node.js] Create a model to persist data in a Node.js LoopBack API
In this lesson you will learn what a LoopBack model is, you will create a Product model using the Lo ...
- [tmux] Handle history in tmux sessions
In this lesson, we'll look at how to manage your history between tmux sessions, and ensure that your ...
- java基本类型(内置类型)取值范围
例1: public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.pri ...
- PHP移动互联网开发笔记(7)——MySQL数据库基础回顾[1]
一.数据类型 1.整型 数据类型 存储空间 说明 取值范围 TINYINT 1字节 非常小的整数 带符号值:-128~127无符号值:0~255 SMALLINT 2字节 较小的整数 带符号值:-32 ...
- Git提交到多个远程仓库(多看两个文档)
Git提交到多个远程仓库(多看两个文档) 一.总结 一句话总结: 二. Git提交到多个远程仓库(多看两个文档) 有两种做法,先看第一种 一.通过命令行进行操作 例如我有下面两个仓库: Mybatis ...
- .netcore consul实现服务注册与发现-单节点部署
原文:.netcore consul实现服务注册与发现-单节点部署 一.Consul的基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分 ...
- Erlang与ActionScript3采用JSON格式进行Socket通讯
http://hideto.iteye.com/blog/235811 需要下载as3corelib来为ActionScript3处理JSON codec server.erl -module(ser ...
- 【codeforces 782B】The Meeting Place Cannot Be Changed
[题目链接]:http://codeforces.com/contest/782/problem/B [题意] 每个人都有一个速度,只能往上走或往下走; 然后让你找一个地方,所有人都能够在t时间内到达 ...
- Docker Redis 安装
原文:Docker Redis 安装 拉取官方的镜像 docker pull redis:3.2 查看 docker images redis 运行容器 docker run -p 6379:6379 ...