codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702
题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值
思路:

f[i][j] 第i个点出发,经过2^j条边后的相连点 其余类似
二进制巧妙解决问题应用太广了
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define pi acos(-1.0)
const int N = 1e5+;
const int MOD = 1e9+;
#define inf 0x7fffffff
typedef long long LL; void frein(){freopen("in.txt","r",stdin);}
void freout(){freopen("out.txt","w",stdout);}
inline LL read(){LL x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}while(ch>=''&&ch<='') { x=x*+ch-'';ch=getchar();}return x*f;}
const int M =; int f[N][M];
LL sum[N][M];
int mn[N][M];
int main(){
int n;
LL k;
n=read(),k=read();
for(int i=;i<n;i++) scanf("%d",&f[i][]);
for(int i=;i<n;i++) {scanf("%d",&sum[i][]);mn[i][]=sum[i][];}
for(int j=;j<M;j++){
for(int i=;i<n;i++){
f[i][j]=f[f[i][j-]][j-];
sum[i][j]=sum[f[i][j-]][j-]+sum[i][j-];
mn[i][j]=min(mn[i][j-],mn[f[i][j-]][j-]);
}
}
LL ans;
for(int i=;i<n;i++){
int v=i,minn=inf;
LL K=k;
ans=;
for(int j=M-;j>=;j--){
if((1LL<<j)<=K){
ans+=sum[v][j];
minn=min(minn,mn[v][j]);
v=f[v][j];
K-=(1LL<<j);
}
}
printf("%I64d %d\n",ans,minn);
}
return ;
}
codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制的更多相关文章
- codeforces 702E Analysis of Pathes in Functional Graph 倍增
题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- 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 ...
- CF702E Analysis of Pathes in Functional Graph
倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...
- Codeforces 739D - Recover a functional graph(二分图匹配)
Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...
- Educational Codeforces Round 15 (A - E)
比赛链接:http://codeforces.com/contest/702 A. Maximum Increase A题求连续最长上升自序列. [暴力题] for一遍,前后比较就行了. #inclu ...
- CF上的3道小题(1)
CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...
- 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 ...
随机推荐
- React如何性能调优
一. 二.调优例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...
- lua的split函数
function split(s, delim) then return end local t = {} while true do local pos = string.find (s, deli ...
- UNIX 高手的 20 个习惯[转]
使用 mkdir 的 -p 选项并在单个命令中创建所有父目录及其子目录要容易得多.但是即使对于知道此选项的管理员,他们在命令行上创建子目录时也仍然束缚于逐步创建每级子目录.花时间有意识地养成这个好习惯 ...
- UVa 10256 - The Great Divide 判断凸包相交
模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...
- 传感器(2)常用api简介及列出当前设备支持的传感器代码
Android SDK提供了Android sensor framework,可以用来访问当前Android设备内置的传感器. ASF提供了很多类和接口,可以帮助我们完成各种与传感器有关的任务. 例如 ...
- Sublime Text汉化方法和注册码
汉化方法 安装 SublimeText3 汉化包运行SublimeText3 点击 Preferneces -> Browse Packages 会打开 X:\..\Sublime Text 3 ...
- Sass结合Modernizr的使用方法
Modernizr在初始化的时候会首先找寻class=“no-js”的元素: <!DOCTYPE html> <html class="no-js"> &l ...
- 定义 androidlistview 滚动条位置
1.找到每一页的最后一条数据的位置 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, ...
- hdu 3535 AreYouBusy
// 混合背包// xiaoA想尽量多花时间做ACM,但老板要求他在T时间内做完n堆工作,每个工作耗时ac[i][j],// 幸福感ag[i][j],每堆工作有m[i]个工作,每堆工作都有一个性质,/ ...
- geusture for chrome cfg
{ "name": "Chrome Gestures", "version": "1.13.4", "norm ...