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.
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 a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
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 |
In the first sample, the path with largest value is 1 → 3 → 4 → 5 1 → 3 → 4 → 5 . The value is 3 because the letter ‘a’ appears 3 times.
题意:给一个含n个结点和m条有向边的图。其中每一个结点都有一个小写字母与其对应。定义在一条路径上出现各个字母的最大次数为这条路径的value,求所有路径中最大的value。如果value是无限大,则输出-1。
思路:因为是最大value,同一条路径上,长的路径的value一定不小于短的路径,所以每条路径遍历到末尾就一定能找到该路径的最大value。
#include
#include//存储图和树比较方便的容器
#include//为了使用memset()
#include//为了使用exit()
using std::vector;
#define c2n(ch) (ch-'a')//一个把a~z 映射到0~25的宏,便于用数组记录
#define max(a,b) ((a)>(b)?(a):(b))//比较大小的宏
const int maxn = 300005;
int n, m, a, b;
bool rt[maxn];//为了统计哪些结点是路径的起点
int dp[maxn][30];//动态规划的辅助数组
char lst[maxn];//记录每个结点对应的字母
vector<int>node[maxn];
int vis[maxn] = { 0 };//检查环的辅助数组
void solve(int num) {
vis[num] = true;
int next,l=node[num].size();
for (int sn = 0; sn < l; ++sn)
{//先序遍历,先处理所有儿子
next = node[num][sn];
if (vis[next] == false)
solve(next);
}
//运行到这里说明所有儿子结点已经处理完毕(或者是叶子结点)
for (int i = 0; i < 26; ++i) {//对于每个字母分别计数
for (int sn = 0; sn < l; ++sn)//如果是叶子结点l=0,就没有这一步
{//比较所有儿子的记录,选择最大的那一个
dp[num][i] = max(dp[num][i], dp[node[num][sn]][i]);
}
}
//在子节点中为各个字母选择最佳的路线后
//因为是从它出发,它所含字母的那条路线还应该+1
dp[num][c2n(lst[num])]++;
}
int next;
void findring(int num) {//检查有无环的存在
vis[num] = -1;
int l = node[num].size();
for (int i = 0; i < l; ++i) {
next = node[num][i];
if (vis[next] == 0)
findring(next);
else if (vis[next] == -1)
{
printf("-1");//遇到环可以直接输出-1然后结束程序
exit(0);
}
}
vis[num] = 1;//该结点的状态更新为从他开始往下不会有环
}
int main() {
scanf("%d %d", &n, &m);
//scanf("%s", lst);
for (int i = 1; i <= n; ++i)
while (1) {
scanf("%c", &lst[i]);
if ('a' <= lst[i] && 'z' >= lst[i])
break;
}
memset(rt, 1, sizeof rt);
for (int i = 0; i < m; ++i) {
scanf("%d %d", &a, &b);
node[a].push_back(b);
rt[b] = false;//b一定不是起点
}
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; ++i) {
if (vis[i] == false)
findring(i);
}
memset(vis, 0, sizeof vis);
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (rt[i])//所有起点都要考虑一下
solve(i);
}
for (int i = 1; i <= n; ++i)
if (rt[i])//比较所有起点的,选最大的
for (int k = 0; k < 26; ++k)
if (ans < dp[i][k])
ans = dp[i][k];
printf("%d", ans);
}