有点意思
原题链接:36889.循环空间2025-01-17 17:25:23
发布于:福建
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
struct node{
int loc,code;
}cir[N];
int n,tot,g[N];
vector<int>form[N];
void bfs(int s){
int len=0;
cir[s].code=++tot;
queue<int>q;
q.push(s);
while(!q.empty()){
int t=q.front();
q.pop();
form[tot].push_back(t);
cir[t].loc=len++;
if(cir[g[t]].code==0){
cir[g[t]].code=tot;
q.push(g[t]);
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>g[i];
}
for(int i=1;i<=n;i++){
if(!cir[i].code){
bfs(i);
}
}
for(int i=1;i<=n;i++){
int k,cun=cir[i].code;
cin>>k;
cout<<form[cun][(k+cir[i].loc)%form[cun].size()]<<" "<<form[cun].size()<<endl;
}
return 0;
}
全部评论 5
其实就算所有点在一个环里也不会炸,cir[i].code等价于vis[i],还是线性的复杂度
2025-01-19 来自 福建
2对 你这个设计的很好
2025-01-19 来自 湖南
1是这样的
2025-01-19 来自 福建
0
征求倍增题解
2025-01-23 来自 福建
1题解O(N),倍增O(NlogN)
2025-01-19 来自 福建
1而且这也不是倍增啊
2025-01-17 来自 湖南
0用倍增的话难度直接爆增
2025-01-17 来自 湖南
0
本来没意思的 但是看到神秘小数字立马就起立了(
2025-01-17 来自 湖南
0
有帮助,赞一个