Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
3 seconds
256 megabytes
standard input
standard output
You are given a graph with n nodes and m 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 3. Your task is find a path whose value is the largest.
The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges.
The second line contains a string s with only lowercase English letters. The i-th character is the letter assigned to the i-th node.
Then m lines follow. Each line contains two integers x, y (1 ≤ x, y ≤ n), describing a directed edge from x to y. Note that x can be equal to y and there can be multiple edges between x and y. 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.
5 4
abaca
1 2
1 3
3 4
4 5
3
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
-1
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
4
In the first sample, the path with largest value is 1 → 3 → 4 → 5. The value is 3 because the letter 'a' appears 3 times.
题意:给一个由字母代替节点的有向图,一条路径的值为出现次数最多的字母出现次数,求出路径的最大值(如果无穷大输出-1)
思路:有环就是-1,接下来就是DAG,暴力26个字母,对于当前字母x,将所有为x的节点权值设为1,其它节点权值设为0,就是一个非常简单的DP了。
代码:
//#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define inf 0x3f3f3f3f
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 3e5 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
vector<int> g[N];
queue<int> q;
char s[N];
int in[N],deg[N],vis[N],f[N],val[N];
int n,m,ans;
bool cal()//判环
{
int cnt=;
for(int i=;i<=n;i++){
if(!in[i]) q.push(i),cnt++;
vis[i]=;
}
while(q.size()){
int v=q.front();
q.pop();
for(int i=;i<g[v].size();i++){
int vv=g[v][i];
in[vv]--;
if(!in[vv]) vis[vv]=,q.push(vv),cnt++;
}
}
return cnt==n;
}
void dfs(int u)//DP(DAG)
{
f[u]=val[u];
vis[u]=;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(!vis[v]) dfs(v);
f[u]=max(f[u],f[v]+val[u]);
}
ans=max(f[u],ans);
}
int main()
{
ci(n),ci(m);
scanf("%s",s+);
for(int i=;i<m;i++){
int x,y;
ci(x),ci(y);
g[x].push_back(y);
in[y]++,deg[y]++;
}
if(!cal()) puts("-1");
else
{
for(int i=;i<;i++){
memset(vis,, sizeof(vis));
memset(f,, sizeof(f));
for(int j=;j<=n;j++){//节点赋值
if(s[j]=='a'+i) val[j]=;
else val[j]=;
}
for(int j=;j<=n;j++){
if(!deg[j]) dfs(j);
}
}
pi(ans);
}
return ;
}
Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)的更多相关文章
- Codeforces Round #460 (Div. 2)-D. Substring
D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...
- Codeforces Round #460 (Div. 2)
A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- 【Codeforces Round #460 (Div. 2) D】Substring
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...
- 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) E. Congruence Equation (CRT+数论)
题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...
- Codeforces Round #374 (Div. 2) C(DAG上的DP)
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #460 (Div. 2) 前三题
Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...
随机推荐
- 从0到1构建适配不同端(微信小程序、H5、React-Native 等)的taro + dva应用
从0到1构建适配不同端(微信小程序.H5.React-Native 等)的taro + dva应用 写在前面 Taro 是一套遵循 React 语法规范的 多端开发 解决方案.现如今市面上端的形态多种 ...
- bai_du 采集代码(已过期)
<?php $url = "http://www.baidu.com/s?wd=site:www.xxxxxx.com+inurl:hot&tn=baidulaonian&am ...
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...
- SpringCloud的学习记录(3)
这一章节讲搭建config-server的项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...
- MVC框架的实现
现在web开发基本都是MVC的架构了,struts.springMvc 等等.其中一个重要的功能就是将客户发起的请求,分发至我们定义的Action里面的方法之中. 闲暇之余,我也做了一个类似于spri ...
- Spring Boot入门程序-STS
使用Eclipse EE 中的 Spring Tool插件,完成 第一个Spring Boot应用程序的创建. 一.安装Spirng Tool插件 在 Eclipse EE Oxygen版本,安装“S ...
- Html + JS : 点击对应的按钮,进行选择是隐藏还是显示(用户回复功能)
例如: 当我点击按钮1时,点击第一下进行显示This is comment 01,点击第二下隐藏This is comment 01 当我点击按钮2时,点击第一下进行显示This is comment ...
- 计算后缀表达式的过程(C#)
计算后缀表达式的过程是一个很好玩的过程,而且很简单哦!这里呢,有个计算的技巧,就是:遇到数字直接入栈,遇到运算符就计算! 后缀表达式也叫逆波兰表达式,求值过程可以用到栈来辅助存储: 假定待求值的后缀表 ...
- C#中RichTextBox字体不统一(中英文)
this.richTextBox1.Font = new System.Drawing.Font("微软雅黑", 12F);// new System.Drawing.Font(& ...
- cesium 加载kml polygon和mark(贴地形terrain效果)
key code: var options = { camera : viewer.scene.camera, canvas : viewer.scene.canvas, clampToGround: ...