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