E. Analysis of Pathes in Functional Graph
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

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.

Input

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

Output

Print n lines, the pair of integers si, mi in each line.

Examples
Input
7 3
1 2 3 4 3 2 6
6 3 1 4 2 2 3
Output
10 1
8 1
7 1
10 2
8 2
7 1
9 3
Input
4 4
0 1 2 3
0 1 2 3
Output
0 0
4 1
8 2
12 3
Input
5 3
1 2 3 4 0
4 1 2 14 3
Output
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的更多相关文章

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

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

  2. CodeForces 702E Analysis of Pathes in Functional Graph

    倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...

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

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

  4. CF702E Analysis of Pathes in Functional Graph

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

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

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

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

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

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

随机推荐

  1. 32-语言入门-32-Triangular Sums

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=122    描述The nth Triangular number, T(n) = 1 ...

  2. 2.cadence制板流程[原创]

    1.元器件库(原理图库) 2.原理图 3.DRC检查 4.输出网表 5.PCB封装 6.板子边框 7.导入网表 8.设置约束规则 9.布局,布线,铺铜 10.DRC检查,出丝印,钻孔,出广汇

  3. NDK(11)Android.mk编译APK模板

    转自 :  http://hubingforever.blog.163.com/blog/static/1710405792011656434982/ 以下仅是使用Android.mk编译APK程序的 ...

  4. bash/shell 数学计算

    $ echo $((20.0/7)) $ zcalc $ bc <<< 20+5/2 $ bc <<< 'scale=4;20+5/2' $ expr 20 + 5 ...

  5. 如何使页面滚动条移动到指定元素element的位置处?

    如何使页面滚动条移动到指定元素element的位置处? 在用selenium做测试时,会遇到需要操作的元素不在当前可视页面中的情况,如果是手工测试,自然很简单,手动拖拽滚动条到目标元素处即可. 那么, ...

  6. CSS文字控制与文本控制

    一.CSS控制文字属性: 1.font-size:18px;:设置字号(默认是16px=1em),一般在制作网页的时候,先在body里同一设置下字体大小2.color:#093:设置字色3.font- ...

  7. UVa 122 (二叉树的层次遍历) Trees on the level

    题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...

  8. 51nod1476 括号序列的最小代价

    这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...

  9. 如何向hadoop集群定时提交一个jar作业?

    除了使用Hive,Pig来执行一个MapReduce任务,不需要专门的把项目打成jar包,提交执行,或者定时执行,因为Hive,Pig这些开源框架已经,帮我们自动打包上传了. 而有些时候,我们自己手写 ...

  10. 【第七篇】bootstrap的3级菜单样式,支持母版页保留打开状态

    razor视图的,母版页 依旧不多说,直接上代码 <ul class="sidebar-menu"> @for (int i = 0; i < mList.Cou ...