(RERERERERERERERERERERE) BZOJ 2746: [HEOI2012]旅行问题
二次联通门 : BZOJ 2746: [HEOI2012]旅行问题
神TM STL的vector push_back进一个数后取出时就变成了一个很小的负数。。
调不出来了, 不调了
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue> #define Max 100 #define Mod 1000000007LL void read (int &now)
{
now = ;
register char word = getchar ();
while (word < '' || word > '')
word = getchar ();
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
} int N, M; struct T_D
{ T_D *child[]; T_D *Fail;
int number; long long value; T_D ()
{
for (int i = ; i < ; i ++)
this->child[i] = NULL; Fail = NULL;
value = ;
number = ;
}
}; inline int swap (int &x, int &y)
{
int now = x;
x = y;
y = now; } std :: vector <int> List[Max << ]; T_D *map[Max]; class Tree_Chain_Get_Type
{ private : int size[Max];
int deep[Max]; int father[Max]; int __to[Max];
int __next[Max]; int Edge_Count;
int edge_list[Max]; int top[Max];
int Count; public : inline void Add_Edge (int from, int to)
{
Edge_Count ++; __to[Edge_Count] = to;
__next[Edge_Count] = edge_list[from];
edge_list[from] = Edge_Count; Edge_Count ++;
__to[Edge_Count] = from;
__next[Edge_Count] = edge_list[to];
edge_list[to] = Edge_Count; } void Dfs_1 (int now, int __father)
{ int pos = Count ++; deep[now] = deep[__father] + ;
father[now] = __father; for (int i = edge_list[now]; i; i = __next[i])
if (__to[i] != __father)
Dfs_1 (__to[i], now); size[now] = Count - pos;
} void Dfs_2 (int now, int chain)
{
int pos = ;
top[now] = chain; for (int i = edge_list[now]; i; i = __next[i])
if (__to[i] != father[now] && size[pos] < size[__to[i]])
pos = __to[i]; if (!pos)
return ;
Dfs_2 (pos, chain);
for (int i = edge_list[now]; i; i = __next[i])
if (__to[i] != father[now] && __to[i] != pos)
Dfs_2 (__to[i], __to[i]);
} int Get_Lca (int x, int y)
{
for (; top[x] != top[y]; x = father[top[x]])
if (deep[top[x]] < deep[top[y]])
swap (x, y); return deep[x] < deep[y] ? x : y;
} inline void Prepare ()
{
Count = ;
Dfs_1 (, );
Count = ;
Dfs_2 (, );
Count = ;
}
}; Tree_Chain_Get_Type Make; class AC_Type
{ private : T_D *Root;
int Count; public : AC_Type ()
{
Root = new T_D;
Count = ;
Root->number = ++ Count;
map[Root->number] = Root;
} void Insert (char *key)
{
T_D *now = Root, *pos; int Len = strlen (key); int Id;
for (int i = ; i < Len; i ++)
{
Id = key[i] - 'a';
if (now->child[Id] == NULL)
{
now->child[Id] = new T_D;
now->child[Id]->number = ++ Count;
map[Count] = now->child[Id];
now->child[Id]->value = (now->value * 26LL + Id) % Mod;
}
List[i].push_back (now->child[Id]->number);
now = now->child[Id];
}
} void Build_AC ()
{
std :: queue <T_D *> Queue; Queue.push (Root);
T_D *now, *pos;
while (!Queue.empty ())
{
now = Queue.front ();
Queue.pop (); pos = NULL; for (int i = ; i < ; i ++)
{
if (now->child[i] == NULL)
continue;
if (now == Root)
now->child[i]->Fail = Root;
else
{
for (pos = now->Fail; pos; pos = pos->Fail)
if (pos->child[i])
{
now->child[i]->Fail = pos->child[i];
break;
}
if (pos == NULL)
now->child[i]->Fail = Root;
}
Queue.push (now->child[i]);
Make.Add_Edge (now->child[i]->number, now->child[i]->Fail->number);
}
}
} }; AC_Type AC; char line[Max]; int main (int argc, char *argv[])
{
int x, y; read (N); for (int i = ; i <= N; i ++)
{
scanf ("%s", line);
AC.Insert (line);
} AC.Build_AC (); Make.Prepare ();
int x_1, y_1, x_2, y_2; for (read (M); M --; )
{
read (x_1);
read (y_1);
read (x_2);
read (y_2); int now = Make.Get_Lca (List[x_1][y_1 - ], List[x_2][y_2 - ]);
printf ("%lld\n", map[now]->value);
} return ;
}
(RERERERERERERERERERERE) BZOJ 2746: [HEOI2012]旅行问题的更多相关文章
- bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树
2746: [HEOI2012]旅行问题 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 489 Solved: 174[Submit][Status ...
- BZOJ 2746: [HEOI2012]旅行问题
2746: [HEOI2012]旅行问题 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 921 Solved: 291[Submit][Status ...
- [bzoj2746][HEOI2012]旅行问题 _AC自动机_倍增
[HEOI2012]旅行问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2746 题解: 这个是讲课时候的题. 讲课的时候都在想怎么后 ...
- AC日记——[HEOI2012]旅行问题 bzoj 2746
2746 思路: 建立ac自动机,然后把fail树抽出来: 然后在fail树上走lca(神奇): 代码: #include <cstdio> #include <vector> ...
- 旅行问题(bzoj 2746)
Description yz是Z国的领导人,他规定每个地区的名字只能为26个小写拉丁字母的一个.由于地 区数有可能超过26个,便产生了一个问题,如何辨别名字相同的地区?于是yz规定,一个 地区的描述必 ...
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树
题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...
- BZOJ 3531: [Sdoi2014]旅行 [树链剖分]
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1685 Solved: 751[Submit][Status] ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
随机推荐
- nginx 设置开机启动
设置nginx开机启动chkconfig --add /etc/init.d/nginx chkconfig nginx on
- 【数据结构】Tournament Chart
Tournament Chart 题目描述 In 21XX, an annual programming contest, Japan Algorithmist GrandPrix (JAG) has ...
- jira索引失败
""" # 参考:http://www.mamicode.com/info-detail-2369087.html jira断电重启后索引失败, 解决方法: 关闭jira ...
- 在论坛中出现的比较难的sql问题:4(row_number函数+子查询 分组连续编号问题)
原文:在论坛中出现的比较难的sql问题:4(row_number函数+子查询 分组连续编号问题) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 求一查询语句 http: ...
- 傅里叶变换通俗解释及快速傅里叶变换的python实现
通俗理解傅里叶变换,先看这篇文章傅里叶变换的通俗理解! 接下来便是使用python进行傅里叶FFT-频谱分析: 一.一些关键概念的引入 1.离散傅里叶变换(DFT) 离散傅里叶变换(discrete ...
- Marketing Cloud的contact merge机制
Marketing Cloud的contact支持多种多样的数据源,如下图所示: SAP Hybris Commerce SAP ERP SAP Cloud for Customer SAP Gigy ...
- crunch离线密码生成
Crunch是一种创建密码字典工具,按照指定的规则生成密码字典,可以灵活的制定自己的字典文件. 一.Crunch为kali自带工具之一在kali环境下进行,默认基于26个小写英文字母. 语法:cru ...
- centos7安装harbor
harbor是什么? docker容器是集装箱,harbor就是放集装箱的港湾. 一.下载软件: github上选择对应版本下载 https://github.com/goharbor/harbor/ ...
- vue中用div的contenteditable属性实现v-for遍历,双向数据绑定的动态表格编辑
1.HTML部分 <tr v-for="(item,index) in customerVisitList2" :key="index"> < ...
- 浅谈JAVA继承关系中的构造函数
话不多说直接上例子,我的例子中定义了两个类,TheSon和TheFather,TheSon继承了TheFather,如图: TheSon类的定义: TheFather类的定义: 当我们初始化The ...