社区讨论
裸的广搜竟然能拿70分?
P2661[NOIP 2015 提高组] 信息传递参与者 4已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lo15p0ma
- 此快照首次捕获于
- 2023/10/22 15:37 2 年前
- 此快照最后确认于
- 2023/11/02 15:11 2 年前
提交记录:70 pts
CPP#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 200005
int n, tmp, ans = 1e9, t[maxn], vis[maxn];
int bfs(int x) {
int step = 1;
queue<int> Q;
Q.push(t[x]);
while(!Q.empty()) {
if(step > ans)
return ans;
int cur = Q.front();
Q.pop();
// printf("now=%d,nxt=%d,step=%d\n", cur, t[cur], step);
if(!vis[t[cur]]) {
vis[t[cur]] = 1;
Q.push(t[cur]);
}
if(t[cur] == x)
break;
step ++;
}
return step + 1;
}
int main() {
cin >> n;
for(int i = 1; i <= n; i ++) {
cin >> tmp;
t[i] = tmp;
}
for(int i = 1; i <= n; i ++) {
// cout << endl;
ans = min(ans, bfs(i));
memset(vis, 0, sizeof(vis));
}
cout << ans;
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...