Codeforces Round #460 (Div. 2) D (拓扑判环+DAG上DP)

本文介绍了一道关于寻找有向图中最大路径权值的问题,并提供了完整的解决方案。通过拓扑排序判断是否存在环,若存在则权值无限大;若不存在,则利用DFS+DP的方法求解最大权值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Substring

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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

Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -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 → 5. The value is 3 because the letter 'a' appears 3 times.

题目大意:给定一个有向图,每个点有一个权值,用一个字母表示。一条路径的权值就是这条路径上出现次数最多的字母的个数。题目要求找出给定有向图的最大权值。

当图中存在环时,则权值可为无穷大,此时无解。可用拓扑排序判环。

排除上面情况,有向图必为DAG。每次选择入度为零的点,保证每条路径都从头开始搜,DFS DP答案即可。搜过的点可直接合并答案,不必再搜下去,保证了复杂度O(m)。取最大值即为答案。

代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct node
{
    int v,tol;
    node(int vv = 0,int tt = 0): v(vv),tol (tt){}
    bool operator < (const node & t) const
    {
        return tol > t.tol;
    }
}node;
const int maxn = 3e5 + 5;
// 拓扑排序判环
// 找入度为零的点记忆化搜索
vector<int> G[maxn];
int in[maxn],in_tsp[maxn];// 入度
int dp[maxn][26];// 记录dp结果
bool vis[maxn];// 标记数组
bool TPS(int sum)
{
    priority_queue<node> q;
    for(int i = 0;i < sum; ++i)
    {
        q.push(node(i,in[i]));
        in_tsp[i] = in[i];
    }
    node p;
    while(!q.empty() && sum)
    {
        p = q.top();
        q.pop();
        int x = p.v;
        if(p.tol > 0) return false;
        if(vis[x]) continue;
        vis[x] = true;
        int len = G[x].size();
        for(int i = 0;i < len; ++i)
        {
            int u = G[x][i];
            if(!vis[u]) q.push(node(u,--in_tsp[u]));
        }
        --sum;
    }
    return true;
}
void DFS(int x)
{
    int len = G[x].size();
    int tp[26] = {0};
    for(int i = 0;i < len; ++i)
    {
        int u = G[x][i];
        if(!vis[u])
        {
            vis[u] = true;
            DFS(u);
        }
        for(int j = 0;j < 26; ++j) tp[j] = max(tp[j],dp[u][j]);
    }
    for(int i = 0;i < 26; ++i) dp[x][i] += tp[i];
}

int main()
{
    int x,y;
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i = 0;i < n; ++i)
    {
        vis[i] = false;
        in[i] = 0;
        for(int j = 0;j < 26; ++j) dp[i][j] = 0;
    }
    char p;
    getchar();
    for(int i = 0;i < n; ++i) dp[i][getchar() - 'a'] = 1;
    for(int i = 0;i < m; ++i)
    {
        scanf("%d %d",&x,&y);
        G[x - 1].push_back(y - 1);
        in[y - 1]++;
    }
    int ans = -1;
    if(TPS(n))
    {;
        memset(vis,false,sizeof(vis));
        for(int i = 0;i < n; ++i)
        {
            if(!in[i])
            {
                vis[i] = true;
                DFS(i);
                for(int j = 0;j < 26; ++j) ans = max(ans,dp[i][j]);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值