poj 2492 A Bug's Life(带权并查集)
2014-04-23 09:03
411 查看
题目:poj 2492 A Bug's Life(带权并查集)
题目大意:一个博士研究虫子里面是否有同性恋,给出多次虫子的交配关系。
解题思路:如果这两只虫子之前没有确定关系的话,每次交配就说明这两只虫子的性别不同,这样用c【x】 = 1 代表和根不同, 0 就是相同的意思,在每次的交配关系中如果发现两只c数组值相同的虫子进行交配,就有同性恋。这题和poj1703的解题思路是一致的。
代码:
题目大意:一个博士研究虫子里面是否有同性恋,给出多次虫子的交配关系。
解题思路:如果这两只虫子之前没有确定关系的话,每次交配就说明这两只虫子的性别不同,这样用c【x】 = 1 代表和根不同, 0 就是相同的意思,在每次的交配关系中如果发现两只c数组值相同的虫子进行交配,就有同性恋。这题和poj1703的解题思路是一致的。
代码:
#include <stdio.h> const int N = 2005; int f , value , n, m; void init () { for (int i = 0; i <= n; i++) { f[i] = i; value[i] = 0; } } int getfather (int x) { if (x == f[x]) return x; else { int t = f[x]; f[x] = getfather (f[x]); value[x] = (value[x] + value[t]) % 2; return f[x]; } } int main () { int cas = 0, t, x, y; bool flag = 0; scanf ("%d", &t); while (t--) { flag = 0; scanf ("%d%d", &n, &m); init(); // for (int i = 0; i <= n; i++) // printf ("%3d", f[i]); // printf ("\n"); for (int i = 0; i < m; i++) { scanf ("%d%d", &x, &y); int p = getfather (x); int q = getfather (y); if (q != p) { f[p] = q; value[p] = (value[y] + 1 + value[x]) % 2; } else { if (value[x] == value[y]) flag = 1; } } // for (int i = 1; i <= n; i++) // printf("%3d%3d\n", f[i], value[i]); printf ("Scenario #%d:\n", ++cas); if (flag) printf ("Suspicious bugs found!\n"); else printf ("No suspicious bugs found!\n"); if (t) printf ("\n"); } return 0; }
相关文章推荐
- POJ 2492 A Bug's Life [数据结构-并查集 union-find sets]
- poj 2492 A Bug's Life(种类并查集)
- poj 2492 A Bug's Life(带权并查集)
- POJ 2492 A Bug's Life (并查集)
- [POJ 2492] A Bug's Life [扩展并查集]
- POJ 2492 A Bug's Life(并查集)
- poj_2492 A Bug's Life (并查集)
- Hdu 1892&&Poj 2492 A Bug's Life[判断二分图 || 种类并查集]
- poj2492 A Bug's Life 种类并查集
- POJ - 2492 A Bug's Life(种类并查集)
- poj 2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life(路径压缩并查集)
- POJ 2492 || HDU 1829:A Bug's Life(并查集)
- POJ 2492 A Bug's Life (并查集)
- POJ 2492 A Bug's Life(种类并查集 + 偏移量)
- POJ 2492 A Bug's Life(加权并查集)
- hdu 1829 &poj 2492 A Bug's Life(判断二分图、带权并查集)
- poj 2492 A Bug's Life(并查集)分组并查集
- poj2492——A Bug's Life(并查集扩展)