首页 > 代码库 > codeforces 702E Analysis of Pathes in Functional Graph
codeforces 702E Analysis of Pathes in Functional Graph
直接倍增就好了。。。。。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxv 100500#define inf 0x7f7f7f7f7f7f7f7fLLusing namespace std;long long n,k,anc[maxv][35],mn[maxv][35],w[maxv][35],table[35];int main(){ table[0]=1;for (long long i=1;i<=34;i++) table[i]=table[i-1]*2; scanf("%I64d%I64d",&n,&k); for (long long i=0;i<n;i++) scanf("%I64d",&anc[i][0]); for (long long i=0;i<n;i++) {scanf("%I64d",&mn[i][0]);w[i][0]=mn[i][0];} for (long long e=1;e<=34;e++) for (long long i=0;i<n;i++) { anc[i][e]=anc[anc[i][e-1]][e-1]; mn[i][e]=min(mn[i][e-1],mn[anc[i][e-1]][e-1]); w[i][e]=w[i][e-1]+w[anc[i][e-1]][e-1]; } for (long long i=0;i<n;i++) { long long ret1=0,ret2=inf,ret3=k,now=i; for (long long e=34;e>=0;e--) { if (ret3>=table[e]) { ret3-=table[e]; ret1+=w[now][e];ret2=min(ret2,mn[now][e]); now=anc[now][e]; } } printf("%I64d %I64d\n",ret1,ret2); } return 0;}
codeforces 702E Analysis of Pathes in Functional Graph
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。