POJ 2492 A Bug's Life

xiaoxiao2021-02-27  161

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.   【题意】 以第一个样例为例: 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!”; 现在,题目就是要求我们判断是否存在同性恋。

 【分析】我们可以将2性别分为两个集合,比如a为一种性别,那我就可以把a+n

放在集合B里面,同时b放到B集合里,b+n放到A集合里,这样分类完了以后我就可以在A,B集合里判断是不是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; } printf("Scenario #%d:\n", w++); printf("%s\n", flag ? "Suspicious bugs found!" : "No suspicious bugs found!"); printf("\n"); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-11915.html

最新回复(0)