D. Substring

time limit: per test3 seconds

memory limit: per test256 megabytes

inputstandard: input

outputstandard: output

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

Input

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

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

Then m lines follow. Each line contains two integers x,y(1≤x,y≤n)x,y (1≤x,y≤n)x,y(1≤x,y≤n), describing a directed edge from xxx to yyy. Note that xxx can be equal to yyy and there can be multiple edges between xxx and yyy. 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 −1-1−1 instead.

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→51→3→4→51→3→4→5. The value is 333 because the letter ‘a’ appears 333 times.

题意

给出有nnn个点和mmm条边的有向图,每个节点上有一个小写字母,求所有通路中出现次数最多的字母出现的次数,如果出现了无数次,输出−1-1−1

Solve

用拓扑排序判断图中是否有环,如果有环,那么环上的字母出现的次数为无限多次,输出−1-1−1。

如果能够进行拓扑排序,对于每次遍历的节点,更新当前路上字母出现次数的最大值

dp[i][j]dp[i][j]dp[i][j]表示到达节点iii,字母′a′+j'a'+j′a′+j出现的最大次数

Code

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define INF 0x7f7f7f7f
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int n,m;
char ch[maxn];
int visi[maxn];
// dp[i][j]表示到i位置,字母j出现的最多次数
int dp[maxn][50];
int cnt;
vector<int>v[maxn];
queue<int>que;
void topo()
{
for(int i=1;i<=n;i++)
if(!visi[i])
{
que.push(i);
dp[i][ch[i]-'a']++;
}
while(!que.empty())
{
cnt++;
int res=que.front();
que.pop();
int sz=v[res].size();
for(int i=0;i<sz;i++)
{
visi[v[res][i]]--;
if(!visi[v[res][i]])
que.push(v[res][i]);
for(int j=0;j<26;j++)
dp[v[res][i]][j]=max(dp[v[res][i]][j],dp[res][j]+(ch[v[res][i]]-'a'==j));
}
}
if(cnt<n)
cout<<-1<<endl;
else
{
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<26;j++)
ans=max(ans,dp[i][j]);
}
cout<<ans<<endl;
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
cin>>(ch+1);
ms(visi,0);
int x,y;
for(int i=0;i<m;i++)
{
cin>>x>>y;
v[x].push_back(y);
visi[y]++;
}
topo();
return 0;
}

Codeforces 919D:Substring(拓扑排序+DP)的更多相关文章

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

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

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

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

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

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

  4. CodeForces 721C Journey(拓扑排序+DP)

    <题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...

  5. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  6. [NOIP2017]逛公园 最短路+拓扑排序+dp

    题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...

  7. 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp

    正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...

  8. 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 456  Solved: 215[Submit][Stat ...

  9. 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP

    [BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...

  10. bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

随机推荐

  1. flink---实时项目----day03---1.练习讲解(全局参数,数据以parquet格式写入hdfs中) 2 异步查询 3 BroadcastState

    1 练习讲解(此处自己没跑通,以后debug) 题目见flink---实时项目---day02 kafka中的数据,见day02的文档 GeoUtils package cn._51doit.flin ...

  2. JAXB—Java类与XML文件之间转换

    JAXB-Java类与XML文件之间转换 简介         JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生 ...

  3. 集合类——集合输出、栈和队列及Collections集合

    1.集合输出 在之前我们利用了toString()及get()方法对集合进行了输出,其实那都不是集合的标准输出,集合输出有四种方式:Iterator.ListIterator.Enumeration. ...

  4. git删除了本地文件,从远程仓库中恢复

    在本地删除了文件,使用git pull,无法从远程项目中拉取下来 具体操作 查看项目的状态,会显示出你删除的数据 git status 进入被删除的文件的目录下,假设删除的文件名为 test.txt ...

  5. Virtual Destructor

    Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...

  6. static JAVA

    static 关键字:使用static修饰的变量是类变量,属于该类本身,没有使用static修饰符的成员变量是实例变量,属于该类的实例.由于同一个JVM内只对应一个Class对象,因此同一个JVM内的 ...

  7. Java设计模式—Proxy动态代理模式

    代理:设计模式 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理. 图 1. 代 ...

  8. lambda表达式快速创建

    Java 8十个lambda表达式案例 1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable( ...

  9. Spring Boot事务支持

    一.创建项目 二.添加依赖 <dependencies> <dependency> <groupId>org.projectlombok</groupId&g ...

  10. Appium获取toast消息遇到的问题(一)

    一.运行错误 Android获取toast,需要在参数里设置automationName:Uiautomator2 1 # 设置设备的信息 2 desired_caps = { 3 'platform ...