D. Substring
time limit per test

3 seconds

memory limit per test

256 megabytes

You are given a graph with n nodes and m 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 3. Your task is find a path whose value is the largest.

Input

The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges.

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

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

Output

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

Examples
input
5 4
abaca
1 2
1 3
3 4
4 5
output
3
input
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
output
-1
input
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
output
4
Note

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

题意:n,m分别代表顶点个数和边的条数,输入一串字符串,输入边的信息,一条路径的价值是出现最多的那个字母的次数,让你求最大价值!例如案例1:最大路径为1->3->4->5,价值为a出现的次数,3次,所以输出3;

分析:拓扑排序+思维;记录好每到达一个顶点所有字母出现的次数,开不了300000*300000的数组就用vector,如果遍历的点的个数不等于n的话输出-1,否则二重循环查询到最大的价值!

#include<bits/stdc++.h>
#define N 300010
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int indegree[N];//记录每个节点的入度
int dp[N][]; //记录到达某个节点的时候26个字母所出现的次数
queue<int>w;
vector<int>v[N];
int n,m;
char s[N];
int ans;//记录遍历的节点数
int main()
{
cin>>n>>m;
cin>>s;
mem(indegree,);
int x,y;
for (int i=;i<=m;i++)
{
cin>>x>>y;
v[x].push_back(y);
indegree[y]++;
}
for (int i=;i<=n;i++)
if (!indegree[i])
w.push(i),dp[i][s[i-]-'a']++;
int q,p;
while (!w.empty())
{
q=w.front();
w.pop();
for (int i=;i<v[q].size();i++)
{
p=v[q][i];
indegree[p]--;
for (int j=;j<;j++)
{
if (j==s[p-]-'a') dp[p][j]=max(dp[p][j],dp[q][j]+);
else dp[p][j]=max(dp[p][j],dp[q][j]);
}
if(!indegree[p]) w.push(p);
}
ans++;
}
if (ans!=n)
cout << "-1" << endl;
else
{
int maxn=;
for (int i=;i<=n;i++)
for (int j=;j<;j++)
maxn=max(maxn,dp[i][j]);
cout << maxn << endl;
}
return ;
}

Substring(Codeforces-D-拓扑排序)的更多相关文章

  1. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  2. CodeForces - 721C 拓扑排序+dp

    题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c     //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...

  3. Codeforces 1100E 拓扑排序

    题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347 如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向 ...

  4. National Property CodeForces - 875C (拓扑排序)

    大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...

  5. Codeforces 1159E 拓扑排序

    题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...

  6. Codeforces 919D Substring (拓扑排序+树形dp)

    题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...

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

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

  8. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

  9. CodeForces - 919D Substring (拓扑排序+dp)

    题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...

  10. Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )

    题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是  a ...

随机推荐

  1. vim,neovim 配置文件

    插件管理用的是 https://github.com/junegunn/vim-plug 打开网址,会有示例,如何安装它本身,以及一些插件,照做,很容易完成 里面有两个可用的配置文件,cp_vimrc ...

  2. idea中maven项目依赖jar一直标红线

    网上maven仓库中无法下载某些jar包,这时候就需要手动下载,并导入maven, 导入命令demo: mvn install:install-file -DgroupId=javax.media - ...

  3. python基础,if判断

    一.计算机基础知识: 1.计算机基本组成:主板+CPU+内存 (CPU:主频,核数(16)   内存:大小,型号,主频   显卡:显存,位宽) 2.计算机最低层:电子电路,只能识别0和1. 二.pyt ...

  4. ant design vue 时间选择器只能到最大日期

    <a-date-picker :disabledDate="disabledEndDate"  style="width: 100%" placehold ...

  5. ant design for vue 解决 vue.esm.js?c5de:628 [Vue warn]: Invalid prop: custom validator check failed for prop "defaultValue". 的错误

    错误重现: 在使用ant design for vue 的选择器插件的时候, 设置默认为为id(为数字) 报错: 解决办法: id为数字, 而defaultValue 的key 值必须为字符串, 将i ...

  6. 将List 分成n个长度由调用者指定的子List

    public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8); ...

  7. 学习spring第三天

    Spring第三天笔记 今日内容 Spring的核心之一 -  AOP思想 (1) 代理模式- 动态代理 ① JDK的动态代理 (Java官方) ② CGLIB 第三方代理 (2) AOP思想在Spr ...

  8. tc/traffic control 网络控制工具

    第一个例子 增加延时 tc qdsic add dev enp0s3 root netem delay 200ms qdisc : queuing discipline, 当内核需要发送包到某个接口时 ...

  9. 903C. Boxes Packing#俄罗斯套娃问题(map使用)

    题目出处:http://codeforces.com/problemset/problem/903/C 题目大意:求这组数据中数据出现的最大重复次数 #include<iostream> ...

  10. D. Fish eating fruit

    题:https://nanti.jisuanke.com/t/41403 题意:求任意俩点之间距离之和模3后的三个结果的总数(原距离之和) 第一种做法: 树形dp #include<bits/s ...