Substring(Codeforces-D-拓扑排序)

D. Substring
time limit per test
3 seconds
memory limit per test
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.

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 -1instead.

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.

题意: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][30]; //记录到达某个节点的时候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,0);
  int x,y;
  for (int i=1;i<=m;i++)
  {
      cin>>x>>y;
      v[x].push_back(y);
      indegree[y]++;
  }
  for (int i=1;i<=n;i++)
    if (!indegree[i])
     w.push(i),dp[i][s[i-1]-'a']++;
  int q,p;
  while (!w.empty())
  {
      q=w.front();
      w.pop();
      for (int i=0;i<v[q].size();i++)
      {
          p=v[q][i];
          indegree[p]--;
          for (int j=0;j<26;j++)
            {
                if (j==s[p-1]-'a')  dp[p][j]=max(dp[p][j],dp[q][j]+1);
                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=0;
          for (int i=1;i<=n;i++)
             for (int j=0;j<26;j++)
               maxn=max(maxn,dp[i][j]);
            cout << maxn << endl;
      }
  return 0;
}

 

 

 

 

转载于:https://www.cnblogs.com/lisijie/p/8396388.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值