本文主要是介绍A Bug‘s Life (并查集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//新生训练
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5000;
int p[N], sz[N];
int n, m;
int find(int x)
{if (p[x] != x)p[x] = find(p[x]);return p[x];
}
int main()
{int T;scanf("%d", &T);for (int k = 1; k <= T; k++){printf("Scenario #%d: ", k);scanf("%d%d", &n, &m);for (int i = 1; i <= 2 * n; i++){p[i] = i;}int flag = 0;for (int i = 1; i <= m; i++){int a, b;scanf("%d%d", &a, &b);int x1 = find(a), y1 = find(b);int x2 = find(a + n), y2 = find(b + n);if (x1 == y1)flag = 1;p[y2] = x1;p[x2] = y1;}if (flag)printf("Suspicious bugs found!\n\n");else printf("No suspicious bugs found!\n\n");}return 0;
}
~~~//仅当笔者个人备忘录使用。
这篇关于A Bug‘s Life (并查集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!