(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 ...
随机推荐
- scratch少儿编程第一季——06、人在江湖混,没有背景怎么行。
各位小伙伴大家好: 到上期我们学习了动作模块的全部指令.接下我们用动作模块做一个小小项目,来总结我们前面学的内容. 在做项目之前我们先来换一个背景. 在左下角舞台区,点击打开背景库,选择自己所需要的背 ...
- react中jsx文件是如何转换成js对象的
通过在线babel转换器,转换出jsx是如何变成js对象的 jsx文件 加入了正常的标签以及嵌套标签以及方法属性 function hello() { click=()=>{ console.l ...
- Spring Boot 多个域名指向同一IP
一.需求:直接通过域名访问首页(同一应用下,多个首页,包括PC端.手机端首页) 方法:采用多个域名绑定同一IP下同一应用,不同域名对应不同产品(PC.手机端)的方法,在后台通过拦截器判断 reques ...
- POJ2729 Robocode(离散化与模拟-----提醒曾经爱玩游戏的自己没做出这个
题目链接 :http://poj.org/problem?id=2729 题目很长,有不少也是废话.类似小时候玩的坦克大战.每个坦克速度为10,炮弹速度为20.子弹出界就消失,坦克出不了界限.相向的子 ...
- Vue中遍历数组的新方法
1.foreach foreach循环对不能使用return来停止循环 search(keyword){ var newList = [] this.urls.forEach(item =>{ ...
- 根据导入xlxs的文件,来写入数据库
今天讲解一下上传文件.前台必须保持传参类型"multipart/form-data" 后台可以设定 public static final String MULTIPART_FOR ...
- FPGA上外挂DDR2&DDR3&MIG IP的使用记录
前言 当需要大容量数据存储及处理的时候,FPGA内部自带的存储资源是远远不够的,所以问题来了,怎么使用外带的DDR3? 首要问题在于DDR3是什么?有没有协议?当然只是需要用Xilinx MIG IP ...
- leetcode-3 最长无重复字串
3. Longest Substring Without Repeating Characters 题面 Given a string, find the length of the longest ...
- 6.NIO2-Path、Paths、Files
NIO.2 jdk1.7中,java对 NIO 极大的扩展,主要增强的是对文件处理 和 文件系统特性的支持 关于其中一些API的使用 public class TestNIO_2_Path_File ...
- MySQL无法启动问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
今天重启一台内网服务器,发现mysql无法正常重启,执行systemctl start mysql,报错如下 Starting LSB: start and stop MySQL... Dec 11 ...