Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]
题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的value等与这条路径上出现次数最多的字母的数目,求该图的最大value
比赛时,用dfs超时,看官方题解用的dp和拓扑排序,a--z用0-25表示,用dp[i][j]表示以第i个结点结尾的路径上第j个字母出现的次数
拓扑排序每排到一个点,就用该点的dp去更新与它相邻点的dp,最开始入度为0的点特殊处理了一下,dp过程中同步更新结果res
也复习了一下拓扑排序
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define Nnum 300005 char ch[Nnum];
int dp[Nnum][]; vector<int> eage[Nnum];
int deg[Nnum];
int n,m,res;
bool deg0[Nnum]; bool toposort()
{
int cnt=;
queue<int> que;
for(int i=; i<=n; i++)
if(deg[i]==)
{
cnt++;
que.push(i);
deg0[i]=;
}
else
deg0[i]=;
while(!que.empty())
{
int now=que.front();
que.pop();
if(deg0[now]==) //最初入度为0的点,其dp需附上初值,再更新相邻结点
dp[now][ch[now-]-'a']++;
for(int i=; i<eage[now].size(); i++)
{
for(int j=; j<; j++)
{
int tmp=ch[eage[now][i]-]-'a';
if(j==tmp)
dp[eage[now][i]][tmp]=max(dp[eage[now][i]][tmp],dp[now][tmp]+);
else
dp[eage[now][i]][j]=max(dp[eage[now][i]][j],dp[now][j]);
res=max(res,dp[eage[now][i]][j]);
}
//cout<<"*"<<res<<endl;
deg[eage[now][i]]--;
if(deg[eage[now][i]]==)
{
que.push(eage[now][i]);
cnt++;
}
}
}
if(cnt==n)
return ;
else
return ;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
eage[i].clear();
res=;
memset(deg,,sizeof(deg));
memset(dp,,sizeof(dp));
scanf("%s",ch);
for(int i=; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
deg[b]++;
eage[a].push_back(b);
}
if(toposort())
printf("%d\n",res);
else
printf("-1\n");
}
return ;
}
Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]的更多相关文章
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- [Codeforces]Codeforces Round #460 (Div. 2)
Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...
- 【Codeforces Round #460 (Div. 2) D】Substring
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...
随机推荐
- forword 与 redirect
直接转发方式(Forward) 客户端和浏览器只发出一次请求,Servlet.HTML.JSP或其它信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每个信息资源是共享的 ...
- IDE配置jvm参数
-------- IntelliJ IDEA 配置参数:-Xms34m -Xmx234m 内存初始化大小,最小和最大值: 测试代码: public class JVMDemoTest { public ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- 一步一步弄懂angularJS基础
问题1:ng-app指令的使用以及自定义指令 <!doctype html> <!--这里的ng-app的属性值就是模块的名称,也就是 angular.module("My ...
- Multi-threading Android Apps for Multi-core Processors – Part 1 of 2
Can my single-threaded application benefit from multiple cores? How? Even a single-threaded applicat ...
- 使用C#开发HTTP服务器系列之Hello World
各位朋友大家好,我是秦元培,欢迎大家关注我的博客.从今天起,我将开始撰写一组关于HTTP服务器开发的系列文章.我为什么会有这样的想法呢?因为人们对Web技术存在误解,认为网站开发是Web技术的全部.其 ...
- oracle分区表有什么作用
oracle分区表有什么作用 https://zhidao.baidu.com/question/1818955865408544348.html (1) 表空间及分区表的概念 表空间: 是一个或多个 ...
- str函数isdigit、isdecimal、isnumeric的区别
s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...
- ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(四)图书信息的增删改查
前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/asp ...
- bzoj 2006: [NOI2010]超级钢琴【st表+堆】
设计一个五元组(i,l,r,p,v),表示在以i为左端点,右端点落在(l,r)中的情况下,取最大值v时右端点落在p.把这个五元组塞到优先队列里,以v排序,每次取出一个,然后把这个取过的五元组分成两个( ...