POJ2492 A Bug's Life

来源:互联网 发布:跳转到一个页面的js 编辑:程序博客网 时间:2024/04/28 20:49
/*并查集*/#include <iostream>#include <cstdio>using namespace std;const int MAXN = 2005;int father[MAXN], Rank[MAXN];int cas = 0, t;int n, m;bool flag;void init(){for (int i = 0; i < n; ++i) {father[i] = i;Rank[i] = 0;}return;}int getfather(int x){if (x == father[x]) return x;int tmp;tmp = getfather(father[x]);Rank[x] = (Rank[x] + Rank[father[x]]) % 2;father[x] = tmp;return father[x];}void merge(int x, int y){int fx, fy;fx = getfather(x);fy = getfather(y);if (fx == fy) {if (Rank[x] == Rank[y]) flag = false;}else {father[fx] = fy;Rank[fx] = (Rank[x] + Rank[y] + 1) % 2;}return;}int main(){cin >> t;while (t--) {cin >> n >> m;init();flag = true;int x, y;for (int i = 0; i < m; ++i) {scanf("%d%d", &x, &y);if (!flag) continue;merge(x, y);}cout << "Scenario #" << ++cas << ":" << endl;if (flag) cout << "No suspicious bugs found!" << endl << endl;else cout << "Suspicious bugs found!" << endl << endl;}return 0;}

0 0
原创粉丝点击