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. 性能测试学习第四天_Loadrunner概述

    1.Loadrunner的组成部分 ·脚本生成器VuGen:virtual user generator ·压力控制器Controller,License主要在这里控制 ·结果分析软件Analysis ...

  2. Unity C# 运用 GetSaveFileName() 导出Excel文件

    本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html 唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根 ...

  3. ElasticSearch之QueryBuilders

    1.termsQuery A filer for a field based on several terms matching on any of them 翻译过来:基于以下几个方面对其中任何匹配 ...

  4. Centos 6.5 修改默认分辨率

    需要两步: 第一步: 编辑/etc/grub.conf文件,删除“nomodeset” 单词 ,翻到该页最后一行,就可以看到该词: 第二步: 删除文件/etc/X11/xorg.conf , Inte ...

  5. Xcode Ghost

    Xcode Ghost,是一种手机病毒,主要通过非官方下载的 Xcode 传播,能够在开发过程中通过 CoreService 库文件进行感染,使编译出的 App 被注入第三方的代码,向指定网站上传用户 ...

  6. Java项目—嗖嗖移动业务大厅

    嗖嗖移动业务大厅包类(如下图): SosoMgr: package cn.biz; import java.util.Scanner; import cn.common.Common; import ...

  7. String在方法中的传递方式(调用外部方法给String变量赋值时,未得到预期结果)

    示例: public class StringTraining { public static void changeStr(String str){ str = "137878" ...

  8. 原生js添加类名,删除类名

    1.添加类名: document.getElementById("myDiv").classList.add('mystyle'); 2.删除类名: document.getEle ...

  9. amap -bq 192.168.5.9 80 3306

    amap -bq 192.168.5.9 80 3306 查看运行在指定端口上运行的服务

  10. C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载

    编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...