Codeforces 919D:Substring(拓扑排序+DP)
D. Substring
time limit: per test3 seconds
memory limit: per test256 megabytes
inputstandard: input
outputstandard: output
You are given a graph with nnn nodes and mmm 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 333. Your task is find a path whose value is the largest.
Input
The first line contains two positive integers n,m(1≤n,m≤300000)n,m (1≤n,m≤300000)n,m(1≤n,m≤300000), denoting that the graph has nnn nodes and mmm directed edges.
The second line contains a string sss with only lowercase English letters. The iii-th character is the letter assigned to the iii-th node.
Then m lines follow. Each line contains two integers x,y(1≤x,y≤n)x,y (1≤x,y≤n)x,y(1≤x,y≤n), describing a directed edge from xxx to yyy. Note that xxx can be equal to yyy and there can be multiple edges between xxx and yyy. 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-1−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→51→3→4→51→3→4→5. The value is 333 because the letter ‘a’ appears 333 times.
题意
给出有nnn个点和mmm条边的有向图,每个节点上有一个小写字母,求所有通路中出现次数最多的字母出现的次数,如果出现了无数次,输出−1-1−1
Solve
用拓扑排序判断图中是否有环,如果有环,那么环上的字母出现的次数为无限多次,输出−1-1−1。
如果能够进行拓扑排序,对于每次遍历的节点,更新当前路上字母出现次数的最大值
dp[i][j]dp[i][j]dp[i][j]表示到达节点iii,字母′a′+j'a'+j′a′+j出现的最大次数
Code
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define INF 0x7f7f7f7f
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int n,m;
char ch[maxn];
int visi[maxn];
// dp[i][j]表示到i位置,字母j出现的最多次数
int dp[maxn][50];
int cnt;
vector<int>v[maxn];
queue<int>que;
void topo()
{
for(int i=1;i<=n;i++)
if(!visi[i])
{
que.push(i);
dp[i][ch[i]-'a']++;
}
while(!que.empty())
{
cnt++;
int res=que.front();
que.pop();
int sz=v[res].size();
for(int i=0;i<sz;i++)
{
visi[v[res][i]]--;
if(!visi[v[res][i]])
que.push(v[res][i]);
for(int j=0;j<26;j++)
dp[v[res][i]][j]=max(dp[v[res][i]][j],dp[res][j]+(ch[v[res][i]]-'a'==j));
}
}
if(cnt<n)
cout<<-1<<endl;
else
{
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<26;j++)
ans=max(ans,dp[i][j]);
}
cout<<ans<<endl;
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
cin>>(ch+1);
ms(visi,0);
int x,y;
for(int i=0;i<m;i++)
{
cin>>x>>y;
v[x].push_back(y);
visi[y]++;
}
topo();
return 0;
}
Codeforces 919D:Substring(拓扑排序+DP)的更多相关文章
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- Codeforces 919D Substring (拓扑排序+树形dp)
题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...
- Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )
题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是 a ...
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- [NOIP2017]逛公园 最短路+拓扑排序+dp
题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...
- 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp
正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...
- 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 456 Solved: 215[Submit][Stat ...
- 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP
[BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...
- bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...
随机推荐
- C++之数组转换
题目如下: 这道题经过好久的思考也没找到能一次性输入两组数的方法,只能一次性处理一组数,所以就把代码放上来,欢迎交流留言一起讨论可以放两组数的方法~(QQ 841587906) 1 #include ...
- vue项目windows环境初始化
下载nodejs zip包并加载到环境变量 nodejs的版本最好使用12版,而不是最新版 npm install webpack -gnpm install -g yarnyarn config s ...
- ORACLE DBMS_ROWID包详解
这个包在11gR2中有11个函数或存储: 1. 根据给定参数返回一个rowid --根据给定参数返回一个rowid FUNCTION rowid_create(rowid_type IN NUMBER ...
- AFNetworking 网络错误提示data转换字符串
AFN在进行网络交互时,有时候会碰到返回502.500.404的时候.后台的总需要你配合他查出问题所在.但是AFN在返回数据序列化时解析错误只会转成NSData类型的数据,如果直接扔给后台Data的数 ...
- @NotBlank 注解不生效
1. @NotBlank 注解是用来校验 String 类型的参数是否为空的 2. 使用方法 (1)Spring-boot 某一个版本之前 spring-boot-starter-web 中有包含 h ...
- feignclient发送get请求,传递参数为对象
feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...
- spring的注解AOP配置
package com.hope.service.impl;import com.hope.service.IAccountService;import org.aspectj.lang.annota ...
- 网络协议之:基于UDP的高速数据传输协议UDT
目录 简介 UDT协议 UDT的缺点 总结 简介 简单就是美.在网络协议的世界中,TCP和UDP是建立在IP协议基础上的两个非常通用的协议.我们现在经常使用的HTTP协议就是建立在TCP协议的基础上的 ...
- 编译工具sbt部署
目录 一.简介 二.部署 三.测试 一.简介 项目构建工具是项目开发中非常重要的一个部分,充分利用好它能够极大的提高项目开发的效率.在学习SCALA的过程中,我遇到了SBT(Simple Build ...
- who 命令的实现
who 命令显示 当前已经登录的用户 查看连接帮助: man who who(1) 表示who的小结编号. NAME 包含命令的名字以及对这个命令的简短说明 SYNOPSYS 给出命令的用法说明,命 ...