【题解】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. poj 1263 Reflections (Simple Geometry)

    1263 -- Reflections 简单计算几何.题目给出射线以及若干个不相交的圆,求出射线会在哪些圆上反弹,依次写出反弹球的编号. 代码如下: #include <cstdio> # ...

  2. ccf-201703-4-地铁修建

    二分+并查集 发现了一个很奇怪的问题,二分的上界设置错了,结果是运行错误...只有95分.后来改了上界就100了,百思不得其解 #include<cstdio> #include<c ...

  3. vue4——把输入框的内容添加到页面(简单留言板)

    文章地址:https://www.cnblogs.com/sandraryan/ vue最最最简单的demo(记得引入) 实例化一个vue,绑定#app的元素,要渲染的数组arr作为data. 把ar ...

  4. 2、Dapper的使用

    1.表结构介绍: 1)课程表 2)成绩表 3)学生表  2.获取数据库连接的工厂类 需要添加System.Configuration和MySql.Data.MySqlClient引用 namespac ...

  5. H3C 路由协议与可路由协议

  6. hihocoder 1272 买零食

    #1272 : 买零食 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho很喜欢在课间去小卖部买零食.然而不幸的是,这个学期他又有在一教的课,而一教的小卖部姐姐以冷若冰 ...

  7. FtpService [windows] 配置

    ---恢复内容开始--- 1. 启动FtpService 2. 增加ftp用户 1) 复制ftpd.bat,重命名为adduser.bat 2) 把set MAIN_CLASS=org.apache. ...

  8. Redis:WRONGTYPE Operation against a key holding the wrong kind of value

    相关连接:通过Canal保证某网站的Redis与MySql的数据自动同步 1.错误信息 redis.clients.jedis.exceptions.JedisDataException: WRONG ...

  9. H3C查看系统启动配置文件

  10. vue-learning:4-template-v-if-and-v-show

    控制元素可见性的指令 v-if 和 v-show v-if v-else v-else-if :多重判断 template :分组渲染包裹元素 key:管理可复用元素 v-show v-if与v-sh ...