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 ...
随机推荐
- P2P平台爆雷不断到底是谁的过错?
早在此前,范伟曾经在春晚上留下一句经典台词,"防不胜防啊".而将这句台词用在当下的P2P行业,似乎最合适不过了.就在这个炎热夏季,P2P行业却迎来最冷冽的寒冬. 引发爆雷潮的众多P ...
- Random Access Iterator
Random Access Iterator 树型概率DP dp[u]代表以当前点作为根得到正确结果的概率 将深度最深的几个点dp[u]很明显是1 然后很简单的转移 有k次,但我们要先看一次的情况,然 ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之字典类型(dict)
字典!在Python中字典是另一种可变容器模型,可以存储任意类型的对象.是Python存储数据类型的最高级(maybe). 特点:1. 字典的存储方式和其他类型数据略有不同,是通过键(key)和值(v ...
- goweb-goweb基础
goweb DNS工作原理 在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析. 如果hosts里没 ...
- [转载]Python方法绑定——Unbound/Bound method object的一些梳理
本篇主要总结Python中绑定方法对象(Bound method object)和未绑定方法对象(Unboud method object)的区别和联系.主要目的是分清楚这两个极容易混淆的概念,顺便将 ...
- E. Delete a Segment(删除一个区间,让并区间最多)
题:https://codeforces.com/contest/1285/problem/E 题意:给定n个区间,最多删除一个区间,让最后的并区间个数最大 #include<bits/stdc ...
- Qt QGraphicsScene||GraphicsView函数刷新多次内存溢出问题
需将QGraphicsScene *scene = new QGraphicsScene;放入上面声明头文件中声明: cpp文件中声明: 使用: 需要添加这个 scene->clear(); 这 ...
- ThreadPoolExecutor自定义线程池
1.ThreadPoolExecutor创建线程池的构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long ...
- 天大IPv6使用指南(老校区)
天津大学是CERNET地区网络中心和地区主结点之一,提供良好的IPv6服务,在老校区最大接入宽带达到100Mbps,下载资源非常方便. 但是,在天大使用IPv6时,同学们是不是经常出现时断时续的现象呢 ...
- 关于TensorFlow2的tf.function()和AutoGraph的一些问题解决
在使用TensorFlow的AutoGraph的时候出现了一些问题,特此记录 AutoGraph did not convert this function. Try decorating it di ...