Codeforces Round #460 (Div. 2)-D. Substring
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。
- 写得贼复杂,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的更多相关文章
- 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 ...
- 【Codeforces Round #460 (Div. 2) D】Substring
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- Codeforces Round #460 (Div. 2)
A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...
- [Codeforces]Codeforces Round #460 (Div. 2)
Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...
- Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)
题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...
- Codeforces Round #460 (Div. 2) 前三题
Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...
- Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理
E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #460 (Div. 2)-C. Seat Arrangements
C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...
随机推荐
- Spring注解之@Lazy注解,源码分析和总结
一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...
- ElasticSearch最新版本下载地址
直接访问官方很慢,打不开,找到这个下载方法: ElasticSearch下载地址: https://download.elastic.co/elasticsearch/elasticsearch/el ...
- JS 获取 今日、昨日、本周、本月、本季度、本年、上月、上周、上季度、去年
/** * 日期范围工具类 */ var dateRangeUtil = (function () { /*** * 获得当前时间 */ this.getCurrentDate = function ...
- WPF学习二:TextBlock和Label的区别
TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...
- uLua学习之使用协程(终)
前言 今天是本系列的第六篇文章,也是最后一篇,我们来看看uLua中如何来实现协程吧.首先,让我们明确协程的概念.在百度百科上的是这样说的,协程更适合于用来实现彼此熟悉的程序组件,如合作式多任务,迭代器 ...
- python 之 BeautifulSoup 常用提取
一.bs4信息提取后返回的数据类型 soup.find('tbody') ---> 返回结构是一个bs4.element.Tag soup.find('tbody').children ---& ...
- mybatis-关联关系
在实现实列中我们在学生表里面增加了一个地址表用于与学生表的一对一 1.创建地址实体类: package com.java1234.mappers; import com.java1234.model. ...
- YII2 定义页面提示
控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...
- ascii码与unicode码的区别
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码)是基于拉丁字母的一套电脑编码系统.它主要用于显示现代英语和其他西 ...
- IOS UITabBarController(控制器)的子控制器
UITabBarController的简单使用 ● UITabBarController的使用步骤 ➢ 初始化UITabBarController ➢ 设置UIWindow的rootViewContr ...