CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理。
先看一下这张图的结构,因为出度都是$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的更多相关文章
- codeforces 702E Analysis of Pathes in Functional Graph 倍增
题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[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 Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- Codeforces 739D - Recover a functional graph(二分图匹配)
Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...
- 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 ...
随机推荐
- RocketMQ源码 — 二、 NameServer
NameServer 作用:Producer和Consumer获取Broker的地址 目的:解耦Broker和Producer.Consumer 原理:使用netty作为通信工具,监听指定端口,如果是 ...
- unity 内置的CG结构解析
一.Cg顶点程序必须在结构中传递顶点数据.几种常用的顶点结构定义在文件UnityCG.cginc中.在大部分情况下仅仅使用它们就够了.结构如下: 1.appdata_base: 包含顶点位置,法线和一 ...
- GourdScan & sqlmapapi
0x01 Windows下配置GourdScan 0x0101 GourdScan项目地址:https://github.com/code-scan/GourdScan PHP环境 + ...
- php学习笔记——基础知识(2)
9.PHP语句 if 语句 - 如果指定条件为真,则执行代码 if...else 语句 - 如果条件为 true,则执行代码:如果条件为 false,则执行另一端代码 if...else if.... ...
- 学习Sass(二)
前面简单介绍了sass的安装,特点和使用方法等,这里记录下sass的基本语法. 1.变量 2.运算 3.嵌套 4.代码复用 5.高级语法 6.自定义函数 一.变量 变量以$开始,像css属性那样赋值, ...
- js格式化时间为JSON格式 ajax提交 后台处理
var effectRow = new Object();if ($('#grd_infos').datagrid('getChanges').length) { var update = $( ...
- 实现自己的JDBC框架
使用JDBC操作数据库时,dao层的增删改查有很多重复的代码,比如下面的 public int getTotal() { Connection conn = null;//通用代码 PreparedS ...
- List list = new ArrayList()
方便以后扩展List是一个接口,而ArrayList 是一个类. ArrayList 继承并实现了List.List list = new ArrayList();这句创建了一个ArrayList的对 ...
- 移动web开发学习笔记一
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0&quo ...
- AJAX应用的五个步骤
1.建立xmlHttpRequest对象 if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); if(xmlHttp.ov ...