A Bug's Life POJ

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.

题目:教授假设这种虫子有两种性别,且只有异性的虫子之间才有交流,每个虫子从1开始编号。接下来给出每对虫子之间的交流,问是不是只有异性之间才有交流。

思路:带权并查集板子题(不清楚带权并查集的点这里------>传送门)。同性权值为0,异性权值为1。

1 1 #include <iostream> 2 2 #include <cstdio> 3 3 #include <algorithm> 4 4 #include <queue> 5 5 #include <vector> 6 6 #include <cmath> 7 7 8 8 using namespace std; 9 9 1010 #define ll long long 1111 #define pb push_back 1212 #define fi first 1313 #define se second 1414 1515 const int N = 2000 << 1; 1616 struct node 1717 { 1818 int rt, v; 1919 }fa[N]; 2020 2121 int Find(int x) 2222 { 2323 if(fa[x].rt == x) return x; 2424 else{ 2525 int tmp = fa[x].rt; 2626 fa[x].rt = Find(tmp); 2727 fa[x].v = (fa[x].v + fa[tmp].v) % 2; 2828 return fa[x].rt; 2929 } 3030 } 3131 3232 bool Union(int x, int y) 3333 { 3434 int fax = Find(x); 3535 int fay = Find(y); 3636 if(fax != fay){ 3737 fa[fay].rt = fax; 3838 fa[fay].v = (fa[x].v + 1 - fa[y].v) % 2; 3939 return true; 4040 }else{ 4141 if(0 == (fa[x].v + 1 - fa[y].v) % 2) return true; 4242 else return false; 4343 } 4444 } 4545 4646 void solve() 4747 { 4848 int T; 4949 scanf("%d", &T); 5050 for(int _case = 1; _case <= T; ++_case){ 5151 int n, m; 5252 scanf("%d%d", &n, &m); 5353 for(int i = 0; i <= n; ++i){ 5454 fa[i].rt = i; 5555 fa[i].v = 0; 5656 } 5757 vector<pair<int ,int > > info; 5858 int x, y; 5959 for(int i = 1; i <= m; ++i){ 6060 scanf("%d%d", &x, &y); 6161 info.pb({x, y}); 6262 } 6363 6464 int error = 0; 6565 for(int i = 0; i < m; ++i){ 6666 x = info[i].fi; 6767 y = info[i].se; 6868 if(!Union(x, y)){ 6969 error = 1; 7070 break; 7171 } 7272 } 7373 7474 printf("Scenario #%d:\n", _case); 7575 printf("%s\n\n", error == 1 ? "Suspicious bugs found!" : "No suspicious bugs found!"); 7676 } 7777 } 7878 7979 int main() 8080 { 8181 8282 solve(); 8383 8484 return 0; 8585 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )