题目链接

给一个图, 然后给出每条边的权值和一个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 倍增的更多相关文章

  1. CodeForces 702E Analysis of Pathes in Functional Graph

    倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...

  2. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  3. 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 ...

  4. CF702E Analysis of Pathes in Functional Graph

    倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  6. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  7. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  8. Codeforces 739D - Recover a functional graph(二分图匹配)

    Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...

  9. 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  ...

随机推荐

  1. <转>Java 理论与实践: 正确使用 Volatile 变量

    Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...

  2. U - 神、上帝以及老天爷(第二季水)

    Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了!         为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的:  ...

  3. MYSQL SET类型字段的SQL查询某个字段保函某个值的查询

    1.column set('hot','crazy','smart')  //column字段(set属性)三个值 2.select * from table where FIND_IN_SET('h ...

  4. RemoteViews的内部机制

    1.RemoteViews的构造方法public RemoteViews(String packageName,int layoutId) 第一个表示当前应用的包名(反射机制需要),第二个表示加载的布 ...

  5. 定制化Azure站点Java运行环境(3)

    定制化Azure Website提供的默认的Tomcat和JDK环境 在我们之前的测试中,如果你访问你的WEB站点URL时不加任何上下文,实际上你看到的web界面是系统自带的测试页面index.jsp ...

  6. UIDatePicker控件

    UIDatePicker继承关系如下: UIDatePicker-->UIControl-->UIView-->UIResponder-->NSObject 1.创建UIDat ...

  7. 谈谈我对Java中CallBack的理解

    谈谈我对Java中CallBack的理解 http://www.cnblogs.com/codingmyworld/archive/2011/07/22/2113514.html CallBack是回 ...

  8. linux系统怎么改为中文版(转)

    linux系统安装好后怎么改为中文版呢?今天就跟大家介绍下linux系统改为中文版的方法,希望能帮助到大家! 以下是linux系统改为中文版的四种方法,一起来看看: 方法1:写入环境变量 echo & ...

  9. 轻量级交互数据json格式初探

    [w3cschool tydef]什么是 JSON ?JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)JSON 是轻量级的文本数据交换格式JS ...

  10. cropbox插件实现的头像裁剪效果

    html代码 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...