首页 > 代码库 > POJ 2492 A Bug's Life
POJ 2492 A Bug's Life
A Bug‘s Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28121 | Accepted: 9142 |
Description
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.
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! 传送门:点击打开链接 大致题意: 解释一下第一个样例吧: 3,3表示3个数(1,2,3),接下来3行表示每两个数之间的关系; 1 2表示1,2是恋爱关系(就当成这个意思理解吧); 2 3表示2,3是恋爱关系; 1 3表示1,3是恋爱关系; 所以这3个人中存在同性恋,输出“Suspicious bugs found!”; 对于不存在同性恋的情况输出“No suspicious bugs found!”; 现在,题目就是要求我们判断是否存在同性恋。 解题思路: 分组并查集(种类并查集)。我们可以这样思考a b存在恋爱关系,表示他们是异性,之间相互矛盾,用集合A表示与a是相同性别 的元素,集合B表示与a是不同性别的元素,这样的话a放在A集合,a+n(表示与a相异的性别)放在B集合,b放在B集合,b+n放在 A集合。如果在统计时发现,a和a+n或是b和b+n在相同的集合里面就是出现了矛盾的情况,即存在同性恋,反之,不存在。 代码:#include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 2010; int set[MAXN<<1]; int find(int p) { if(set[p] < 0) return p; return set[p] = find(set[p]); } void join(int p, int q) { p = find(p); q = find(q); if(p != q) set[p] = q; } int main() { int t, m, n, w = 1; scanf("%d", &t); while(t--) { memset(set, -1, sizeof(set)); scanf("%d%d" , &n, &m); bool flag = false;//没有矛盾情况 while(m--) { int a, b; scanf("%d%d", &a, &b); join(a, b+n); join(b, a+n); if(find(a)==find(a+n) || find(b)==find(b+n)) flag = true; } if(1 != w) printf("\n"); printf("Scenario #%d:\n", w++); printf("%s\n", flag ? "Suspicious bugs found!" : "No suspicious bugs found!"); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。