POJ P2492 A Bug's Life 并查集
2013-10-31 12:30
337 查看
A Bug's Life
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. Problem Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. Input The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one. Output The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong. Sample Input 2 3 3 1 2 2 3 1 3 4 2 1 2 3 4 Sample Output Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found! Hint Huge input,scanf is recommended. Source TUD Programming Contest 2005, Darmstadt, Germany |
自己写了个向量偏移。。。结果WA了两天。还是看题解明白的。
/article/6093247.html
#include <cstdio> int set[5010],offset[5010]; int n,m,k,i,j,x,y; bool flag; int FindSet(int x); void Union(int x, int y); int main() { scanf("%d",&k); for (int i = 1; i <= k; ++i){ scanf("%d%d", &n, &m); flag = true; for (j=1;j<=n;++j){ set[j] = j; offset[j] = 0; } for (j = 1; j <= m; ++j){ scanf("%d%d", &x, &y); if (flag) if (FindSet(x) != FindSet(y)) Union(x, y); else if (offset[x] == offset[y]) flag=0; } if (flag) printf("Scenario #%d:\nNo suspicious bugs found!\n", i); else printf("Scenario #%d:\nSuspicious bugs found!\n", i); if (i != k) printf("\n"); } return 0; } int FindSet(int x){ if (x != set[x]){ int temp=set[x]; set[x] = FindSet(set[x]); offset[x]^=offset[temp]; } return set[x]; } void Union(int x, int y){ int fx = FindSet(x); int fy = FindSet(y); set[fy] = fx; offset[fy] = offset[x]^offset[y]^1; return; }
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz
相关文章推荐
- hdoj 1829 A Bug's Life 【带权并查集】
- poj 2492 A BUG'S LIFE 不是纯裸的并查集
- POJ 2492 A Bug's Life 并查集的应用
- poj 2492 A Bug's Life 【并查集拓展】
- A Bug's Life 【POJ--2492】【带权的并查集】
- 【图论05】并查集 1004 A Bug's Life
- POJ 2492--A Bug's Life 【并查集,向量偏移】
- POJ2492A Bug's Life【并查集+根节点偏移】
- Pku acm 2492 A Bug's Life数据结构题目解题报告(十)---- 并查集的应用
- HDU1829 - A Bug's Life 分组并查集
- [POJ 2492]A Bug's Life[并查集]
- POJ 2492 A Bug's Life 并查集
- HDU 1829 A Bug's Life 并查集
- A Bug's Life --- 种类并查集
- hdu 1829 A Bug's Life 并查集
- hdu A Bug's Life 并查集 关系标记
- HDU1829 A Bug's Life 并查集
- POJ-2492 A Bug's Life 并查集
- POJ2492(A Bug's Life) && POJ1703(Find them, Catch them)带权(种类)并查集
- POJ 2492 A Bug's Life 并查集维护类别