D. Substring

time limit per test3 seconds

memory limit per test256 megabytes

Problem Description

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


解题心得:

  1. 比赛的时候读错了题写到崩溃啊。其实题意是每个点用一个字母表示,一个人随机从一个点开始走,获得的值是他走过的路径中遇到的字母(次数最多的那个)的次数。问这个人在途中可能获得的最大值是多少。如果有环输出-1。
  2. 写得贼复杂,tarjan判断环,map去除重边,记忆化搜索得到答案。不想说话去角落默默呆着。

我的智障代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+100;
vector <int> ve[maxn];
map <pair<int,int>,int> maps;
char s[maxn];
bool find_cir,in[maxn];
int n,m,Max,dfn[maxn],low[maxn],dp[maxn][26];
stack <int> st; void init(){
Max = -1;
scanf("%s",s+1);
for(int i=0;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
if(a == b)//自身到自身形成环
find_cir = true;
if(maps[make_pair(a,b)] == 233)//去除重边
continue;
maps[make_pair(a,b)] = 233;
ve[a].push_back(b);
}
} int tot = 0;
void tarjan(int x){
dfn[x] = low[x] = ++tot;
st.push(x);
in[x] = true;
for(int i=0;i<ve[x].size();i++){
int v = ve[x][i];
if(!dfn[v]){
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(in[v])
low[x] = min(low[x],dfn[v]);
}
int num = 0;
if(low[x] == dfn[x]){
while(1){
int now = st.top();
st.pop();
in[now] = false;
num++;
if(now == x)
break ;
}
if(num > 1){
find_cir = true;
return ;
}
}
} void judge_cir(){//用tarjan判断有没有环
for(int i=1;i<=n;i++)
if(!dfn[i]){
tarjan(i);
}
} int dfs(int u,int c){
if(dp[u][c] != -1)
return dp[u][c];
int res = 0;
for(int i=0;i<ve[u].size();i++){
int v = ve[u][i];
res = max(res,dfs(v,c));
}
res += (s[u]-'a' == c);
return dp[u][c] = res;
} void get_Max(){
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=0;j<26;j++)
Max = max(Max,dfs(i,j));
printf("%d",Max);
return;
} int main(){
scanf("%d%d",&n,&m);
init();
judge_cir();
if(find_cir){
printf("-1");
return 0;
}
get_Max();
}

轻松过:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+100;
vector <int> ve[maxn];
int Max = -1,n,m,dp[maxn][30];
char s[maxn]; void init(){
memset(dp,-1,sizeof(dp));
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=0;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
}
} int dfs(int u,int c){
int res = 0;
if(dp[u][c] == -2){//在之前已经走过这条边,形成环
printf("-1");
exit(0);
}
if(dp[u][c] != -1)
return dp[u][c];
dp[u][c] = -2;
for(int i=0;i<ve[u].size();i++){
int v = ve[u][i];
res = max(res,dfs(v,c));
}
res += (s[u]-'a' == c);
return dp[u][c] = res;
} void get_Max(){
for(int i=1;i<=n;i++)
for(int j=0;j<26;j++){
Max = max(Max,dfs(i,j));
}
printf("%d",Max); } int main(){
init();
get_Max();
}

Codeforces Round #460 (Div. 2)-D. Substring的更多相关文章

  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. 【Codeforces Round #460 (Div. 2) D】Substring

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...

  3. Codeforces Round #460 (Div. 2) ABCDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...

  4. Codeforces Round #460 (Div. 2)

    A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...

  5. [Codeforces]Codeforces Round #460 (Div. 2)

    Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...

  6. Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)

    题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...

  7. Codeforces Round #460 (Div. 2) 前三题

    Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...

  8. Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理

    E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  9. Codeforces Round #460 (Div. 2)-C. Seat Arrangements

    C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...

随机推荐

  1. JS展示预览PDF。

    刚好遇到需求,需要在手机端--展示一个电子收据,电子收据返回是PDF格式的,所以需要在前端上面去做PDF预览. 在学习过程中,了解到一种很简单,不需要任何插件的方法做PDF预览,但是这方法有局限性. ...

  2. 初识MAC(由window到mac的转变适应)

    * Windows上的软件可以拿到Mac上面安装吗? Windows上面的软件不能拿到Mac的操作系统上安装,除此之外,Windows里的 exe文件,在Mac下面也是无法运行的,要特別注意.如果你要 ...

  3. Java虚拟机,类文件结构深度解析

    Java类文件结构 Java虚拟机不和包括Java在内的任何语言绑定,只与 "Class文件" 这种特定的二进制文件所关联, Class文件中包含了Java虚拟机指令集合符号表以及 ...

  4. SpringMVC 返回实体对象时屏蔽某些属性

    SpringMVC 可以直接已JSON的结果返回实体对象,可是返回时是所有属性与属性值都会一并返回, 怎样才能屏蔽某些属性?方法很简单,只要在实体对象类中要屏蔽的属性值上加 @JsonIgnore 注 ...

  5. Struts2初级篇(HelloWorld)

    Struts2的工作流程: 从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的: 操作(Actions) ...

  6. 【干货】Html与CSS入门学习笔记1-3

    从23号开始用了4天时间看完了<Head First Html与CSS>这本书,本书讲解方式深入浅出,便于理解,结合习题,便于记忆,是一本不错的入门书.下面是本书的学习笔记: 一.认识HT ...

  7. HDU 4745 Two Rabbits (区间DP)

    题意: 两只兔子,在一个由n块石头围成的环上跳跃,每块石头有一个权值ai.开始时两兔站在同一石头上(也算跳1次),一只从左往右跳,一只从右往左跳,两只同时跳,而每跳一次,两只兔子所站的石头的权值都要相 ...

  8. 查看SAP CRM和C4C的UI technical信息

    CRM 比如我们想看Quantity这个字段到底是绑在哪个模型上,选中该字段按F2: 就能知道是绑在Context node BTADMINI的QUANTITY字段上. C4C 同理,使用debugM ...

  9. Android中的Matrix(矩阵)

    写在前面 看这篇笔记之前先看一下参考文章,这篇笔记没有系统的讲述矩阵和代码的东西,参考文章写的也有错误的地方,要辨证的看. 如何计算矩阵乘法 android matrix 最全方法详解与进阶(完整篇) ...

  10. URL跨项目调用方法,获取返回的json值,并解析

    package com.mshc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...