codeforces 702E Analysis of Pathes in Functional Graph 倍增
给一个图, 然后给出每条边的权值和一个k值。 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值。
用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四次..八次。 这个很容易计算。然后枚举一下所有点就可以了。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int nxt[][], mn[][];
ll sum[][];
int main()
{
int n;
ll k;
cin>>n>>k;
for(int i = ; i < n; i++) {
scanf("%d", &nxt[i][]);
}
for(int i = ; i < n; i++) {
scanf("%d", &mn[i][]);
sum[i][] = mn[i][];
}
for(int i = ; i < ; i++) {
for(int j = ; j < n; j++) {
nxt[j][i] = nxt[nxt[j][i-]][i-];
mn[j][i] = min(mn[j][i-], mn[nxt[j][i-]][i-]);
sum[j][i] = sum[j][i-] + sum[nxt[j][i-]][i-];
}
}
for(int j = ; j < n; j++) {
ll ansSum = , tmpk = k;
int ansMin = inf, pos = j;
for(int i = ; i >= ; i--) {
ll tmp = 1LL<<i;
if(tmpk >= tmp) {
tmpk -= tmp;
ansSum += sum[pos][i];
ansMin = min(ansMin, mn[pos][i]);
pos = nxt[pos][i];
}
}
printf("%I64d %d\n", ansSum, ansMin);
}
return ;
}
codeforces 702E Analysis of Pathes in Functional Graph 倍增的更多相关文章
- CodeForces 702E Analysis of Pathes in Functional Graph
倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...
- 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 ...
- CF702E Analysis of Pathes in Functional Graph
倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- Codeforces 739D - Recover a functional graph(二分图匹配)
Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...
- Codeforces 841D Leha and another game about graph - 差分
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m ...
随机推荐
- <转>Java 理论与实践: 正确使用 Volatile 变量
Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...
- [转载]提升进程权限-OpenProcessToken等函数的用法
GetCurrentProcessID 得到当前进程的ID OpenProcessToken 得到进程的令牌句柄LookupPrivilegeValue 查询进程的权限 AdjustTokenPriv ...
- PPT 制作必备工具
1.图标 http://www.easyicon.net/ http://ico.58pic.com/ http://www.iconpng.com/ 2.字体 http://www.qiuziti. ...
- JMS & ActiveMQ小结
JMS简介 JMS源于企业应用对于消息中间件的需求,使应用程序可以借助消息进行异步处理而互不影响.Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能 ...
- nginx---Beginner's Guide
一 启动 nginx -s signal Where signal may be one of the following: stop — fast shutdown quit — graceful ...
- mysql各种日志对应的配置项
01.error_log --log-error=<file_name> 02.general_log --general-log-file=<file_name> --gen ...
- jquery 中 (function( window, undefined ) {})(window)写法详解(转)
最常见的闭包 (Closure) 范式大家都很熟悉了: 123 (function() {// ...})(); 很简单,大家都在用.但是,我们需要了解更多.首先,闭包是一个匿名函数 (Anonymo ...
- 数据结构--队列之C数组实现
队列是一种限定操作的线性表,它只能在表的一段插入,另外一段取出.所以也称为先进先出数据结构(FIFO---First In First Out) C代码如下: #include<stdio.h& ...
- yield语句
自C#的第一个版本以来,使用foreach语句可以轻松地迭代集合.在C#1.0中,创建枚举器仍需要做大量的工作.C#2.0添加了yield语句,以便于创建枚举器.yield return语句返 ...