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 ...
随机推荐
- MVC源码分析 - Controller创建和创建扩展
上一篇, 出现了一个至关重要的类:MvcHandler, 接下来就来看一下MvcHandler吧. 先不看具体方法, 先看一下类里面的情况. //这里实现了两个重要的接口, 异步处理和同步处理的接口p ...
- 二、spark入门之spark shell:文本中发现5个最常用的word
scala> val textFile = sc.textFile("/Users/admin/spark-1.5.1-bin-hadoop2.4/README.md") s ...
- 讲解——Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
- Maven配置插件跳过测试代码的编译和运行
Maven配置插件跳过测试代码的编译和运行: <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins< ...
- Android学习笔记(二)Git和Github
一.添加SSH Key ssh-keygen -t rsa -C "email@example.com" 遇到提示只需要一直确认.用户目录(如/root)下会生成一个.ssh文件夹 ...
- python爬虫---python3.5---eclipse
解析中文会出现\xbe\c8\90\hd........ 这个和你的编码选择有关.如果是解析成html,则需 fout = open('output.html', 'w',encoding='utf- ...
- Linux网络管理之net-tools VS iproute2
查看网卡及IP ifconfig ip link [show] --------- ifconfig -a ip addr show 激活和停止网络接口 ifconfig eth0 up/down i ...
- [读书笔记]python3.5实现socket通讯(TCP)
TCP连接: tcp是面向连接的一个协议,意味着,客户端和服务器开发发送数据之前,需要先握手创建一个TCP连接.TCP连接的一端与客户端套接字相互联系,另一端与服务器套接字相联系.当创建该TCP连接的 ...
- unity3d使用litjson中文显示的问题
我们在使用litjson时它的编码方式是unicode的,所以我将json转成string输出时显示的是unicode的编码.这样我们显示或者保存中文时不是很方便.我们可以将中文的unicode转成能 ...
- Win10下Mysql5.7.13,解压版安装流程
一.环境变量配置 1.将下载好的压宿包解压到安装目录,我的安装目录就是:D:\DevelopmentTool\Mysql5.7.13\mysql-5.7.13-winx64 2.鼠标选择计算机右键,点 ...