题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的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][拓扑排序]的更多相关文章

  1. Codeforces Round #460 (Div. 2) ABCDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...

  2. [Codeforces]Codeforces Round #460 (Div. 2)

    Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...

  3. 【Codeforces Round #460 (Div. 2) D】Substring

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  7. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  8. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  9. 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 ...

随机推荐

  1. java语法基础(一)

    这个是自己前两年做java视频教程时候的课件.感兴趣的同学可以参考下. 这里是纯粹的语法行总结. editplus的使用 选择项目目录 打开editplus 左侧目录导航栏 可以打开盘符,文件夹 可以 ...

  2. java根据文件流判断文件类型(后缀名)

    import java.io.FileInputStream; public class FileType{ public static String bytesToHexString(byte[] ...

  3. Lightoj 1016 - Brush (II)

    After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to ...

  4. c#截屏功能的实现

    using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Ru ...

  5. 3.ExtJs常用布局--layout详解(含实例)

    转自:https://blog.csdn.net/fifteen718/article/details/51482826

  6. Ueditor中代码的高亮和背景在前端页面的实现

    首先废话就不多说,这个富文本编辑器的下载和js等基本文件的导入略. 我的最终目标是这样的,我们在页面中的富文本框中输入代码,希望它能够被后台接受.存入数据库,当通过服务器将这些代码再一次显示在前台的页 ...

  7. Java多线程系列八——volatile和ThreadLocal

    参考资料: http://ifeve.com/java-memory-model-4/ http://www.infoq.com/cn/articles/java-memory-model-1 htt ...

  8. 安装phpwind报错

    在安装phpwind时,下面的报错提示是什么原因呢?  答:数据库密码应设置为空

  9. 类似查询mysql数据库的查询XML的JS类

    一个快捷操作XML数据库的Javascript接口对象,包含select.count.tables.fields等方法,能够像操作mysql等其它数据库一样操作XML数据库. if(document. ...

  10. springboot(十一)SpringBoot任务

    github地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1 ...