Educational Codeforces Round 15 E 树上倍增 RMQ思想
来源:互联网 发布:苹果mac偏好设置在哪 编辑:程序博客网 时间:2024/06/05 03:13
链接:戳这里
E. Analysis of Pathes in Functional Graph
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
题意:
n个点n条边的图,每个节点都连向一个点,可以是自己。
问从该点到长度为k的路径上。输出路径权值总和以及路径上的最小值
思路:
学习了一下树上倍增。其实理解了RMQ写这个还很快的。
fa[j][i]=fa[fa[j][i-1]][i-1] 这个很关键啊 表示以j节点开始到长度为i的路径上 所到的节点
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double lb;#define INF (1ll<<60)-1#define Max 1e9using namespace std;int n;int fa[100100][40];ll s[100100][40],m[100100][40],k;int main(){ scanf("%d%I64d",&n,&k); for(int i=0;i<n;i++) scanf("%d",&fa[i][0]); for(int i=0;i<n;i++){ scanf("%I64d",&m[i][0]); s[i][0]=m[i][0]; } for(int i=1;(1LL<<i)<=k;i++){ for(int j=0;j<n;j++){ fa[j][i]=fa[fa[j][i-1]][i-1]; m[j][i]=min(m[j][i-1],m[fa[j][i-1]][i-1]); s[j][i]=s[j][i-1]+s[fa[j][i-1]][i-1]; } } for(int i=0;i<n;i++){ ll mn=INF,sum=0; int x=i; for(int j=0;(1LL<<j)<=k;j++){ if((1LL<<j)&k){ mn=min(mn,m[x][j]); sum+=s[x][j]; x=fa[x][j]; } } printf("%I64d %I64d\n",sum,mn); } return 0;}
0 0
- Educational Codeforces Round 15 E 树上倍增 RMQ思想
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增RMQ)
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge MST+树上路径倍增
- Codeforces Round #294 (Div. 2) E 树上倍增lca
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增法)
- Educational Codeforces Round 15 E. Analysis of Pathes in Functio(倍增)★ ★
- Educational Codeforces Round 15 E Analysis of Pathes in Functional Graph(倍增)
- Educational Codeforces Round 21E
- Educational Codeforces Round 26 E
- Educational Codeforces Round 903E
- Codeforces 702 (Educational Codeforces Round 15) A~E
- Educational Codeforces Round 15
- Educational Codeforces Round 15
- Educational Codeforces Round 15
- Educational Codeforces Round 15
- Educational Codeforces Round 15
- Educational Codeforces Round 15
- Educational Codeforces Round 12 E. Beautiful Subarrays
- Mac下在zsh中配置adb命令
- hdu5222Exploration
- 一个应用设置两个icon
- POJ 1611 The Suspects【路径压缩】
- qt tcp长连接 粘包
- Educational Codeforces Round 15 E 树上倍增 RMQ思想
- JDBC连接mysql数据库简单案例
- Qt QPainter中获取字体的像素高度和宽度
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 关于Android开发时天天模拟器的使用
- NOIP2011提高组 聪明的质检员(重庆一中高2018级信息学竞赛测验6) 解题报告
- iOS开发之高级转场动画,tableview到collectionView自定义转场动画+手势驱动
- Android Volley完全解析(一),初识Volley的基本用法
- Android 实现图片、视频即时更新到相册