二次联通门 : 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]旅行问题的更多相关文章

  1. bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树

    2746: [HEOI2012]旅行问题 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 489  Solved: 174[Submit][Status ...

  2. BZOJ 2746: [HEOI2012]旅行问题

    2746: [HEOI2012]旅行问题 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 921  Solved: 291[Submit][Status ...

  3. [bzoj2746][HEOI2012]旅行问题 _AC自动机_倍增

    [HEOI2012]旅行问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2746 题解: 这个是讲课时候的题. 讲课的时候都在想怎么后 ...

  4. AC日记——[HEOI2012]旅行问题 bzoj 2746

    2746 思路: 建立ac自动机,然后把fail树抽出来: 然后在fail树上走lca(神奇): 代码: #include <cstdio> #include <vector> ...

  5. 旅行问题(bzoj 2746)

    Description yz是Z国的领导人,他规定每个地区的名字只能为26个小写拉丁字母的一个.由于地 区数有可能超过26个,便产生了一个问题,如何辨别名字相同的地区?于是yz规定,一个 地区的描述必 ...

  6. BZOJ 3091: 城市旅行 [LCT splay 期望]

    3091: 城市旅行 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1454  Solved: 483[Submit][Status][Discuss ...

  7. BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树

    题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...

  8. BZOJ 3531: [Sdoi2014]旅行 [树链剖分]

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1685  Solved: 751[Submit][Status] ...

  9. BZOJ 2743: [HEOI2012]采花

    2743: [HEOI2012]采花 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 2056  Solved: 1059[Submit][Status ...

随机推荐

  1. 怎样理解没有this的构造函数

    如果一个构造函数内部没有this, 那可以说这个压根不是构造函数, 因为他并不能返回一个实例对象; function Person(name) { var name = name; }; var li ...

  2. 数据结构-平衡二叉树Java实现

    1,Node.java package com.cnblogs.mufasa.BalanceBinaryTree; public class Node { Node parent; Node left ...

  3. 通过DBCC Page查看在SQL Server中哪行数据被锁住了?

    原文:通过DBCC Page查看在SQL Server中哪行数据被锁住了? 如何查看被锁的是哪行数据?通过dbcc page可以. 要想明白这个问题: 首先,需要模拟阻塞问题,这里直接模拟了阻塞问题的 ...

  4. 笔记: ASP.NET Core视图组件

    视图组件 asp.net core mvc 提供了部分视图的新替代品:视图组件. 视图组件与分布视图的主要区别在于视图组件与控制器不相关.可使用在独立于单个控制器的场景,如:菜单导航.侧边栏.分页栏等 ...

  5. .NET Core 常用第三方包

    .NET Core 常用第三方包 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10845370.html 写在前面 最近在学习.NET Core 中经常用 ...

  6. java01_简介_开发环境

    JAVA的前世今生 美国SUN(Stanford University Network)公司,在中国大陆的正式中文名为"太阳计算机系统(中国)有限公司",在中国台湾的正式中文名为& ...

  7. Mysql 中删除重复数据(保留一条)

    sql去重 先根据需要去重的字段进行分组,取到主键值最小的记录(id 是主键,删除重复的 record_id 的记录) select min(id) from tb_traffic_wf_record ...

  8. Linux Centos7配置mysql8.0数据库

    本文转至:672530440 在此感谢博主,撒花!!! 本文主要从以下几个方面对自己在centos7 下安装mysql8过程做如下总结: CentOS7 安装mysql8 步骤: window下的Na ...

  9. C++线程同步之原子操作

    所谓的原子操作就是指一个线程对于某一个资源做操作的时候能够保证没有其它的线程能够对此资源进行访问. 原子操作仅仅能够解决某一个变量的问题,只能使得一个整型数据做简单算术运算的时候是原子的. 以下案例需 ...

  10. vscode 使用ESLint 自动检查,保存时自动格式化

    1:全局安装eslint `npm install -g eslint`2: 打开vscode 点击 “文件”----->“首选项”---->“设置”,在右侧“用户设置/settings. ...