Codeforces Round #460 (Div. 2)-D Substring(Tarjian+记忆化搜索)

本文介绍了一道关于图论的问题,旨在寻找一条路径,该路径上的节点字母出现次数最多。文章详细阐述了解决方案,包括如何使用Tarjan算法检测环,并结合记忆化深度优先搜索优化算法。

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

D. Substring
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意:给你n个点,m条有向边,每个点由一个小写字母代表其权值,定一一条路径的权值为其经过的节点的字母中相同字母数量的最大值。要求输出所有路径中权值最大值,如果没有答案,输出-1。
思路:首先考虑没有答案的情况,即是有环的情况,所有跑一遍Tarjian,有环直接输出-1,还有一种情况需要特判一下,就是当存在一条边(u,v),u==v时,也算环,输入时直接特判输出-1即可。
然后考虑,如何求出答案,题目只要求最大路径的权值,所以我们只需要在Tarjian的同时,记录每个节点通过子节点所能经过的每个字母的最大值,同时更新答案,加上记忆化DFS的剪枝就可以过了。
AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=300000+100;
const int inf=2100;
const double eps=1e-4;
char str[maxn];
int ru[maxn],p[maxn],num,n,m,contnum,dfn[maxn],dep[maxn],vis[maxn],stacks[maxn],lid;
struct node
{
    int cont[30];
    int flag;
}x[maxn];
struct edge
{
    int en,next;
} e[400000];
void init()
{
    met(p,-1);
    num=0;
    contnum=1;
    lid=1;
}
void add(int st,int en)
{
    e[num].en=en;
    e[num].next=p[st];
    p[st]=num++;
}
int dfs(int u)
{
    stacks[++lid]=u;
    dfn[u]=dep[u]=contnum++;
    vis[u]=1;
    for(int i=p[u];i!=-1;i=e[i].next)
    {
        int v=e[i].en;
        if(vis[v]==0)
        {
            if(dfs(v))return 1;
        }
        if(vis[v]==1)dep[u]=min(dep[u],dep[v]);
        if(!x[u].flag)for(int i=0;i<26;i++)x[u].cont[i]=max(x[v].cont[i],x[u].cont[i]);//即使这个点已经被标记过,仍然要Tarjian,但是不需要更新答案了
    }
    if(dfn[u]==dep[u])
    {
        int check=0;
        do
        {
            vis[stacks[lid]]=-1;
            check++;
        }
        while(stacks[lid--]!=u);
        if(check>1)return 1;//这里直接判断是否成环即可,不需要染色
    }
    if(!x[u].flag)x[u].cont[(int)str[u]-'a']++;//如果是第一次经过,要加上当前节点的字母值
    x[u].flag=1;
    return 0;
}
int main()
{
    int ans=0;
    scann(n,m);
    init();
    scanf("%s",str+1);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scann(x,y);
        add(x,y);
        if(x==y)
        {
            prin(-1);
            return  0;
        }
        ru[y]++;
    }
    for(int i=1;i<=n;i++)
    {
        if (!vis[i])
        {
            if (dfs(i))
            {
                prin(-1);
                return 0;
            }

        for (int j = 0; j < 26; j++)ans = max(ans, x[i].cont[j]);
        }
    }
    prin(ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值