倍增练习题。

基环树上倍增一下维护维护最小值和权值和,注意循环的时候$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的更多相关文章

  1. 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 ...

  2. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  3. codeforces 702E Analysis of Pathes in Functional Graph 倍增

    题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...

  4. CodeForces 702E Analysis of Pathes in Functional Graph

    倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...

  5. Codeforces 739D - Recover a functional graph(二分图匹配)

    Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...

  6. CF上的3道小题(1)

    CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...

  7. Educational Codeforces Round 15 (A - E)

    比赛链接:http://codeforces.com/contest/702 A. Maximum Increase A题求连续最长上升自序列. [暴力题] for一遍,前后比较就行了. #inclu ...

  8. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  9. 转债---Pregel: A System for Large-Scale Graph Processing(译)

    转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/   作者:Grzegorz Malewicz, Matthew ...

随机推荐

  1. C++结构体成员列表初始化

    C++关于struct和class的区别,可以看上一篇文章:c ++ class和struct[转] 结构体成员列表初始化,来个例子: #include <iostream> #inclu ...

  2. python究竟要不要使用多线程

    在总结concurrent.futures库之前先来弄明白三个问题: (1)python多线程究竟有没有用? (2)python虚拟机机制如何控制代码的执行? (3)python中多进程处理原理是怎么 ...

  3. Linux下anaconda的安装

    http://peteryuan.net/use-anaconda/ http://www.jianshu.com/p/03d757283339

  4. 「LOJ#10051」「一本通 2.3 例 3」Nikitosh 和异或(Trie

    题目描述 原题来自:CODECHEF September Challenge 2015 REBXOR 1​​≤r​1​​<l​2​​≤r​2​​≤N,x⨁yx\bigoplus yx⨁y 表示 ...

  5. 检测硬件的批处理命令,检测硬件bat,一键获取电脑硬件信息

    警告:运行BAT源码是一种危险的动作,如果你不熟悉,请不要尝试! 批处理语言: 简体中文 授权方式: 免费软件 运行环境: Windows平台 检测硬件批处理命令.一键获取.直接双击就可以查看 @ec ...

  6. 【转】深入剖析Java中的装箱和拆箱

    深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...

  7. 释放Windows C盘空间 -- 虚拟内存和休眠文件

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4111718.html ,转载请保留此声明. 项目组有一Windows工作站, 由于需要使用Kinect最 ...

  8. python之 python 起源、语言特点

    一. 1.1  什么是 PythonPython 是一门优雅而健壮的编程语言,它继承了传统编译语言的强大性和通用性,同时也借鉴了简单脚本和解释语言的易用性.它可以帮你完成工作,而且一段时间以后,你还能 ...

  9. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  10. ECMAScript 2016(ES7) 知多少

    ECMAScript 2016(ES7) 知多少 1. 数组方法 Array.prototype.includes(value : any) : boolean 2. 幂运算符 x ** y 扩展阅读 ...