倍增预处理。

先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去。

接下来我们需要记录一些值便于询问:

设$t[i][j]$表示从$i$节点出发,走了${2^j}$步之后,到达的节点编号为$t[i][j]$。

设$s[i][j]$表示从$i$节点出发,走了${2^j}$步之后,路径上的权值和为$s[i][j]$。

设$m[i][j]$表示从$i$节点出发,走了${2^j}$步之后,路径上的权值最小值为$m[i][j]$。

像$RMQ$一样,我们可以$dp$预处理出所有的$t[i][j]$,$s[i][j]$,$m[i][j]$。

然后每次询问,只要沿着路从起点找到终点就可以了。单次询问的复杂度为$O(\log n)$,可以理解为$k$的二进制上哪几位是$1$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int n,f[maxn],w[maxn];
LL t[maxn][],s[maxn][],m[maxn][],k;
LL b[maxn]; int main()
{
b[]=; for(int i=;i<=;i++) b[i]=*b[i-];
scanf("%d%lld",&n,&k);
for(int i=;i<n;i++) scanf("%d",&f[i]);
for(int i=;i<n;i++) scanf("%lld",&w[i]);
for(int i=;i<n;i++) m[i][]=s[i][]=w[i], t[i][]=f[i]; for(int i=;i<=;i++)
{
for(int j=;j<n;j++)
{
m[j][i]=min(m[j][i-],m[t[j][i-]][i-]);
s[j][i]=s[j][i-]+s[t[j][i-]][i-];
t[j][i]=t[t[j][i-]][i-];
}
} for(int i=;i<n;i++)
{
LL sum=,Min=w[i]; int tmp=i;
for(int j=;j>=;j--)
{
if(k&((LL)<<(LL)j))
{
int pos=j;
sum=sum+s[tmp][pos];
Min=min(Min,m[tmp][pos]);
tmp=t[tmp][pos];
}
} printf("%lld %lld\n",sum,Min);
}
return ;
}

CodeForces 702E Analysis of Pathes in Functional Graph的更多相关文章

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

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

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

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

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

  4. CF702E Analysis of Pathes in Functional Graph

    倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  6. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  7. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

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

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

  9. Codeforces 841D Leha and another game about graph - 差分

    Leha plays a computer game, where is on each level is given a connected graph with n vertices and m  ...

随机推荐

  1. html - table 表格不被撑开,td某些列宽度固定某些列自适应

    table-layout 属性的解释:http://www.w3school.com.cn/cssref/pr_tab_table-layout.asp 1,实现table细边框,设置如下css: t ...

  2. Java排序方法sort的使用详解

    对数组的排序: //对数组排序 public void arraySort(){ int[] arr = {1,4,6,333,8,2}; Arrays.sort(arr);//使用java.util ...

  3. js如何判断是否在iframe中

    JS代码://方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME") { alert ...

  4. Sipdroid实现SIP(五): 用Java实现的UserAgent

    I. 概述 UserAgent是SIP协议中的一个概念, 将"打电话"功能中的主叫和被叫逻辑上封装成UserAgent, 就像将"注册"功能的发起方和接收方封装 ...

  5. mysql解压版的配置安装

    先在CMD进入编辑筐,用管理员身份运行 切换到mysql的解压目录的bin目录下并输入mysqld -install 这个时候启动服务, 发现出错!!! 检查这两个文件 这里的路径一定要核对 再次启动 ...

  6. Token注解防止表单的重复提交

    注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...

  7. C# CodeHelper

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  8. mybatis学习笔记二(接口注解)

    直接上代码,全部在代码里讲解. 1.实体类 package com.home.entity; /** * 此类是:user实体类 * @author hpc * @2017年1月10日下午9:36:5 ...

  9. springIOC

    从这段代码开始 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Pers ...

  10. VIP站长大会(北京站)常见问题解答

    功能支持问题 1. react能否和MIP结合使用,如果暂时不能以后是否有考虑?是否会和其他 js 框架(比如angular )结合? 目前暂无计划支持. 2. MIP页是否支持自定义cookie?实 ...