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

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章