码迷,mamicode.com
首页 > 编程语言 > 详细

Substring(Codeforces-D-拓扑排序)

时间:2018-02-02 00:39:14      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:des   multiple   ble   positive   between   lines   inpu   queue   信息   

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

 

 

 

 

Substring(Codeforces-D-拓扑排序)

标签:des   multiple   ble   positive   between   lines   inpu   queue   信息   

原文地址:https://www.cnblogs.com/lisijie/p/8396388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!