D. Substring
time limit per test3 seconds
memory limit per test256 megabytes
Problem Description
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
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.
解题心得:
- 比赛的时候读错了题写到崩溃啊。其实题意是每个点用一个字母表示,一个人随机从一个点开始走,获得的值是他走过的路径中遇到的字母(次数最多的那个)的次数。问这个人在途中可能获得的最大值是多少。如果有环输出-1。
- 写得贼复杂,tarjan判断环,map去除重边,记忆化搜索得到答案。不想说话去角落默默呆着。
我的智障代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+100;
vector <int> ve[maxn];
map <pair<int,int>,int> maps;
char s[maxn];
bool find_cir,in[maxn];
int n,m,Max,dfn[maxn],low[maxn],dp[maxn][26];
stack <int> st;
void init(){
Max = -1;
scanf("%s",s+1);
for(int i=0;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
if(a == b)//自身到自身形成环
find_cir = true;
if(maps[make_pair(a,b)] == 233)//去除重边
continue;
maps[make_pair(a,b)] = 233;
ve[a].push_back(b);
}
}
int tot = 0;
void tarjan(int x){
dfn[x] = low[x] = ++tot;
st.push(x);
in[x] = true;
for(int i=0;i<ve[x].size();i++){
int v = ve[x][i];
if(!dfn[v]){
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(in[v])
low[x] = min(low[x],dfn[v]);
}
int num = 0;
if(low[x] == dfn[x]){
while(1){
int now = st.top();
st.pop();
in[now] = false;
num++;
if(now == x)
break ;
}
if(num > 1){
find_cir = true;
return ;
}
}
}
void judge_cir(){//用tarjan判断有没有环
for(int i=1;i<=n;i++)
if(!dfn[i]){
tarjan(i);
}
}
int dfs(int u,int c){
if(dp[u][c] != -1)
return dp[u][c];
int res = 0;
for(int i=0;i<ve[u].size();i++){
int v = ve[u][i];
res = max(res,dfs(v,c));
}
res += (s[u]-'a' == c);
return dp[u][c] = res;
}
void get_Max(){
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=0;j<26;j++)
Max = max(Max,dfs(i,j));
printf("%d",Max);
return;
}
int main(){
scanf("%d%d",&n,&m);
init();
judge_cir();
if(find_cir){
printf("-1");
return 0;
}
get_Max();
}
轻松过:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+100;
vector <int> ve[maxn];
int Max = -1,n,m,dp[maxn][30];
char s[maxn];
void init(){
memset(dp,-1,sizeof(dp));
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=0;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
}
}
int dfs(int u,int c){
int res = 0;
if(dp[u][c] == -2){//在之前已经走过这条边,形成环
printf("-1");
exit(0);
}
if(dp[u][c] != -1)
return dp[u][c];
dp[u][c] = -2;
for(int i=0;i<ve[u].size();i++){
int v = ve[u][i];
res = max(res,dfs(v,c));
}
res += (s[u]-'a' == c);
return dp[u][c] = res;
}
void get_Max(){
for(int i=1;i<=n;i++)
for(int j=0;j<26;j++){
Max = max(Max,dfs(i,j));
}
printf("%d",Max);
}
int main(){
init();
get_Max();
}