【题解】CF894E Ralph and Mushrooms (缩点)

这是紫?给个解方程算法

考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少

\[\sum_{i=0}^x (w-\sum_{j=0}^i j)=\sum_{i=0}^x (w-{i(i+1)\over 2})
\]

那么考虑解出\(x\)的值,根据初中知识解出来\(x=\lfloor{-1+\sqrt{1+8w}\over 2}\rfloor\),预处理\(\sum {i(i+1)\over 2}\)可以直接计算贡献(其实也可以解通项公式)

然后Tarjan缩点之后变成DAG上的DP,同时有点权和边权的DAG DP

时间复杂度\(O(n)\)

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath> using namespace std; typedef long long ll; char __buf[1<<18],*__c=__buf,*__ed=__buf;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(!isdigit(c))f|=c==45,c=getchar();
while(isdigit(c)) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
} const int maxn=1e6+5;
struct E{int to,w;};
vector<E> e[maxn],e2[maxn];
inline void add(const int&fr,const int&to,const int&w){e[fr].push_back({to,w});}
inline void add2(const int&fr,const int&to,const int&w){
e2[fr].push_back({to,w});
}
int n,m;
int qaq[maxn],cnt;
ll w[maxn],dp[maxn];
bool usd[maxn];
int low[maxn],dfn[maxn],in[maxn],stk[maxn],top,siz[maxn];
ll s[maxn]; void dfs(const int&now){
stk[++top]=now; in[now]=1;
low[now]=dfn[now]=++*dfn;
for(auto t:e[now]){
if(!dfn[t.to]) dfs(t.to),low[now]=min(low[now],low[t.to]);
else if(in[t.to]) low[now]=min(dfn[t.to],low[now]);
}
if(low[now]==dfn[now]){
++cnt;
int temp;
do temp=stk[top--],in[temp]=0,qaq[temp]=cnt,++siz[cnt];
while(temp!=now);
}
} ll Dp(const int&now){
if(usd[now]) return dp[now];
dp[now]=0;
for(auto t:e2[now]) dp[now]=max(dp[now],Dp(t.to)+t.w);
usd[now]=1;
return dp[now]=w[now]+dp[now];
} int main(){
n=qr(); m=qr();
for(int t=1,t1,t2,t3;t<=m;++t) t1=qr(),t2=qr(),t3=qr(),add(t1,t2,t3);
for(int t=1;t<maxn;++t) s[t]=s[t-1]+(1ll*t*(t+1)>>1);
int S=qr();
dfs(S);
for(int t=1;t<=n;++t)
for(auto i:e[t]){
if(qaq[t]==qaq[i.to]) {
int k=(sqrt((long double)1+8ll*i.w)-1)/2.0;
w[qaq[t]]=w[qaq[t]]+(k+1ll)*i.w-s[k];
}
else add2(qaq[t],qaq[i.to],i.w);
}
ll ans=Dp(qaq[S]);
printf("%lld\n",ans);
return 0;
}

【题解】CF894E Ralph and Mushrooms (缩点)的更多相关文章

  1. CF894E Ralph and Mushrooms

    题目 一眼题. 缩点然后dp. 注意一下计算一条边经过无限次可以获得多少价值这个东西要用到平方和公式. \(\sum\limits_{i=1}^ni^2=\frac{i(i+1)(2i+1)}6\) ...

  2. Codeforces 894.E Ralph and Mushrooms

    E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...

  3. CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)

    题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0.问最多能在这张图上采多少个蘑菇. 分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑 ...

  4. 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP

    题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...

  5. 「CF894E」 Ralph and Mushrooms

    传送门 Luogu 解题思路 首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的. 那么我们就有了思路:先缩点,再跑拓扑排序. 那么问题就是 \(\text{DP}\) 状态如何初始化. 我 ...

  6. CF894E Ralph and Mushrooms_强连通分量_记忆化搜索_缩点

    Code: #include<cstdio> #include<stack> #include<cstring> using namespace std; cons ...

  7. Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms

    Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n  ...

  8. [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...

  9. Codeforces Round #447 (Div. 2) 题解 【ABCDE】

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

随机推荐

  1. QT 中如何实现一个简单的动画

    QT可以实现一下简单的动画,比如 运动的时钟 闪烁的按钮. 动画的实现: (1)创建一个定时器 (2)调用QWidget::update()通知界面重绘 实现一个按钮闪烁的例子: circlewidg ...

  2. Python基础:12函数细节

    一:返回值 当没有显式地返回元素时,Python 会返回一个None.如果函数返回多个对象,python 把他们聚集起来并以一个元组返回. 二:创建函数 1:强烈推荐,在函数体之前,编写函数的文档字符 ...

  3. 谈一谈Python的上下文管理器

    经常在Python代码中看到with语句,仔细分析下,会发现这个with语句功能好强,可以自动关闭资源.这个在Python中叫上下文管理器Context Manager.那我们要怎么用它,什么时候用它 ...

  4. TensorFlow的 卷积层

    用 TensorFlow 做卷积 让我们用所学知识在 TensorFlow 里构建真的 CNNs.在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias.这在很大程度上 ...

  5. CF1054F Electric Scheme

    CF1054F Electric Scheme  其实没啥的. 离散化后,每行每列选择一个. 但是可能会相交 每行或每列相邻两个点成为一小段. 小段按照行列左右部点 小段有交,连inf边,每个s-左, ...

  6. PHP怎么调用其他类的方法

    2个PHP,这个PHP中的类调用另一个PHP中的类,如何调用.Java中是import ,php中是什么?还是用其他什么方法? 1.引用类:比如类名为product,则:include('...路径/ ...

  7. Android Studio(五):修改Android Studio项目包名

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  8. laravel博客后台操作步骤

  9. EL表达式中的empty和null

    EL表达式中的empty和null 先说一下EL表达式中的null和empty区别,然后再说说最近在项目中出现的一个有趣的问题. EL中的null和empty都可用来判断值是否为空,但两者存在略微的区 ...

  10. Linux环境下第一次提交项目

    Linux环境下第一次提交项目: vi 日记 新增一个文件名为“日记”的文件 git status 工作区的状态 git add 日记 建立跟踪 git commit 提交变更 ----------- ...