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> ...
随机推荐
- 使用lombok时@Setter @Getter无效
原文链接 : https://blog.csdn.net/marion158/article/details/87893480 lombok是一个帮助简化代码的工具,通过注解的形式例如@Setter ...
- Java oop 第13章_多线程
第13章_多线程 一. 多线程相关的概念: 程序:由某种编程语言开发可执行某些功能的代码组合,它是静态的概念. 进程:当程序被执行时的过程可以理解为讲程序从外存调入内存的过程,会为每一个程序 ...
- unicode_start - 将控制台设为Unicode模式.
总览 unicode_start [ font [ screen-font-map ] ] 描述 unicode_start 命令将显示屏及键盘设为 Unicode 模式, 并且有可能还会装载所用的 ...
- 关于windows cmd的一些便捷应用
在同事的指点下,我学会了一种非常方便的进入路径的方法 在windows文件夹中直接打开到要执行的文件的位置,然后在我的电脑那个路径当中输入cmd 之后,cmd的对话框会弹出来,并且显示在当前路径下,这 ...
- map方法的简单使用
假设有一个数组a,将a中的数值以2倍的形式放到b数组中 <!DOCTYPE html> <html lang="en"> <head> < ...
- docker一键部署zookeeper
version: '3.1' services: zoo1: image: zookeeper:3.4.11 restart: always hostname: zoo1 container_name ...
- JRebel安装部署,激活
1.安装部署 2.激活 去官网获得激活码,首先进行注册,之后获得激活码 官网:https://jrebel.com/software/jrebel/trial/getkey/ 查看是否激活
- 为什么要使用动态链接库(DLL)
为什么要使用动态链接库(DLL) 第一章 为什么要使用动态链接库(DLL) top 提起DLL您一定不会陌生,在Windows中有着大量的以DLL为后缀的文件,它们是保证Windows正常运行和维 ...
- delphi 单元 MSHTML 之Ihtmldocument2
delphi : Ihtmldocument2接口的利用 MSHTML是微软公司的一个COM组件,该组件封装了HTML语言中的所有元素及其属性,穿越其供给的规范接口,能够访问指定网页的所有元素. MS ...
- bat命令自用其(一)
每秒钟打印ping命令结果到指定文件: @echo off set /p ip=Input the IP required to monitor: :starts echo %date% %time% ...