Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
2 seconds
512 megabytes
standard input
standard output
You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1.
Graph is given as the array f0, f1, ..., fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. Besides you are given array with weights of the arcs w0, w1, ..., wn - 1, where wi — the arc weight from i to fi.
The graph from the first sample test.
Also you are given the integer k (the length of the path) and you need to find for each vertex two numbers si and mi, where:
- si — the sum of the weights of all arcs of the path with length equals to k which starts from the vertex i;
- mi — the minimal weight from all arcs on the path with length k which starts from the vertex i.
The length of the path is the number of arcs on this path.
The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 1010). The second line contains the sequence f0, f1, ..., fn - 1 (0 ≤ fi < n) and the third — the sequence w0, w1, ..., wn - 1 (0 ≤ wi ≤ 108).
Print n lines, the pair of integers si, mi in each line.
7 3
1 2 3 4 3 2 6
6 3 1 4 2 2 3
10 1
8 1
7 1
10 2
8 2
7 1
9 3
4 4
0 1 2 3
0 1 2 3
0 0
4 1
8 2
12 3
5 3
1 2 3 4 0
4 1 2 14 3
7 1
17 1
19 2
21 3
8 1 题目链接:http://codeforces.com/contest/702/problem/E
#include<bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(i=a;i<=b;i++)
using namespace std;
ll f[][],sum,w[][],s[][];
int main() { ll i,j,k,x,m,n;
cin>>n>>k;
FOR(i,,n-)
cin>>f[i][];
FOR(i,,n-)
{
cin>>w[i][];
s[i][]=w[i][];
}
FOR(j,,)
FOR(i,,n-)
{
f[i][j]=f[f[i][j-]][j-];
w[i][j]=min(w[i][j-],w[f[i][j-]][j-]);
s[i][j]=s[i][j-]+s[f[i][j-]][j-];
} FOR(i,,n-)
{
m=w[i][];
x=i;
sum=;
FOR(j,,)
{
if(k&1LL<<j)
{
sum+=s[x][j];
m=min(m,w[x][j]);
x=f[x][j];
}
}
cout<<sum<<" "<<m<<endl;
}
return ;
}
Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph的更多相关文章
- codeforces 702E Analysis of Pathes in Functional Graph 倍增
题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...
- CF702E Analysis of Pathes in Functional Graph
倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Educational Codeforces Round 15 A. Maximum Increase
A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- armeabi,armeabi-v7a ,x86 和mips 都是什么?
首先要明白ABI的概念: ABI(Application Binary Interface)实际就是指应用程序基于哪种指令集来进行编译,我们能用到的ABI 也就四种 armeabi,armeabi ...
- html5 getComputedStyle + resize 实现动态宽高度等比响应式页面设计
序:通常我们只能控制div的宽度 而不能控制高度,在响应式页面里 如果要这个div是正方形那么必须的用媒体查询在不同的分辨率下写死宽高度 今天突发奇想研究了个 用百分比来动态控制div的高度让其与宽度 ...
- C#图片切割、图片压缩、缩略图生成
C#图片切割.图片压缩.缩略图生成的实现代码 /// 图片切割函数 /// </summary> /// <param name="sourceFile"&g ...
- java 文件text的写入
日志文件 OutputStream out = new FileOutputStream(f, true); if (list1.size() > 0) { int h1 = 0; for (i ...
- 【Todo】各种排序整理
今天面试别人,问到堆排序.发现自己都记不太清楚了. 堆排序 从小到大排序,要用到的是,最大堆. 过程是最大堆,堆顶的最大的元素,调换到数组最后,依次进行.最后达到从小到大的效果. 归并排序 可以看这个 ...
- Less tips:声明变量之前可以引用变量!
Less中的variable可以在使用之后才被声明,这一特性对于希望覆盖前期声明的(比如bootstrap等第三方library的variable)变量,从而优雅地 使用你希望的效果提供了便利. 比如 ...
- UVa 12265 (单调栈) Selling Land
紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...
- 【第四篇】说说layer的iframe弹窗给里面的标签赋值的问题
说到这一篇,真的是颠覆了我的思维. 官方文档,没有介绍这一部分的操作,大致上提了一下. 我的思路是把页面的数据传过去,在iframe弹窗的页面拿到接收到的数据,然后赋值,但是这样就会有个问题, 怎么传 ...
- Java笔记之数组
1.int flags[] = new int[10];数组中的每个元素初始化为0. Arrays.fill(flags, 0);将数组中每个元素置为0.
- IP碎片原理:攻击和防护
为了加深理解IP协议和一些DoS攻击手段大家有必要看看以下内容,也许对你理解这个概念有所帮助.先来看看IP碎片是如何产生的吧. 一.IP碎片是如何产生的 链路层具有最大传输 ...