Substring(Codeforces-D-拓扑排序)
3 seconds
256 megabytes
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.
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 a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1instead.
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 → 5. The value is 3 because the letter 'a' appears 3 times.
题意:n,m分别代表顶点个数和边的条数,输入一串字符串,输入边的信息,一条路径的价值是出现最多的那个字母的次数,让你求最大价值!例如案例1:最大路径为1->3->4->5,价值为a出现的次数,3次,所以输出3;
分析:拓扑排序+思维;记录好每到达一个顶点所有字母出现的次数,开不了300000*300000的数组就用vector,如果遍历的点的个数不等于n的话输出-1,否则二重循环查询到最大的价值!
#include<bits/stdc++.h>
#define N 300010
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int indegree[N];//记录每个节点的入度
int dp[N][]; //记录到达某个节点的时候26个字母所出现的次数
queue<int>w;
vector<int>v[N];
int n,m;
char s[N];
int ans;//记录遍历的节点数
int main()
{
cin>>n>>m;
cin>>s;
mem(indegree,);
int x,y;
for (int i=;i<=m;i++)
{
cin>>x>>y;
v[x].push_back(y);
indegree[y]++;
}
for (int i=;i<=n;i++)
if (!indegree[i])
w.push(i),dp[i][s[i-]-'a']++;
int q,p;
while (!w.empty())
{
q=w.front();
w.pop();
for (int i=;i<v[q].size();i++)
{
p=v[q][i];
indegree[p]--;
for (int j=;j<;j++)
{
if (j==s[p-]-'a') dp[p][j]=max(dp[p][j],dp[q][j]+);
else dp[p][j]=max(dp[p][j],dp[q][j]);
}
if(!indegree[p]) w.push(p);
}
ans++;
}
if (ans!=n)
cout << "-1" << endl;
else
{
int maxn=;
for (int i=;i<=n;i++)
for (int j=;j<;j++)
maxn=max(maxn,dp[i][j]);
cout << maxn << endl;
}
return ;
}
Substring(Codeforces-D-拓扑排序)的更多相关文章
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- Codeforces 1100E 拓扑排序
题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347 如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向 ...
- National Property CodeForces - 875C (拓扑排序)
大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...
- Codeforces 1159E 拓扑排序
题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...
- Codeforces 919D Substring (拓扑排序+树形dp)
题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- Codeforces 919D Substring 【拓扑排序】+【DP】
<题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )
题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是 a ...
随机推荐
- 2.Jenkins结合k8s完成Jenkins slave功能
1.构建镜像 下载基础镜像,这里使用openvz的包,下载地址为:https://wiki.openvz.org/Download/template/precreated,下载centos7的镜像 下 ...
- 项目开发git-短信验证-redis数据库
项目开发git操作 基本流程 """ 1.开发前,拉一次远程仓库 2.工作区进行开发 3.将开发结果提交到本地版本库 - git status查看时没有待处理的事件 4. ...
- springboot配置多个yml文件
新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...
- 163邮箱报错: 535 Error: authentication failed
今天更换新的异常信息发件邮箱,重新申请了一个新邮箱,SMTP功能已经开通,调用java代码报异常,错误信息为:535 Error: authentication failed.经过网上查证,原来新的1 ...
- Sublime Text 3 快捷键的汇总
Sublime Text 3非常实用,但是想要用好,一些快捷键不可或缺,所以转了这个快捷键汇总. 选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按 ...
- <黑马新秀>Spring学习日志
# 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...
- 吴裕雄--天生自然 PYTHON3开发学习:多线程
import _thread import time # 为线程定义一个函数 def print_time( threadName, delay): count = 0 while count < ...
- Entity Framework实现属性映射约定
Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...
- 爬虫—文件存储—CSV存储
一,简介 CSV,全称Comma—Separated Values,可以称为逗号分隔或者字符分隔值,其文件以纯文本形式存储表格数据.该文件是一个字符序列,可以有任意的数目记录组成,记录间已某种换行符分 ...
- Log4J基础
Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式.日志信息的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重要程度: ...