HDU 5469 Antonidas
Antonidas
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
#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的更多相关文章
- hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)
题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...
- hdu 5469 Antonidas (dfs+剪枝)2015 ACM/ICPC Asia Regional Shanghai Online
题意: 给出一棵树,再给出每个节点上的值(一个char字符)这些值以一个字符串s1表示,然后给出一个s2字符串,问在这棵树上是否存在两个点,从一个点走到另一个点所经过的路径上的char字符组成的字符串 ...
- HDU 5469 Antonidas (树形DP,暴力)
题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...
- 【HDU5469】Antonidas(点分治,字符串哈希)
[HDU5469]Antonidas(点分治,字符串哈希) 题面 HDU Vjudge 题解 啊哈?什么垃圾一眼点分治+Hash判断,哈哈哈哈哈,让我来码码码. 诶,怎么WA了.改改改改改. 诶,怎么 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- Excel 宏练习
任务描述: 利用 Excel 绘制函数图像 f(x)=x^2/3+0.9*(3.3-x^2)^1/2*sin(a*x),并通过按钮事件来刷新图像. 问题分析: 可以参考类似 Matlab 绘图的方式, ...
- RHEL6.5----LVS(NAT)
主机名 IP 所需软件 master 192.168.30.130(Nat) 192.168.17.130(VMnet4) ipvsadm node-1 192.168.17.131 http ...
- SpringBoot 2.x (8):模板引擎
SpringBoot中有很多的starter:本质是多个JAR包集合 比如我们常用的: <dependency> <groupId>org.springframework.bo ...
- JSP报错The value for the useBean class attribute *** is invalid.
环境:IDEA+Tomcat9+JDK1.8 在前期学习时,环境一直能够"正常"使用,实际上环境并没有完全搭建成功. 推荐: https://blog.csdn.net/lw_po ...
- http与WebSocket
利用websocket连接服务器的最大特点就是:持久链接的特点. 共同点是:都是基于TCP协议进行client-server的链接,websocket是HTML5提出的一套补缺HTTP链接中不能持久链 ...
- Pow挖矿流程
Pow挖矿流程 POW即工作量的证明,主要特征是客户端需要做一定难度的工作得出一个结果,验证方却很容易通过结果来检查出客户端是不是做了相应的工作. Pow挖矿即不断接入新的Block延续Block C ...
- Android(java)学习笔记159:多线程断点下载的原理(Android实现)
之前在Android(java)学习笔记215中,我们从JavaSE的角度去实现了多线程断点下载,下面从Android角度实现这个断点下载: 1. 新建一个Android工程: (1)其中我们先实现布 ...
- [转] 学习,思维三部曲:WHAT、HOW、WHY(通过现象看本质)
https://www.douban.com/note/284947308/?type=like 学习技术的三部曲:WHAT HOW WHY 我把学习归类为三个步骤:What.How.Why.经过我对 ...
- Unity3D 在自定义脚本中实现Button组件上的OnClick面板
下述内容不对c#语法做过多讲解,仅对已入门并有兴趣的同学做为学习和拓展的资料 大家在Unity制作的过程中一定都使用过UI功能,那么很多人也一定见过这个面板: 那么我们如何能在自己的脚本中添加上像On ...
- Js 之获取QueryString的几种方法
一.正则匹配 function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(& ...