CF Round#460 D - Substring
作者:互联网
D - Substring
拓扑排序
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
int n, m;
string s;
vector<int> G[N];
int f[N][26];
int din[N];
void add(int a, int b)
{
G[a].push_back(b);
din[b]++;
}
int topsort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
{
if (!din[i])
{
q.push(i);
f[i][s[i] - 'a'] = 1;
}
}
int cnt = 0;
while(!q.empty())
{
int u = q.front();
cnt++;
q.pop();
for (int v : G[u])
{
din[v]--;
int t = s[v] - 'a';
for (int i = 0; i < 26; i++)
f[v][i] = max(f[v][i], f[u][i]);
f[v][t] = max(f[v][t], f[u][t] + 1);
if (!din[v])
q.push(v);
}
}
if (cnt != n)
return -1;
int maxn = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < 26; j++)
maxn = max(maxn, f[i][j]);
return maxn;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> s;
s = " " + s;
while(m--)
{
int a, b;
cin >> a >> b;
add(a, b);
}
cout << topsort() << endl;
return 0;
}
标签:din,int,CF,long,460,Substring,add,include 来源: https://www.cnblogs.com/hzy717zsy/p/16289773.html