Codeforces Round #460 (Div. 2) D. Substring BFS拓扑排序dp
Posted ProLightsfxjh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #460 (Div. 2) D. Substring BFS拓扑排序dp相关的知识,希望对你有一定的参考价值。
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard outputYou 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.
InputThe 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.
OutputOutput a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
Examples input5 4 abaca 1 2 1 3 3 4 4 5output
3input
6 6 xzyabc 1 2 3 1 2 3 5 4 4 3 6 4output
-1input
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 7output
4Note
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.
Source
Codeforces Round #460 (Div. 2)
My Solution
题意:给出一个可能有环可能不连通的图,找出一个路径其上出现最多的字母出现的次数最大,求这个最大值。
BFS、拓扑排序、dp
这题与以前一个求最长路径的题差不多,这里定义状态dp[i][j]表示从某点开始跑到节点i时路径上出现字母j+'a'的最大次数。
只需要按照拓扑序跑一遍dp即可,
ch[v] != j, dp[v][j] = max(dp[v][j], dp[u][j]);
ch[v] != j, dp[v][j] = max(dp[v][j], dp[u][j] + 1);
最后如果度数不为0的点则有环,ans无穷大,置为-1.
否则 ans = max(ans, dp[i][j]).
时间复杂度 O(26*n)
空间复杂度 O(26*n)
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
typedef pair<int, int> ii;
const int MAXN = 3e5 + 8;
const LL MOD = 142857;
string s;
LL ch[MAXN];
vector<int> sons[MAXN];
int deg[MAXN];
LL dp[MAXN][26];
queue<ii> que;
inline void bfs()
int fa, u, v, sz, i, j;
while(!que.empty())
u = que.front().first;
fa = que.front().second;
que.pop();
sz = sons[u].size();
for(i = 0; i < sz; i++)
v = sons[u][i];
if(v == fa) continue;
deg[v]--;
for(j = 0; j < 26; j++)
if(ch[v] != j) dp[v][j] = max(dp[v][j], dp[u][j]);
else dp[v][j] = max(dp[v][j], dp[u][j] + 1);
if(deg[v] == 0)
que.push(ii(v, u));
int main()
#ifdef LOCAL
freopen("d.txt", "r", stdin);
//freopen("d.out", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
LL n, m, ans, i, j, u, v, sz;
memset(deg, 0, sizeof deg);
memset(dp, 0, sizeof dp);
cin >> n >> m;
cin >> s;
for(i = 1; i <= n; i++)
ch[i] = s[i-1] - 'a';
for(i = 0; i < m; i++)
cin >> u >> v;
sons[u].push_back(v);
for(i = 1; i <= n; i++)
sz = sons[i].size();
for(j = 0; j < sz; j++)
deg[sons[i][j]]++;
for(i = 1; i <= n; i++)
if(deg[i] == 0)
que.push(ii(i, -1));
dp[i][ch[i]]++;
bfs();
ans = 0;
for(i = 1; i <= n; i++)
if(deg[i])
ans = -1;
break;
for(j = 0; j < 26; j++)
ans = max(ans, dp[i][j]);
cout << ans << endl;
return 0;
Thank you!
------from ProLights
以上是关于Codeforces Round #460 (Div. 2) D. Substring BFS拓扑排序dp的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #460 (Div. 2)
Codeforces Round #460 (Div. 2) 前三题
Codeforces Round #460 (Div. 2) 919 笔记
Codeforces Round #460 (Div. 2)