Codeforces Gym 100962 J. Jimi Hendrix
来源:互联网 发布:手机计步器软件排行 编辑:程序博客网 时间:2024/05/01 01:30
Description
You are given a tree T consisting of n vertices and
with a lowercase English letter
You are given a string s consisting of lowercase English letters. Your task is to nd a simple path
in the tree such that the string formed by concatenation of letters associated with edges of this
path contains string s as a subsequence, or determine that there exists no such simple path.
Input
The rst line of input contains two positive integers n and m (
the number of vertices in the tree and the length of the string s.
The following
letter), denoting an edge
The last line contains a string s (
Output
If the desired path exists, output its endpoints a and b. Otherwise, output -1 -1
. If there are
several possible answers, you are allowed to output any of them.
题意
给定 G = (V, E) , |E| = |V|-1 的树。树上直接相连两点间的边上记录一个字符 a b
。否则输出 -1 -1
。
分析
貌似已经搞不清此题的算法,感觉算是树分治的问题,但又有最优化问题的思想。
此题的关键在于维护以某点为根的子树的最优状态。
dp[i].lenl
表示以点 i 为根的子树最长已经匹配了 S 串的串首 lenl 个元素,同时匹配这 lenl 个元素的起始点为 idxl 。
dp[i].lenr
表示以点 i 为根的子树最长已经匹配了 S 串的串尾 lenr 个元素,同时匹配这 lenr 个元素的结束点为 idxr 。
任意取一个点作为树的根,跑一遍 dfs 。若在某点最终 dp[i].lenl + dp[i].lenr >= m 且取到这么多元素不止点 i 的同一子树内,则有解。
代码
#include<bits/stdc++.h>using namespace std;const int maxn = 5e5 + 10;int n, m;char ch, s[maxn];vector< pair<int, char> > g[maxn];struct Node{ int lenl, lenr, idxl, idxr;}dp[maxn];bool dfs(int rt, int fa){ for(int i=0, to;i<g[rt].size();i++) { to = g[rt][i].first; if(to == fa) continue; if(dfs(to, rt)) return true; int tmpl = s[ dp[to].lenl + 1 ] == g[rt][i].second ? dp[to].lenl+1 : dp[to].lenl; int tmpr = s[ m - dp[to].lenr ] == g[rt][i].second ? dp[to].lenr+1 : dp[to].lenr; if(tmpl + dp[rt].lenr >= m) { printf("%d %d",dp[to].idxl, dp[rt].idxr); return true; } if(tmpr + dp[rt].lenl >= m) { printf("%d %d",dp[rt].idxl, dp[to].idxr); return true; } if(tmpl > dp[rt].lenl) { dp[rt].lenl = tmpl; dp[rt].idxl = dp[to].idxl; } if(tmpr > dp[rt].lenr) { dp[rt].lenr = tmpr; dp[rt].idxr = dp[to].idxr; } } return false;}int main(){ scanf("%d %d",&n,&m); for(int i=1, u, v;i<n;i++) { scanf("%d %d %c",&u,&v,&ch); dp[i].lenl = dp[i].lenr = 0; dp[i].idxl = dp[i].idxr = i; g[u].push_back(make_pair(v, ch)); g[v].push_back(make_pair(u, ch)); } dp[n].lenl = dp[n].lenr = 0, dp[n].idxl = dp[n].idxr = n; scanf(" %s",s+1); if(!dfs(1, -1)) printf("-1 -1");}
- Codeforces Gym 100962 J. Jimi Hendrix
- Gym 100962J Jimi Hendrix (DFS + 树形dp)
- codeforces-gym-100187-J【dfs】
- Codeforces Gym 101246J Buoys
- Codeforces Gym 100753J Souvenirs
- Codeforces Gym 100342J Triatrip Bitset+枚举
- Codeforces Gym 100814J Game 简单博弈
- Codeforces Gym 100800J Jelly Raid (暴搜)
- CodeForces Gym 100989J Objects Panel (A)
- 【模拟】NEERC15 J Jump (Codeforces GYM 100851)
- Codeforces gym 101343 J 状压dp
- Codeforces Gym 101142 J. Java2016 (构造)
- Codeforces Gym 101173 J. Jazz Journey
- Codeforces GYM 100753J: Souvenirs 题解
- codeforces Gym 100187J J. Deck Shuffling dfs
- 【数位DP】Codeforces Gym 100418J Lucky tickets
- CodeForces Gym 100971J 感觉题意有问题
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
- Codeforces Round #397 E. Tree Fold(bfs,想法题,好题)
- 排队接水
- 六角填数(DFS)
- Easy单例模式
- 欢迎使用CSDN-markdown编辑器
- Codeforces Gym 100962 J. Jimi Hendrix
- 游戏开发时关于UGUI的一些控件无法使用的问题
- 八皇后
- Spark Streaming使用Kafka保证数据零丢失
- 死锁的产生
- 剑指offer经典编程(十三)
- 两种常见的雷击浪涌和雷击浪涌防护电路设计概要
- 喇叭音量调节
- super的理解