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 ...
随机推荐
- linux下,如何把整个文件夹上传到服务器(另一台linux)
1.Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 #test1为源目录, ...
- jQuery选择器最佳实践--来自jQ官网
1.基于ID选择器进行查询,并且使用find方法. //快速 $("#container div.footer"); //超快 $("#container"). ...
- Fucking "pkg-config not found"
If you got pkg-config not found error in gnu auto tools then, you must install the related librarys ...
- 一个简单json数据提交实例
1.客户端编程:jsp页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- JAVA的核心概念:接口(interface)
JAVA的核心概念:接口(interface) 接口与类属于同一层次,实际上,接口是一种特殊的抽象类. 如: interface IA{ } public interface: 公开接口 与 ...
- struct TABLE
struct TABLE { TABLE() {} /* Remove gcc warning */ TABLE_SHARE *s; handler *file; TABLE *next, *prev ...
- AssetManager asset的使用
Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里./res 和/assets的不同点是,android不为/assets下的文件生成ID.如果使用/a ...
- 基于Live555,ffmpeg的RTSP播放器直播与点播
基于Live555,ffmpeg的RTSP播放器直播与点播 多路RTSP高清视频播放器下载地址:http://download.csdn.net/detail/u011352914/6604437多路 ...
- ubuntu12.04上搭建darwin streaming server6.03
个人建议:使用DarwinStreamingSrvr5.5.5,因为DarwinStreamingSrvr6.0.3安装过程中有很多问题需要解决!而且安装只需执行./Install就可以! 1:下载d ...
- 【转】Android Studio系列教程一--下载与安装
原文网址:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...