Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增法)

本文介绍了一种路径分析算法,用于解决给定有向图中长度为k的路径的权重总和及最小边的问题。通过预处理节点间的跳转关系,利用二进制表示快速求解任意起点的路径特征。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/702/E

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

给你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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值