Codeforces Round #455 (Div. 2) E. Coprocessor DAG图上dp

E. Coprocessor

题意:n 个任务,每个任务在 主 / 副 处理器上执行。每个任务可能依赖于其它的一些任务,副处理器每次可以处理多个任务。但如果一个任务要在副处理器上执行,那它所依赖的任务要么已执行完了,要么和它一起在这个副处理器上同时执行。问副处理器最少调用多少次。

直白一点讲,就是给出一个 DAG 图,n 个点, m 条边,每个点的权值为 0 或1 。操作:直接相互连通的权值为 1 的点可以一次处理掉。 问最少操作多少次。

tags:因为是DAG 图,直接跑 dp

dp[i] 表示第 i 个点最少的操作次数,设 i 可以到 to 点,则 dp[i] = min(dp[i],  dp[to]+(a[i]==0 && dp[i]==1)) 。

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 100005;

int n, m, a[N], dp[N], in[N], ans;
vector< int > G[N];
bool  vis[N];
void solve(int u)
{
    if(G[u].size()==0 && a[u] && !vis[u]) ++dp[u];
    vis[u] = true;
    for(auto to : G[u])
    {
        if(!vis[to]) solve(to);
        dp[u] = max(dp[u], dp[to]+(a[u] && a[to]==0));
    }
    ans = max(ans, dp[u]);
}
int main()
{
    scanf("%d%d", &n, &m);
    rep(i,0,n-1) scanf("%d", &a[i]);
    int u, v;
    rep(i,1,m)
    {
        scanf("%d%d", &u, &v);
        G[u].PB(v);
        ++in[v];
    }
    rep(i,0,n-1)
        if(in[i]==0) solve(i);
    printf("%d\n", ans);

    return 0;
}

原文地址:https://www.cnblogs.com/sbfhy/p/8178222.html

时间: 2024-08-24 11:14:19

Codeforces Round #455 (Div. 2) E. Coprocessor DAG图上dp的相关文章

Codeforces Round #455 (Div. 2) C. Python Indentation dp递推

Codeforces Round #455 (Div. 2) C. Python Indentation 题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句.按 python 缩进的方式组合成合法的程序,问有多少种可能方案. tags: dp dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移: 1] 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j] = dp[i][j]. 2]如果

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #433 (Div. 1) D. Michael and Charging Stations(dp)

题目链接:Codeforces Round #433 (Div. 1) D. Michael and Charging Stations 题意: 一个人每天要加油,1种为1000,1种为2000,如果付全额,会得到10%的回扣放在卡上. 如果卡上有剩余的回扣,可以拿来抵现金.问n天最少需要花多少钱. 题解: 很直观的一个dp就是考虑dp[i][j],表示第i天卡上剩余回扣为j的最小花费. 将所有的数除以100后,j其实是小于40的,严格的说是小于30,官方题解有个证明. 因为卡上不可能积累很多的

Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形DP)

题目链接:Codeforces Round #419 (Div. 2) E. Karen and Supermarket 题意: 有n件物品,每个物品有一个价格,和一个使用优惠券的价格,不过这个优惠券有一个限制,必须要在第x个使用后才可以使用.现在有m的钱,问最多能买多少个物品. 题解: 每个优惠券都只与一个券有关,所以根据这个关系就可以构成一棵树. 考虑树形dp,dp[i][j][k(0|1)]表示第i个节点所构成的子树中买了j个物品,使用优惠券和不使用优惠券的最少钱. 转移方程看代码详细解释

Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)

D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one fro

Codeforces Round #396 (Div. 2)C. Mahmoud and a Message(dp)

题目链接:http://codeforces.com/problemset/problem/766/C 题意:给你一组小写字母组成的字符串,和26个数字,分别对应26位小写字母能够存在的字符串的最大长度. 要求:①:求出最多的划分方案 ②:求出分配方案中,最长的子串长度 ③:求出分配方案中,最少分成的子串数 思路:关于①:设置dp[i],dp[i]表示在第i个字符后面有一个横杠的方案数,从第i个往前枚举前一个横杠的位置j. 举个例子,有子串abc,当横杠断在a处,有一种方案,dp[1]=1:当横

Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 题解: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. 代码: 31 int n; 32 int x[MAXN]; 33 VI G[MAXN]; 34 ll dp[MAX

Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f

Codeforces Round #293 (Div. 2) D. Ilya and Escalator (概率DP)

dp[i][j]表示第i秒电梯进去的人数为j时的概率.由于概率比较好求,而且这里的样本是有限个.所以可以先求出概率,然后用公式转化成期望. #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <