题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的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. mongo03

    Mongo存储的单位是文档,文档是js对象, use test2 db.createCollection("stu")//隐士创建库显示创建表 db.stu.,name:" ...

  2. YTU 2864: 分跑道。

    2864: 分跑道. 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 19 题目描述 有N个人参加100米短跑比赛.跑道为8条.程序的任务是按照尽量使每组的人数相差最少的原 ...

  3. center os 安装mysql5.6

    软件 MySQL-server-5.6.13-1.el6.x86_64.rpm MySQL-client-5.6.13-1.el6.x86_64.rpm 安装命令 rpm -ivh MySQL-ser ...

  4. linux WEB服务器***

    Apache sudo apt-get install apache2 PHP sudo apt-get install php5 sudo apt-get install php5-gd     / ...

  5. Entity Framework Code First 迁移

    Entity Framework CodeFirst数据迁移 http://www.cnblogs.com/aehyok/p/3325459.html Entity Framework Code Fi ...

  6. 杭电acm5698-瞬间移动(2016"百度之星" - 初赛(Astar Round2B))

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5698 Problem Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列), ...

  7. bzoj 1070: [SCOI2007]修车【最小费用最大流】

    一开始从客人角度想的,怎么建都不对 从一个修车工所接待的所有顾客花费的总时间来看,设一共有x个人,那么第一个修的对总时间的贡献是x*w1,第二个是(x-1)*w2-以此类推.所以把第i个修车工拆成n组 ...

  8. P2093 [国家集训队]JZPFAR(KDTree)

    传送门 类似于p4357 不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符 //minamoto #include<bits/stdc++.h> #define R regi ...

  9. (5)css盒子模型(基础上)

    CSS 盒子模型概述 ***什么是CSS的盒子模型呢?为什么叫它是盒子?先说说我们在网页设计中常听的属性名:内容(content).边框(border).内边距(padding).外边距(margin ...

  10. 报错Cannot determine embedded database driver class for database type NONE解决方法

    由于我不需要数据库,启动springboot报错: Cannot determine embedded database driver class for database type NONE If ...