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. Unity Download Assistant Error: 'SendRequest Error' while downloading ini file from http://files.unity3d.com/bootstrapper/29055738eb78/unity-5.3.6f1-win.ini

    Unity 官网的哥们如此说道 I open the exe on Compatibility Mode , it's solved. You can try. :) 翻译就是 我用兼容模式打开,就能 ...

  2. 《C#高效编程》读书笔记06-理解几个等同性判断之间的关系

    当创建自定义类型时(无论是class还是struct),应为类型定义"等同性"的含义.C#提供了4种不同的函数来判断两个对象是否"相等": public sta ...

  3. hibernate课程 初探单表映射1-4 hibernate开发前准备

    开发前准备: 1 eclipse 2 hibernate tools的安装(需要相关的jar包)(可以简化orm框架) hibernate tools的安装步骤: 1 到官网下载 https://so ...

  4. 面向对象(OOP)一

    一.面向对象理论 1)面向对象概念 面向对象编程(object Oriented Programming,OOP),是一种计算机编程构架,OOP达到软件工程的三个目标重用.灵活和扩展性. 2)什么是对 ...

  5. HTTP响应报文与工作原理详解(转)

    超文本传输协议(Hypertext Transfer Protocol,简称HTTP)是应用层协议.HTTP 是一种请求/响应式的协议,即一个客户端与服务器建立连接后,向服务器发送一个请求;服务器接到 ...

  6. 关于dom4j在maven中的使用

    在maven中添加dom4j的依赖, (如何使用eclipse构建maven项目, 这里就不在赘述) <!-- https://mvnrepository.com/artifact/org.do ...

  7. 【ros】【bug】gtk2\3 冲突

    ORBSLAM2首次运行出现GTK冲突. Gtk-ERROR **: GTK+ 3 symbols detected. Using GTK+ 2.x and GTK+ 3 in the same pr ...

  8. python3基础04(requests常见请求)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import requestsimport jsonimport reimport urllib3from ur ...

  9. 详细讲解:yii 添加外置参数 高级版本

    在YII中,添加状态参数的形式 首先,我们在advanced\common\config\params.php文件中,添加我们要设置的参数: 要在控制器中进行使用的话,形式为:\Yii::$app-& ...

  10. Centos7安装 PostgreSQL步骤

    1. 安装服务器即可. Yum install postgresql-server Yum install postgresql-contrib 2. 验证是否安装成功: rpm -aq| grep ...