http://codeforces.com/problemset/problem/919/D

就是先判环,如果有环就-1,否则对每个字母分开跑一下dp

错误记录:

1.有向图判环,自环一定要特判!(不能直接用强连通)

2.思考的时候很容易跑偏,去想把各个字母合在一起dp,不知道为什么?

 #include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
struct E
{
int to,nxt;
}e[];
int f1[],ne,sccc,sccnum[],top,dfn[],dfsc,low[];
int s[],n,m;
void me(int u,int v)
{
e[++ne].to=v;
e[ne].nxt=f1[u];
f1[u]=ne;
} void dfs(int u)
{
low[u]=dfn[u]=++dfsc;
s[++top]=u;
for(int k=f1[u],v;k!=;k=e[k].nxt)
{
v=e[k].to;
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!sccnum[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
sccc++;
while(top&&s[top]!=u) sccnum[s[top--]]=sccc;
sccnum[s[top--]]=sccc;
}
}
char ss[];
int ans[],in[],in2[],anss;
queue<int> q;
int main()
{
int c1,c2,i,k,t;char cc;
scanf("%d%d%s",&n,&m,ss+);
for(i=;i<=m;i++)
{
scanf("%d%d",&c1,&c2);
if(c1==c2)
{
puts("-1");
return ;
}
me(c1,c2);in2[c2]++;
}
for(i=;i<=n;i++)
if(!dfn[i])
dfs(i);
if(sccc!=n)
{
puts("-1");
return ;
}
for(cc='a';cc<='z';cc++)
{
memset(ans,,sizeof(ans));
memcpy(in,in2,sizeof(in));
for(i=;i<=n;i++)
if(!in[i])
q.push(i);
while(!q.empty())
{
t=q.front();q.pop();ans[t]+=ss[t]==cc;
anss=max(anss,ans[t]);
for(k=f1[t];k;k=e[k].nxt)
{
in[e[k].to]--;ans[e[k].to]=max(ans[e[k].to],ans[t]);
if(!in[e[k].to]) q.push(e[k].to);
}
}
}
printf("%d",anss);
return ;
}

Substring CodeForces - 919D的更多相关文章

  1. Codeforces 919D - Substring

    919D - Substring 思路: 拓扑排序判环+DAG上dp+记忆化搜索 状态:dp[i][j]表示以i为起点的路径中j的最大出现次数 初始状态:dp[i][j]=1(i have no so ...

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

    题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...

  3. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  4. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

  5. CodeForces - 919D Substring (拓扑排序+dp)

    题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...

  6. Codeforces 919D Substring (拓扑图DP)

    手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...

  7. Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )

    题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是  a ...

  8. The minimal unique substring CodeForces - 1159D (构造)

    核心观察是形如01,001,0001,...的串循环时, $n$每增长1, $k$就增长1. #include <iostream> #include <sstream> #i ...

  9. 【学习笔记】有向无环图上的DP

    手动博客搬家: 本文发表于20180716 10:49:04, 原地址https://blog.csdn.net/suncongbo/article/details/81061378 首先,感谢以下几 ...

随机推荐

  1. run as maven test报错解决办法

    eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...

  2. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  3. delphi中的HOOK [转贴]

    按事件分类,有如下的几种常用类型的钩子: 1)键盘钩子可以监视各种键盘消息. 2)鼠标钩子可以监视各种鼠标消息. 3)外壳钩子可以监视各种Shell事件消息. 4)日志钩子可以记录从系统消息队列中取出 ...

  4. Spark SQL is a Spark module for structured data processing.

    http://spark.apache.org/docs/latest/sql-programming-guide.html

  5. UILabel与UIFont的用法和属性的一些总结

    初始化一个UILabel对象,并初始化大小 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100) ...

  6. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

  7. 用 javascript 操作 xml

    1. [代码]js代码     <script language="JavaScript"><!--var doc = new ActiveXObject(&qu ...

  8. java中Math常用方法

    public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...

  9. 第一次通过AVD Manager创建了一个虚拟设备,但是在Android Studio运行程序时却无设备可选

    第一次通过AVD Manager创建了一个虚拟设备,但是在Android Studio运行程序时却无设备可选 原因是adb.exe未运行起来 至于adb.exe未正常运行起来的原因多半是5037端口被 ...

  10. 如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)

    前言 本次编写所用的库为于仕祺老师免费提供的人脸检测库.真心好用,识别率和识别速度完全不是Opencv自带的程序能够比拟的.将其配合Opencv的EigenFace算法,基本上可以形成一个小型的毕业设 ...