传送门

解题思路

  感觉这种题都是套路,首先缩点判了环(没看见自环挂了一次。。),然后设\(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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [SDOI2010] 所驼门王的宝藏 [建图+tarjan缩点+DAG dp]

    题面传送门: 传送门 思路: 看完题建模,容易得出是求单向图最长路径的问题 那么把这张图缩强联通分量,再在DAG上面DP即可 然而 这道题的建图实际上才是真正的考点 如果对于每一个点都直接连边到它所有 ...

  4. CF459E Pashmak and Graph (Dag dp)

    传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...

  5. D. Mysterious Present DAG dp

    https://codeforces.com/problemset/problem/4/D 这个题目比较简单,就是一个DAG模型,这个可以看看紫书学习一下, 我这次是用dp来写的,用记忆化搜索也许更好 ...

  6. 5. Longest Palindromic Substring (DP)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. CF919D Substring

    思路: 拓扑排序过程中dp.若图有环,返回-1. 实现: #include <bits/stdc++.h> using namespace std; ; vector<int> ...

  8. Codeforces 919D Substring (拓扑图DP)

    手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...

  9. poj3249 Test for job 【图的DAG dp】

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

随机推荐

  1. Eureka配置详解(转)

    Eureka客户端配置       1.RegistryFetchIntervalSeconds 从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒 2.InstanceInfoR ...

  2. centos做免密登录

    CentOS 6.9 SSH配置用户免密码登录 1. 演示环境: 192.168.1.144:CentOS 6.9 x86_64 192.168.1.146:CentOS 7.4 x86_64 2.  ...

  3. EasyUI - 简介

    1. EasyUI : 简单的界面设计框架, 基于jQuery的UI插件, 主要用来设计网站的后台管理系统 2. EasyUI使用 : 将EasyUI提供的js文件和主题(themes)样式存放到项目 ...

  4. 双目立体匹配经典算法之Semi-Global Matching(SGM)概述:视差计算、视差优化

    文章目录 视差计算 视差优化 剔除错误匹配 提高视差精度 抑制噪声 视差计算   在SGM算法中,视差计算采用赢家通吃(WTA)算法,每个像素选择最小聚合代价值所对应的视差值作为最终视差,视差计算的结 ...

  5. NX二次开发-UFUN获取直线的两个端点UF_CURVE_ask_line_data

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_ui.h> UF_initialize(); ...

  6. NX二次开发-UFUN获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)UF_OBJ_ask_display_properties

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  7. hdu多校第八场 1003 (hdu6659) Acesrc and Good Numbers 数论/打表

    题意: 对于某数k,若数字d在1-k中出现次数恰好为k,则称k为好数. 给定d,x,求x以内,对于d而言最大的好数.k范围1e18. 题解: 打表二分即可. 但是,1e18的表是没法打出来的,只能在o ...

  8. scrapy-redis + Bloom Filter分布式爬取tencent社招信息

    scrapy-redis + Bloom Filter分布式爬取tencent社招信息 什么是scrapy-redis 什么是 Bloom Filter 为什么需要使用scrapy-redis + B ...

  9. jeecg下实现自动默认模糊查询

    也许jeecg的作者深受SAP毒害吧,没考虑到广大使用JEECG的人群为SAP用户,及所开发的项目均为中小项目,无惧大数据模糊查询带来的功能影响. 经网友“&&康&&& ...

  10. (转)在Source Insight中看Python代码

    http://blog.csdn.net/lvming404/archive/2009/03/18/4000394.aspx SI是个很强大的代码查看修改工具,以前用来看C,C++都是相当happy的 ...