题目描述

You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 33 . Your task is find a path whose value is the largest.

输入输出格式

输入格式:

The first line contains two positive integers n,mn,m ( 1<=n,m<=3000001<=n,m<=300000 ), denoting that the graph has nn nodes and mmdirected edges.

The second line contains a string ss with only lowercase English letters. The ii -th character is the letter assigned to the ii -th node.

Then mm lines follow. Each line contains two integers x,yx,y ( 1<=x,y<=n1<=x,y<=n ), describing a directed edge from xx to yy . Note that xx can be equal to yy and there can be multiple edges between xx and yy . Also the graph can be not connected.

输出格式:

Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.

输入输出样例

输入样例#1:

5 4
abaca
1 2
1 3
3 4
4 5

输出样例#1:

3

输入样例#2:

6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4

输出样例#2:

-1

输入样例#3:

10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7

输出样例#3:

4

说明

In the first sample, the path with largest value is 1→3→4→51→3→4→5 . The value is 33 because the letter 'a' appears 33 times.

XJB DP就行了,之前判一下是不是DAG

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
#define maxn 300005
using namespace std;
int ans=0,g[maxn];
int n,m,id[maxn],u,v;
int f[maxn],hd[maxn];
int to[maxn],ne[maxn];
bool vis[maxn];
char s[maxn];inline int num(char x){return x-'a';
}inline bool topsort(){queue<int> q;int x,tot=0;for(int i=1;i<=n;i++) if(!id[i]) q.push(i);while(!q.empty()){x=q.front(),q.pop(),tot++;for(int i=hd[x];i;i=ne[i]) if(!(--id[to[i]]))q.push(to[i]);}return tot==n;
}int dp(int x){if(vis[x]) return g[x];vis[x]=1,g[x]=0;for(int i=hd[x];i;i=ne[i]) g[x]=max(g[x],dp(to[i]));g[x]+=f[x];return g[x];
}int main(){scanf("%d%d",&n,&m);scanf("%s",s+1);for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);to[i]=v,ne[i]=hd[u];hd[u]=i,id[v]++;}if(!topsort()){puts("-1");return 0;}for(int i=0;i<26;i++){memset(vis,0,sizeof(vis));for(int j=1;j<=n;j++) f[j]=(num(s[j])==i);for(int j=1;j<=n;j++) ans=max(ans,dp(j));}printf("%d\n",ans);return 0;}

  

转载于:https://www.cnblogs.com/JYYHH/p/8474694.html

Codeforces 919 D Substring相关推荐

  1. Codeforces 1015F Bracket Substring AC自动机 + dp

    Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...

  2. Codeforces 1196D RGB Substring

    (easy version) https://codeforces.com/contest/1196/problem/D1 (hard version) https://codeforces.com/ ...

  3. 做题记录 To 2019.2.13

    2019-01-18 4543: [POI2014]Hotel加强版:长链剖分+树形dp. 3653: 谈笑风生:dfs序+主席树. POJ 3678 Katu Puzzle:2-sat问题,给n个变 ...

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

    Codeforces 919D Substring (拓扑图DP) 手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo ...

  5. Codeforces Round #590 (Div. 3) F. Yet Another Substring Reverse 子集dp

    传送门 文章目录 题意: 思路: 题意: 思路: 之前做过类似的题,翻转一个字串相当于将任意两个不相交的串连在一起.再一看字符集≤20\le20≤20,那就是铁子集dpdpdp了. 定义f[i]f[i ...

  6. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  7. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 &quo ...

  8. Codeforces 919D - Substring

    919D - Substring 思路: 拓扑排序判环+DAG上dp+记忆化搜索 状态:dp[i][j]表示以i为起点的路径中j的最大出现次数 初始状态:dp[i][j]=1(i have no so ...

  9. codeforces Balanced Substring

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

最新文章

  1. DEDECMS站点内容自动更新到新浪微博的方法
  2. [网络应用]winrar主题之Vista Ultimate篇
  3. c语言编程GetTickCount,c语言计算时间方法---clock-GetTickCount-QueryPerformanceCounter
  4. python北京房价预测_python数据分析——北京房价分析
  5. 昵图网源码php,【PHP】仿我图,千图,昵图网素材下载,素材销售平台最新宽屏商业版:含新版vip会员中心,全开源...
  6. wx小程序 解决子组件样式不起效
  7. PaddlePaddle证件照换底换大小工具
  8. 谷歌搜索引擎总是被修改
  9. 安卓系统手机如何用作电脑摄像头使用及解决方法
  10. 东方财富股票接口解析优缺点
  11. 使用git时报错schannel: next InitializeSecurityContext failed
  12. (数字通信)星座图 平均功率 峰值功率 峰均比 练习题;16-QAM 星座图 格雷编码
  13. Simulink自动代码生成2——生成嵌入式代码(generating embedded code)
  14. Linux(5):正则表达式 权限
  15. 燕山大学计算机辅助设计三级项目报告,计算机辅助设计三级项目报吿.doc
  16. 【转】只能在同一IE窗口打开网页的基本解决方法
  17. net net net
  18. 天云数据五度蝉联Fintech50强 Hubble实时流计算完胜Flink
  19. jquery 移除onclick事件
  20. 橄榄山350-423的插件,部分实现

热门文章

  1. HTTPS 证书配置
  2. 如何将django部署从顶级目录迁移到子目录下(NGINX UWSGI DJANGO)
  3. KeeSoft.DBHelper.DBFactory 数据库访问类
  4. Spring MVC 教程,快速入门,深入分析——多视图控制器
  5. 【vSphere系列二】安装 Openfiler 存储
  6. 轻松实现远程批量拷贝文件脚本(女学生作品)
  7. (How to)Windows Live Writer插入Latex公式
  8. 剑指Offer(Java实现)把字符串转换成整数
  9. 鸿蒙荣耀x10max,荣耀X10 Max有秘密武器:5G大屏手机能比4G还省电
  10. 组态王图素制作_组态王的图形及动画功能