Antonidas

Time Limit: 4000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 5469
64-bit integer IO format: %I64d      Java class name: Main

 

Given a tree with N vertices and N−1 edges. Each vertex has a single letter Ci. Given a string S, you are to choose two vertices A and B, and make sure the letters catenated on the shortest path from A to B is exactly S. Now, would you mind telling me whether the path exists?

Input
The first line is an integer T, the number of test cases.
For each case, the first line is an integer N. Following N−1 lines contains two integers a and b, meaning there is an edge connect vertex a and vertex b.
Next line contains a string C, the length of C is exactly N. String C represents the letter on each vertex.
Next line contains a string S.
$1\leq T\leq 200, 1\leq N\leq 10^4, 1\leq a,b\leq N, a\not = b, |C|=N, 1\leq |S|\leq 10^4$. String C and S both only contain lower case letters.

Output
First, please output "Case #k: ", k is the number of test case. See sample output for more detail.
If the path exists, please output “Find”. Otherwise, please output “Impossible”.

Sample Input

2

7
1 2
2 3
2 4
1 5
5 6
6 7
abcdefg
dbaefg 5
1 2
2 3
2 4
4 5
abcxy
yxbac

Sample Output

Case #1: Find
Case #2: Impossible

Source

 
解题:点分治+hash
 debug大半天的成果啊
比如一个目标串是$abcdefg$,假设我们树分治的时候,选的根恰好是$d$,那么我们需要的怎样的hash信息?是不是从$d->a$以及$d->g$的hash信息啊,注意方向。d才是高位!!!
所以我们要预处理出来目标串每个字符么,向左走到端点已经向右走到端点的hash值。
在树分治的时候,我们可以从根到当前点,走出一条路,路上的点就会hash出一个值,根据深度,我们可以看看它跟这个长度的前缀配不配,或者跟这个长度的后缀配不配,配的话,我们要分别找剩下长度的后缀或者前缀是否存在。
比放说根就是$d$,当前走到深度4,那么我们就可以看看目标串从4位置hash到 1位置的hash跟当前路径的hash值等否?如果相等,那么需要一段什么样的值呢?
需要一个后缀,从d开始,走到g这段的hash值,是否在前面的搜索过程中出现过,如果出现过,那么好了a到d的有了,d到g的也有了,那么a到g的就有了,答案就找到了。
 #include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
