洛谷-图的遍历-P2661-信息传递
生活随笔
收集整理的這篇文章主要介紹了
洛谷-图的遍历-P2661-信息传递
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 200010;
int n, fa[N], ans = 0x3f3f3f3f;
int get (int x, int &cnt) { //cnt記錄環(huán)的長度 cnt ++;if (fa[x] == x) return x;else return get(fa[x], cnt);
}
int main () {scanf("%d", &n);for (int i = 1; i <= n; i ++)fa[i] = i;for (int i = 1; i <= n; i ++) {int cnt = 0, f;scanf("%d", &f);if (get(f, cnt) == i) {ans = min(ans, cnt); //維護最小的環(huán) }elsefa[i] = f;}printf("%d", ans);return 0;
}
這個解法是真的帥–并查集
overover
總結
以上是生活随笔為你收集整理的洛谷-图的遍历-P2661-信息传递的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux开发5款实用工具推荐
- 下一篇: 快手sig签名和did egid的注册(