题目描述

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; }

  

Codeforces 919 D Substring的更多相关文章

  1. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

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

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

  3. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  4. CF 919 D. Substring

    D. Substring 链接 题意: 在一张有向图中,定义路径的权值为路径中出现次数最多的字符出现的次数,求一条权值最大的路径.如果权值可以无限大,输出-1. 分析: 注意是一张有向图.如果存在环那 ...

  5. Codeforces 873B - Balanced Substring(思维)

    题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...

  6. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  7. Codeforces 919 E Congruence Equation

    题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1&l ...

  8. Codeforces 919 C. Seat Arrangements

    C. Seat Arrangements   time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces 919 B. Perfect Number

      B. Perfect Number   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. easyui-combogrid必填为空时无法通过表单验证的问题

    在使用easyui-combogrid时,由于html解析出的格式是如下三层: <td> <input id="txcombo" class="easy ...

  2. Understanding on 'Error to Origin (50x)' , 'Internal CDN Error (50x)' and 'External Error (50x)' in Chartron

    Overview This document explains about definition of these values on OUI Chartron. Definition of Erro ...

  3. flex遭遇text-overflow:hidden,white-space:nowrap

    最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...

  4. 图说不为人知的IT传奇故事-4-王安用一生来跟IBM抗衡

    此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...

  5. 踩坑 PHP Fatal Error Failed opening required File

    使用 require 引用文件时,报错如下: require 'https://dev.ryan.com/test.php'; [Sat Mar 19 23:10:50 2011] [warn] mo ...

  6. [oldboy-django][2深入django]django模板使用函数

    1 模板引入子html--include 模板引擎 - 母版 - include,导入公共的html a. 用法:{% include "pub.html" %}, pub.htm ...

  7. html基础标签之head和body标签

    什么是标签标签是由一对尖括号包裹的单词构成的,也有一些单闭和标签,仅仅就自己出现就可以了,例如meta,link 1. 这里介绍了几种html语言里常见的head标签 <!DOCTYPE htm ...

  8. 截图神器-snipaste

    基础操作 Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 截屏 开始截图 快捷键(默认为 F1) 鼠标左键 单击托盘图标 何谓一次 成功的截图 保存到剪贴板 (  /  ...

  9. Zabbix整合MegaCLI实现物理硬盘的自动发现和监控

    MegaCLI是LSI提供的用户空间管理RAID卡(LSI芯片)工具,适用于大多数的Dell服务器. MegaCLI介绍: http://zh.community.dell.com/techcente ...

  10. 平滑升级nginx

    平滑升级nginx版本技术文档 作者 联系方式 日期 版本号 马坤 852115346@qq.com 2017-12-31 V1.0.0 备注:作者水平有限,难免出现错误.如若发现错误,请您及时与作者 ...