CF919D Substring (dag dp)
解题思路
感觉这种题都是套路,首先缩点判了环(没看见自环挂了一次。。),然后设\(f[x][i]\)表示到了\(x\),\(i\)这个字母走过的最长距离,然后拓扑排序更新即可。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
const int MAXN = 300005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
int n,m,head[MAXN],cnt,dfn[MAXN],low[MAXN],num,w[MAXN],ans;
int to[MAXN],nxt[MAXN],f[MAXN][30],stk[MAXN],top,deg[MAXN];
bool vis[MAXN],flag;
char s[MAXN];
queue<int> Q;
inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
}
void tarjan(int x){
dfn[x]=low[x]=++num;stk[++top]=x;vis[x]=1;
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];
if(!dfn[u]) {tarjan(u);low[x]=min(low[x],low[u]);}
else if(vis[u]) low[x]=min(low[x],dfn[u]);
}
if(low[x]==dfn[x]) {
if(stk[top]!=x) {flag=1;return;}
top--;vis[x]=0;
}
}
int main(){
n=rd(),m=rd();int x,y;
scanf("%s",s+1);
for(int i=1;i<=n;i++) w[i]=s[i]-'a'+1;
for(int i=1;i<=m;i++){
x=rd(),y=rd();deg[y]++;
if(x==y) flag=1;
add(x,y);
}
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
if(flag) {puts("-1");return 0;}
else{
for(int i=1;i<=n;i++) if(!deg[i]) Q.push(i),f[i][w[i]]=1;
while(Q.size()){
int x=Q.front();Q.pop();
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];
f[u][w[u]]=max(f[u][w[u]],f[x][w[u]]+1);
for(int k=1;k<=26;k++) if(w[u]!=k) f[u][k]=max(f[u][k],f[x][k]);
deg[u]--;if(!deg[u]) Q.push(u);
}
if(!head[x]) for(int i=1;i<=26;i++) ans=max(ans,f[x][i]);
}
}
printf("%d\n",ans);
return 0;
}
CF919D Substring (dag dp)的更多相关文章
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- 5.Longest Palindromic Substring (String; DP, KMP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [SDOI2010] 所驼门王的宝藏 [建图+tarjan缩点+DAG dp]
题面传送门: 传送门 思路: 看完题建模,容易得出是求单向图最长路径的问题 那么把这张图缩强联通分量,再在DAG上面DP即可 然而 这道题的建图实际上才是真正的考点 如果对于每一个点都直接连边到它所有 ...
- CF459E Pashmak and Graph (Dag dp)
传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...
- D. Mysterious Present DAG dp
https://codeforces.com/problemset/problem/4/D 这个题目比较简单,就是一个DAG模型,这个可以看看紫书学习一下, 我这次是用dp来写的,用记忆化搜索也许更好 ...
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- CF919D Substring
思路: 拓扑排序过程中dp.若图有环,返回-1. 实现: #include <bits/stdc++.h> using namespace std; ; vector<int> ...
- Codeforces 919D Substring (拓扑图DP)
手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...
- poj3249 Test for job 【图的DAG dp】
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...
随机推荐
- 解决MSF更新证书错误
如下图所示提示签名无效下载失败,导致更新不了msf 解决办法如下: echo 'deb http://apt.metasploit.com/ lucid main' > /etc/apt/sou ...
- Laravel 项目运行 phpunit 测试结果只显示点号
在laravel 项目的根目录下,运行 phpunit 只显示 点号的情况 我尝试将 tests/Unit 和 tests/Feature 目录将 ExampleTest.php 文件删除,然后再运行 ...
- vue+elementui 中 @keyup 键盘上下左右移动聚焦
<template> <el-table :data="CreditUnclearOutlineList" border style="width: 1 ...
- MongoDB启动时遇到的问题
问题一:child process failed, exited with error number 100 原因分析:100是锁了 解决办法:#cd /usr/local/mongodb/data ...
- MapReduce计算原理及步骤
步骤:input从HDFS读取内容, split()切割分片内容,key/value, map()方法对输入的key/value进行计算处理,先写到内存,在内存中进行分区.排序,之后将Key/valu ...
- Unity 调用android
{ https://www.bilibili.com/video/av49002527 }
- jQuery的两把利器
1 jQuery核心函数 * 简称: jQuery函数($/jQuery) * jQuery库向外直接暴露的就是$/jQuery * 引入jQuery库后, 直接使用$即可 * 当函数用: $(xxx ...
- noip 2014 总结
2014 年的noip 已经结束了,成绩从个人而言不是特别的理想,从今年题的逗的程度,本来是个xxx 就被玩脱了= = 当然现在后悔事没有用的了,不过第二天全屏技术的在最后一小时看到了两道题的错误,然 ...
- NX二次开发-常用lib库文件
在项目属性->配置属性->链接器->输入->附加依赖项: libufun.lib UFUNC API 函数库 libugopenint.lib UFUNC 对话框 API 函数 ...
- Spring-Security (学习记录四)--配置权限过滤器,采用数据库方式获取权限
目录 1. 需要在spring-security.xml中配置验证过滤器,来取代spring-security.xml的默认过滤器 2. 配置securityMetadataSource,可以通过ur ...