CF702E Analysis of Pathes in Functional Graph
倍增练习题。
基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了。
时间复杂度$O(nlogk)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const int Lg = ;
const int inf = 0x3f3f3f3f; int n, to[N][Lg];
ll stp, val[N], sum[N][Lg], minn[N][Lg]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} template <typename T>
inline void chkMin(T &x, T y) {
if(y < x) x = y;
} template <typename T>
inline T min(T x, T y) {
return x > y ? y : x;
} inline void solve(int x) {
ll resSum = 0LL, resMin = inf, tmp = stp;
for(int i = ; i >= ; i--)
if((tmp >> i) & ) {
resSum += sum[x][i];
chkMin(resMin, minn[x][i]);
x = to[x][i];
}
printf("%lld %lld\n", resSum, resMin);
} int main() {
read(n), read(stp);
for(int i = ; i <= n; i++) read(to[i][]), to[i][]++;
for(int i = ; i <= n; i++) read(val[i]); memset(minn, 0x3f, sizeof(minn));
for(int i = ; i <= n; i++)
sum[i][] = minn[i][] = val[i];
for(int j = ; j <= ; j++)
for(int i = ; i <= n; i++)
{
to[i][j] = to[to[i][j - ]][j - ];
minn[i][j] = min(minn[i][j - ], minn[to[i][j - ]][j - ]);
sum[i][j] = sum[i][j - ] + sum[to[i][j - ]][j - ];
} /* for(int i = 1; i <= n; i++)
printf("%lld ", sum[i][1]);
printf("\n"); */ for(int i = ; i <= n; i++) solve(i); return ;
}
CF702E Analysis of Pathes in Functional Graph的更多相关文章
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...
- codeforces 702E Analysis of Pathes in Functional Graph 倍增
题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- Codeforces 739D - Recover a functional graph(二分图匹配)
Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...
- CF上的3道小题(1)
CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...
- Educational Codeforces Round 15 (A - E)
比赛链接:http://codeforces.com/contest/702 A. Maximum Increase A题求连续最长上升自序列. [暴力题] for一遍,前后比较就行了. #inclu ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- 转债---Pregel: A System for Large-Scale Graph Processing(译)
转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/ 作者:Grzegorz Malewicz, Matthew ...
随机推荐
- 剑指offer--3.用两个栈实现队列
快速刷一遍,先捏软柿子 ----------------------------------------------------------------- 时间限制:1秒 空间限制:32768K 热度 ...
- uva10943(隔板法)
很裸的隔板法. 引用一下维基上对隔板法的解释: 现在有10个球,要放进3个盒子里 ●●●●●●●●●● 隔2个板子,把10个球被隔开成3个部份 ●|●|●●●●●●●●.●|●●|●●●●●●●.●| ...
- Phone numbers
Phone number in Berland is a sequence of n digits. Often, to make it easier to memorize the number, ...
- BZOJ5341: [Ctsc2018]暴力写挂
BZOJ5341: [Ctsc2018]暴力写挂 https://lydsy.com/JudgeOnline/problem.php?id=5341 分析: 学习边分治. 感觉边分治在多数情况下都能用 ...
- 消息队列mq总结(重点看,比较了主流消息队列框架)
转自:http://blog.csdn.net/konglongaa/article/details/52208273 http://blog.csdn.net/oMaverick1/article/ ...
- Python利用itchat库向好友或者公众号发消息
首先获得好友或者公众号的UserName 1.获取好友UserName #coding=utf8 import itchat itchat.auto_login(hotReload=True) #想给 ...
- PIX v2版本中Query 失败时, ERR段的构造
在ITI-9中描述PIX query事务的几个TestCase场景.其中有些是对于Query失败的描述. ERR 段包含Error location, Error code, Error code t ...
- 使用PHP的GD2裁剪 + 缩放图片
/** * 裁剪 + 缩放图片 * @param array $params 包含x,y,width,height,path * @return string */ public function t ...
- 判断唯一约束是否是唯一的Unique
//检查 唯一约束Name //检查 唯一约束Name int count = new BLL.Funcs().GetRecordCount(string.Format("Name={0}& ...
- [转载]linux内核中的HZ介绍
时钟中断由系统定时硬件以周期性的间隔产生,这个间隔由内核根据 HZ 值来设定,HZ 是一个体系依赖的值,在 <Linux/param.h>中定义或该文件包含的某个子平台相关文件中.作为通用 ...