Codeforces 919 D Substring
题目描述
You are given a graph with nn nodes and mm 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 33 . Your task is find a path whose value is the largest.
输入输出格式
输入格式:
The first line contains two positive integers n,mn,m ( 1<=n,m<=3000001<=n,m<=300000 ), denoting that the graph has nn nodes and mmdirected edges.
The second line contains a string ss with only lowercase English letters. The ii -th character is the letter assigned to the ii -th node.
Then mm lines follow. Each line contains two integers x,yx,y ( 1<=x,y<=n1<=x,y<=n ), describing a directed edge from xx to yy . Note that xx can be equal to yy and there can be multiple edges between xx and yy . Also the graph can be not connected.
输出格式:
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
输入输出样例
5 4
abaca
1 2
1 3
3 4
4 5
3
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
-1
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
4
说明
In the first sample, the path with largest value is 1→3→4→51→3→4→5 . The value is 33 because the letter 'a' appears 33 times.
XJB DP就行了,之前判一下是不是DAG
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
#define maxn 300005
using namespace std;
int ans=0,g[maxn];
int n,m,id[maxn],u,v;
int f[maxn],hd[maxn];
int to[maxn],ne[maxn];
bool vis[maxn];
char s[maxn]; inline int num(char x){
return x-'a';
} inline bool topsort(){
queue<int> q;
int x,tot=0; for(int i=1;i<=n;i++) if(!id[i]) q.push(i); while(!q.empty()){
x=q.front(),q.pop(),tot++;
for(int i=hd[x];i;i=ne[i]) if(!(--id[to[i]]))
q.push(to[i]);
} return tot==n;
} int dp(int x){
if(vis[x]) return g[x];
vis[x]=1,g[x]=0;
for(int i=hd[x];i;i=ne[i]) g[x]=max(g[x],dp(to[i]));
g[x]+=f[x];
return g[x];
} int main(){
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
to[i]=v,ne[i]=hd[u];
hd[u]=i,id[v]++;
} if(!topsort()){
puts("-1");
return 0;
} for(int i=0;i<26;i++){
memset(vis,0,sizeof(vis));
for(int j=1;j<=n;j++) f[j]=(num(s[j])==i); for(int j=1;j<=n;j++) ans=max(ans,dp(j));
} printf("%d\n",ans);
return 0; }
Codeforces 919 D Substring的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- CF 919 D. Substring
D. Substring 链接 题意: 在一张有向图中,定义路径的权值为路径中出现次数最多的字符出现的次数,求一条权值最大的路径.如果权值可以无限大,输出-1. 分析: 注意是一张有向图.如果存在环那 ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- Codeforces 919 E Congruence Equation
题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1&l ...
- Codeforces 919 C. Seat Arrangements
C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 919 B. Perfect Number
B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- getsupportfragmentmanager 没有这个方法
让activity继承自fragmentactivity就行了.
- 电脑卡,eclipse Android stadio 卡,什么都卡解决方法
昨天还好好的,今天什么都没有动就很卡.Android stadio 半天,改了东西才编译.什么都慢一拍,你能感觉到,打开网页也好,什么也好. 莫名的问题,总是被莫名的解决.真的,下了个360杀毒,没效 ...
- python django 路由系统
URL配置 基本格式: from django.conf.urls import url urlpatterns = [ url(正则表达式, views ...
- 第一次接触php
一.什么是PHP PHP的中文意思:超文本预处理器,英文名字: HyperText Preprocessor. PHP通常有两层含义: (1)PHP是一个编程语言. (2)PHP是处理PHP编程语言的 ...
- nyoj 325
zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...
- Mysql实战之数据备份
author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 数据备份和恢复 mysqldump 冷备份单库(不会创建新库,需要手动创建并指定导入数 ...
- KM算法【带权二分图完美匹配】
先orz litble--KM算法 为什么要用KM算法 因为有的题丧心病狂卡费用流 KM算法相比于费用流来说,具有更高的效率. 算法流程 我们给每一个点设一个期望值[可行顶标] 对于左边的点来说,就是 ...
- BZOJ4540 [Hnoi2016]序列 【莫队 + ST表 + 单调栈】
题目 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称a[s:t]是a[ ...
- svg动画 之 我的自制太阳系
SVG意为可缩放矢量图形,svg的图片与普通的jpg,png等图片相比,其优势在于不失真.一般普通的图片放大后,会呈现出锯齿的形状,但是svg图片则不会这样,它可以被高质量地打印. 现在就用dream ...
- pat 甲级 Cars on Campus (30)
Cars on Campus (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 Zhejiang University ...