显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
const int inf=0x3f3f3f3f;
typedef long long ll;
ll n,k;
ll f[N][40],a[N][40],s[N][40];
int main(){
freopen("pathsjump.in","r",stdin);
freopen("pathsjump.out","w",stdout);
std::cin>>n>>k;
for(int i=1;i<=n;i++){
std::cin>>f[i][0];
}
for(int i=1;i<=n;i++){
std::cin>>a[i][0];
s[i][0]=a[i][0];
}
for(int j=1;j<=35;j++){
for(int i=1;i<=n;i++){
ll p=f[i][j-1];
f[i][j]=f[p][j-1];
a[i][j]=min(a[i][j-1],a[p][j-1]);
s[i][j]=s[i][j-1]+s[p][j-1];
}
}
for(int i=1;i<=n;i++){
ll kp=k,minn=1e9,ans=0,x=i;
for(int j=35;j>=0;j--){
if((1ll<<j)<=kp){
minn=min(minn,a[x][j]);
ans+=s[x][j];
kp-=(1ll<<j);
x=f[x][j];
}
}
std::cout<<ans<<" "<<minn<<endl;
}
return 0;
}