【题解】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. "不用谢" 的11种表达

    说Thank you ,机械版的反应you are welcome.虽然没错,但实在太老掉牙,在国外使用率不高: 随性,不足挂齿的小事 Not a problem 别放在心上 Any time 有事随 ...

  2. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  3. [Pytorch]基于混和精度的模型加速

    这篇博客是在pytorch中基于apex使用混合精度加速的一个偏工程的描述,原理层面的解释并不是这篇博客的目的,不过在参考部分提供了非常有价值的资料,可以进一步研究. 一个关键原则:“仅仅在权重更新的 ...

  4. oracle用UNION替换OR (适用于索引列)

    通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描. 注意, 以上规则只针对多个索引列有效. 如果有column没有被索引, 查询效率可能会因为你 ...

  5. vue 组件评论 的同时进行刷新

    注意:1.最重要理解这里的父组件的刷新功能,通过v-on事件绑定委托给子组件执行,因为子组件的提交按钮和父组件的刷新评论的功能是分开的. 2.没有数据时直接点击提交按钮时会出bug,并且关闭后重新加载 ...

  6. Python--day65--Django框架介绍

    原文链接:http://www.cnblogs.com/liwenzhou/p/8296964.html 做web开发用的是MVC框架: Django框架简介 MVC框架和MTV框架(了解即可) MV ...

  7. CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)

    传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...

  8. C# “不支持给定路径的格式”异常处理

    问题背景 无聊研究了一下怎么发送邮件(包含附件),但发现附带的文件路径除了报错就是报错,不知道为什么. 用了不下好几种方式,比如 var x = "E:\\Git\\cmd\\git.exe ...

  9. P1069 约瑟夫问题

    题目描述 约瑟夫问题是一个非常经典的问题. n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人再出圈,--依次类推,直到所有的人都出圈, ...

  10. H3C 子网划分方法