题目链接:http://codeforces.com/problemset/problem/702/E
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.

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
给你n个点,n条有向边,问你从每个点出发边个数为k的路径权值与最小边大小是多少.
类似RMQ做法,因为是单向n条边,所以路径唯一且没有终点.
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef __int64 LL;
typedef pair <int, int> P;
const int N = 1e5 + 5;
int par[N][35];
LL min_val[N][35];
LL sum[N][35];
void init(int n) {
for(int k = 1; k < 34; ++k) {
for(int i = 0; i < n; ++i) {
par[i][k] = par[par[i][k - 1]][k - 1];
min_val[i][k] = min(min_val[i][k - 1], min_val[par[i][k - 1]][k - 1]);
sum[i][k] = sum[i][k - 1] + sum[par[i][k - 1]][k - 1];
}
}
}
void solve(int root, LL x) {
LL res_min = 1e8 + 1, res_sum = 0;
for(int k = 0; k < 34; ++k) {
if(x & (1LL << k)) {
res_sum += sum[root][k];
res_min = min(res_min, min_val[root][k]);
root = par[root][k];
}
}
printf("%lld %lld\n", res_sum, res_min);
}
int main()
{
int n;
LL m;
scanf("%d %lld", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%d", &par[i][0]);
for(int i = 0; i < n; ++i) {
scanf("%lld", &min_val[i][0]);
sum[i][0] = min_val[i][0];
}
init(n);
for(int i = 0; i < n; ++i) {
solve(i, m);
}
return 0;
}