二次联通门 : 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. 使用Duilib开发Windows软件(5)——使用VLC做视频播放

    需求:调用PC上的摄像头拍照. 实现思路:接入视频流,截屏获取照片. 早期的vlc安装包(Windows)是附带sdk包的,现在的安装后已经没有了,原因如下: VLC SDK下载连接:https:// ...

  2. SpringBoot exception异常处理机制源码解析

    一.Spring Boot默认的异常处理机制 1:浏览器默认返回效果 2:原理解析 为了便于源码跟踪解析,在·Controller中手动设置异常. @RequestMapping(value=&quo ...

  3. .net core mvc + mysql dbfirst 生成 ado.net 数据模型

    1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 安装一下包 Install-Package MySql.Data.EntityFrameworkCore -Pre I ...

  4. mysql8中查询语句表别名不能使用 “of”

    今天在迁移一个项目的时候,发现有一个sql报错,但是语句跟迁移之前完全一样,所以想来应该是 mysql 版本差异导致的. 迁移之前版本:5.6.28(腾讯云) 迁移之后版本:8.0.16(阿里云) 新 ...

  5. POJ1988(Cube Stacking)--并查集

    题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问 ...

  6. c#NPOI导出2007版本excel

    2003和2007版本区别: HSSFWorkbook(2003) IWorkbook(2007版本) 写完之后会有个问题,导出会报错[流已关闭]. NPOI生产.xlsx文件件时,在使用book.W ...

  7. win server服务器 关闭危险端口 135,137,138,139,445的方法

    Windows默认开放135.137.138.139和445五个端口,都与文件共享和打印机共享有关的,若机器连接网络后会在用户不知道的情况下泄露本机部分信息,这样会给用户带来一部分危险,所以我们在工作 ...

  8. 【Day5】2.反爬策略之代理IP

    import urllib.request as ur proxy_address = ur.urlopen('http://api.ip.data5u.com/dynamic/get.html?or ...

  9. centos7下postgresql数据库安装及配置

    1.安装 #yum install -y postgresql-server 2.postgresql数据库初始化 #service postgresql initdb 3.启动postgresql服 ...

  10. 用js刷剑指offer(重建二叉树)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...