显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,k,cnt,tot,mi;
struct made{
int ed,ver;
}e[N];
int a[N],fa[N],ans[N],ss,su,d[N];
int v[N],kk;
void work(int x,int end){
if(x == end)return;
d[tot]++;
ss += e[x].ed;
fa[x] = tot;
work(e[x].ver,end);
}
void sou(int x,int s){
if(v[x]){
if(v[x] == s){
ss = e[x].ed;
tot++;
d[tot] = 1;
fa[x] = tot;
work(e[x].ver,x);
ans[tot] = ss;
}
return;
}
v[x] = cnt;
sou(e[x].ver,s);
}
void work_(int x,int end){
if(x == end)return;
mi = min(e[x].ed,mi);
work_(e[x].ver,end);
}
void dfs(int x){
if(fa[x]){
su += ans[fa[x]] * int(kk / d[fa[x]]);
if(kk >= d[fa[x]]){
mi = min(e[x].ed,mi);
work_(e[x].ver,x);
}
kk -= (kk / d[fa[x]]) * d[fa[x]];
}
if(kk == 0)return;
mi = min(mi,e[x].ed);
su += e[x].ed;
kk--;
dfs(e[x].ver);
}
int main() {
freopen("pathsjump.in","r",stdin);
freopen("pathsjump.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i = 1;i <= n;i++)scanf("%d",&a[i]);
for(int i = 1;i <= n;i++){
int x;
scanf("%d",&x);
e[i].ver = a[i],e[i].ed = x;
}
for(int i = 1;i <= n;i++){
if(!v[i]){
cnt++;
sou(i,cnt);
}
}
// for(int i = 1;i <= n;i++)printf("%d ",d[i]);
// printf("\n");
for(int i = 1;i <= n;i++){
kk = k,su = 0,mi = INT_MAX;
dfs(i);
printf("%d %d\n",su,mi);
}
return 0;
}