CF702E Analysis of Pathes in Functional Graph
倍增练习题。
基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了。
时间复杂度$O(nlogk)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const int Lg = ;
const int inf = 0x3f3f3f3f; int n, to[N][Lg];
ll stp, val[N], sum[N][Lg], minn[N][Lg]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} template <typename T>
inline void chkMin(T &x, T y) {
if(y < x) x = y;
} template <typename T>
inline T min(T x, T y) {
return x > y ? y : x;
} inline void solve(int x) {
ll resSum = 0LL, resMin = inf, tmp = stp;
for(int i = ; i >= ; i--)
if((tmp >> i) & ) {
resSum += sum[x][i];
chkMin(resMin, minn[x][i]);
x = to[x][i];
}
printf("%lld %lld\n", resSum, resMin);
} int main() {
read(n), read(stp);
for(int i = ; i <= n; i++) read(to[i][]), to[i][]++;
for(int i = ; i <= n; i++) read(val[i]); memset(minn, 0x3f, sizeof(minn));
for(int i = ; i <= n; i++)
sum[i][] = minn[i][] = val[i];
for(int j = ; j <= ; j++)
for(int i = ; i <= n; i++)
{
to[i][j] = to[to[i][j - ]][j - ];
minn[i][j] = min(minn[i][j - ], minn[to[i][j - ]][j - ]);
sum[i][j] = sum[i][j - ] + sum[to[i][j - ]][j - ];
} /* for(int i = 1; i <= n; i++)
printf("%lld ", sum[i][1]);
printf("\n"); */ for(int i = ; i <= n; i++) solve(i); return ;
}
CF702E Analysis of Pathes in Functional Graph的更多相关文章
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...
- codeforces 702E Analysis of Pathes in Functional Graph 倍增
题目链接 给一个图, 然后给出每条边的权值和一个k值. 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值. 用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四 ...
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- Codeforces 739D - Recover a functional graph(二分图匹配)
Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...
- CF上的3道小题(1)
CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...
- Educational Codeforces Round 15 (A - E)
比赛链接:http://codeforces.com/contest/702 A. Maximum Increase A题求连续最长上升自序列. [暴力题] for一遍,前后比较就行了. #inclu ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- 转债---Pregel: A System for Large-Scale Graph Processing(译)
转载:http://duanple.blog.163.com/blog/static/70971767201281610126277/ 作者:Grzegorz Malewicz, Matthew ...
随机推荐
- CodeForces-831A-Unimodal Array (水题)
题目链接 /* Name: Copyright: Author: Date: 2018/5/6 19:34:23 Description: */ #include <iostream> # ...
- Leetcode 904. Fruit Into Baskets
sliding window(滑动窗口)算法 class Solution(object): def totalFruit(self, tree): """ :type ...
- THUWC2019 滚粗记
因为真的滚粗了,所以咕咕咕 膜清华爷 OYJason
- B+树和LSM存储引擎代表树和B-树
B+树和LSM比较 https://blog.csdn.net/u013928917/article/details/75912045 在关系型数据库mysql中普遍使用B+树作为索引,在实际中 ...
- IntelliJ IDEA 代码检查规范QAPlug
转自:http://blog.csdn.net/jizi7618937/article/details/51500725 Avoid Array Loops 数组之间的拷贝使用System.array ...
- LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...
- Yii 查询 搜索
一.视图 <div class="form-horizontal"> <?php $form = $this->beginWidget('CActiveFo ...
- 隐藏select中的“请选择”项
<select> <option value="" style="display: none">请选择</option> & ...
- jQuery.extend()方法
定义和用法 jQuery.extend()函数用于将一个或多个对象的内容合并到目标对象. 注意: 1. 如果只为$.extend()指定了一个参数,则意味着参数target被省略.此时,target就 ...
- AFN 请求数据https
第一步: 导入afn库 第二步: 在pch中添加 #import <SystemConfiguration/SystemConfiguration.h> #import <Mobil ...