Codeforces 919 D Substring

题目描述

You are given a graph with nn nodes and mm 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 33 . Your task is find a path whose value is the largest.

输入输出格式

输入格式:

The first line contains two positive integers n,mn,m ( 1<=n,m<=3000001<=n,m<=300000 ), denoting that the graph has nn nodes and mmdirected edges.

The second line contains a string ss with only lowercase English letters. The ii -th character is the letter assigned to the ii -th node.

Then mm lines follow. Each line contains two integers x,yx,y ( 1<=x,y<=n1<=x,y<=n ), describing a directed edge from xx to yy . Note that xx can be equal to yy and there can be multiple edges between xx and yy . 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.

输入输出样例

输入样例#1:

5 4
abaca
1 2
1 3
3 4
4 5

输出样例#1:

3

输入样例#2:

6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4

输出样例#2:

-1

输入样例#3:

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

输出样例#3:

4

说明

In the first sample, the path with largest value is 1→3→4→51→3→4→5 . The value is 33 because the letter ‘a‘ appears 33 times.

XJB DP就行了,之前判一下是不是DAG

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
#define maxn 300005
using namespace std;
int ans=0,g[maxn];
int n,m,id[maxn],u,v;
int f[maxn],hd[maxn];
int to[maxn],ne[maxn];
bool vis[maxn];
char s[maxn];

inline int num(char x){
	return x-‘a‘;
}

inline bool topsort(){
	queue<int> q;
	int x,tot=0;

	for(int i=1;i<=n;i++) if(!id[i]) q.push(i);

	while(!q.empty()){
		x=q.front(),q.pop(),tot++;
		for(int i=hd[x];i;i=ne[i]) if(!(--id[to[i]]))
		    q.push(to[i]);
	}

	return tot==n;
}

int dp(int x){
	if(vis[x]) return g[x];
	vis[x]=1,g[x]=0;
	for(int i=hd[x];i;i=ne[i]) g[x]=max(g[x],dp(to[i]));
	g[x]+=f[x];
	return g[x];
}

int main(){
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&u,&v);
		to[i]=v,ne[i]=hd[u];
		hd[u]=i,id[v]++;
	}

	if(!topsort()){
		puts("-1");
		return 0;
	}

	for(int i=0;i<26;i++){
		memset(vis,0,sizeof(vis));
		for(int j=1;j<=n;j++) f[j]=(num(s[j])==i);

		for(int j=1;j<=n;j++) ans=max(ans,dp(j));
	}

	printf("%d\n",ans);
	return 0;

}

  

原文地址:https://www.cnblogs.com/JYYHH/p/8474694.html

时间: 2024-08-01 12:48:06

Codeforces 919 D Substring的相关文章

CodeForces - 873B Balanced Substring(思维)

inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l,?r] of s is a string slsl?+?1sl?+?2- sr, and its length equals to r?-?l?+?1. A substring is called balanced if the number of zero

Codeforces 919 E Congruence Equation

题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1<=n<=x ) satisfy  where a,b,pa,b,p are all known constants. 输入输出格式 输入格式: The only line contains four integers a,b,p,xa,b,p,x ( 2<=p<=10^{6}+32<

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

Codeforces 163A Substring and Subsequence

http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在LCS上加点改动 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 const int Mod=

Codeforces 919D Substring (拓扑排序+树形dp)

题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个有向图构成一个环的时候就会使得值无限大,所以先用拓扑排序判断一下有没有环,如果有环直接输出-1, 如果没有环就再使用树形dp并记忆化存数,来找到最大值. 代码: 1 #include<cstring> 2 #include<iostream> 3 using namespace std

Codeforces 873 B. Balanced Substring(前缀和 思维)

题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. 1 #include<bi

Codeforces Round #460 (Div. 2) 919 笔记

A. Supermarket 输入n,m, (1?≤?n?≤?5?000, 1?≤?m?≤?100)表示n组价格数据和目标重量m 接下来n组价格数据,表示为a元b千克,每组无限取 求最小花费 B. Perfect Number 输入k,1?≤?k?≤?10?000,求第k个完美数 完美数定义为数位和=10 (tutorial中说难度可以升级为k<1e18)->用数位dp可解 C. Seat Arrangements 输入n,m,k (1?≤?n,?m,?k?≤?2?000)表示nm的课室,有k

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

D2. RGB Substring (hard version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a string ss cons