const int maxn = ;
const ULL Base = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[maxn<<];
bool vis[maxn];
int head[maxn],sz[maxn],maxson[maxn],pre[maxn],suf[maxn],tot,len,cnt;
ULL Pre[maxn],Suf[maxn],B[maxn] = {};
char sa[maxn],sb[maxn];
void add(int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
dfs(e[i].to,u);
sz[u] += sz[e[i].to];
maxson[u] = max(maxson[u],sz[e[i].to]);
}
}
int FindRoot(int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
int x = FindRoot(sum,e[i].to,u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
bool cao(int u,ULL w,int fa,int depth,bool op) {
if(depth > len) return false;
w = w*Base + (ULL)sa[u];
if(op) {
if(w == Pre[depth] && suf[depth] == cnt) return true;
if(w == Suf[len - depth + ] && pre[len - depth + ] == cnt) return true;
} else {
if(w == Pre[depth]) pre[depth] = cnt;
if(w == Suf[len - depth + ]) suf[len - depth + ] = cnt;
}
for(int i = head[u]; ~i; i = e[i].next) {
if(vis[e[i].to] || e[i].to == fa) continue;
if(cao(e[i].to,w,u,depth + ,op)) return true;
}
return false;
}
bool solve(int u){
dfs(u,);
if(sz[u] < len) return false;
int root = FindRoot(sz[u],u,);
vis[root] = true;
++cnt;
ULL w = sa[root];
if(w == Pre[len]) return true;
if(w == Pre[]) pre[] = cnt;
if(w == Suf[len]) suf[len] = cnt;
for(int i = head[root]; ~i; i = e[i].next){
if(vis[e[i].to]) continue;
if(cao(e[i].to,w,root,,true)) return true;
cao(e[i].to,w,root,,false);
}
for(int i = head[root]; ~i; i = e[i].next){
if(vis[e[i].to]) continue;
if(solve(e[i].to)) return true;
}
return false;
}
int main(){
int kase,n,u,v,cs = ;
for(int i = ; i < maxn; ++i) B[i] = B[i-]*Base;
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
memset(head,-,sizeof head);
memset(vis,false,sizeof vis);
memset(pre,,sizeof pre);
memset(suf,,sizeof suf);
tot = ;
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
scanf("%s%s",sa + ,sb + );
len = strlen(sb + );
for(int i = ; i <= len; ++i) Pre[i] = Pre[i-] + B[i-]*sb[i];
Suf[len + ] = ;
for(int i = len; i > ; --i) Suf[i] = Suf[i+] + B[len - i]*sb[i];
printf("Case #%d: %s\n",cs++,solve()?"Find":"Impossible");
}
return ;
}

HDU 5469 Antonidas的更多相关文章

  1. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  2. hdu 5469 Antonidas (dfs+剪枝)2015 ACM/ICPC Asia Regional Shanghai Online

    题意: 给出一棵树,再给出每个节点上的值(一个char字符)这些值以一个字符串s1表示,然后给出一个s2字符串,问在这棵树上是否存在两个点,从一个点走到另一个点所经过的路径上的char字符组成的字符串 ...

  3. HDU 5469 Antonidas (树形DP,暴力)

    题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...

  4. 【HDU5469】Antonidas(点分治,字符串哈希)

    [HDU5469]Antonidas(点分治,字符串哈希) 题面 HDU Vjudge 题解 啊哈?什么垃圾一眼点分治+Hash判断,哈哈哈哈哈,让我来码码码. 诶,怎么WA了.改改改改改. 诶,怎么 ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. pwa-serviceWorker与页面通信postMessage

    https://ppt.geekbang.org/list/gmtc2018?from=groupmessage&amp%3Bisappinstalled=0 http://www.sohu. ...

  2. Retrofit实现PUT网络请求,并修改Content-Type

    @FormUrlEncoded @PUT(Constant.BOSS_HX_CHANGE_PHONE_INTERVIEW) Call<ResponseHxResultBean> handl ...

  3. Codeforces Round #243 (Div. 1)

    ---恢复内容开始--- A 枚举l,r #include <iostream> #include<cstdio> #include<cstring> #inclu ...

  4. 在windows下编译出linux可执行程序

    set GOARCH=amd64 set GOOS=linux go build xx.go 会生成一个没有后缀的xx二进制文件 将该文件放入linux系统某个文件夹下 赋予权限 chmod 777 ...

  5. scala 通过jdbc访问mysql

    scala是jvm语言,运行在jvm之上 我们知道jdbc是java访问数据库的技术,那么scala能不能通过jdbc操作数据库呢,答案是可以的 部分代码如下: /** * 获取连接 */ priva ...

  6. MongoDB部署、使用、监控及调优

    MongoDB部署 系统环境:CentOS7   下载地址:http://mirrors.163.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD ...

  7. centos设置系统时间

    系统日期设定成1996年6月10日上午9点date -s 06/22/96date -s 09:00:00

  8. Tomcat配置Oracle数据源

    开发环境:Eclipse luna.tomcat 7.Oracle 配置Oracle datasource步骤 第一步:打开tomcat目录下的 context.xml 文件,添加 Resource ...

  9. 如何使用capedit分割数据包文件

    wireshark是一个网络数据包的分析工具,主要用来捕获网卡上的数据包并显示数据包的详细内容.在处理一些大的数据包文件时,如果直接用wireshark图形工具打开一些大文件的数据包会出现响应慢甚至没 ...

  10. java工作流activiti的步骤

    链接:activiti 表名称的解释 工作流从流程定义到创建一个流程实例完成执行步骤(省略bpmn的画法) 工作流的所有操作都是使用流程引擎来进行操作的,流程引擎只是存储流程的过程,而不存储具体的业务 ...