A Bug's Life POJ

Stella981
• 阅读 429

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 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <vector>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 #define ll long long
11 #define pb push_back
12 #define fi first
13 #define se second
14 
15 const int N = 2000 << 1;
16 struct node
17 {
18     int rt, v;
19 }fa[N];
20 
21 int Find(int x)
22 {
23     if(fa[x].rt == x) return x;
24     else{
25         int tmp = fa[x].rt;
26         fa[x].rt = Find(tmp);
27         fa[x].v = (fa[x].v + fa[tmp].v) % 2;
28         return fa[x].rt;
29     }
30 }
31 
32 bool Union(int x, int y)
33 {
34     int fax = Find(x);
35     int fay = Find(y);
36     if(fax != fay){
37         fa[fay].rt = fax;
38         fa[fay].v = (fa[x].v + 1 - fa[y].v) % 2;
39         return true;
40     }else{
41         if(0 == (fa[x].v + 1 - fa[y].v) % 2) return true;
42         else return false;
43     }
44 }
45 
46 void solve()
47 {   
48     int T;
49     scanf("%d", &T);
50     for(int _case = 1; _case <= T; ++_case){
51         int n, m;
52         scanf("%d%d", &n, &m);
53         for(int i = 0; i <= n; ++i){
54             fa[i].rt = i;
55             fa[i].v = 0;
56         }
57         vector<pair<int ,int > > info;
58         int x, y;
59         for(int i = 1; i <= m; ++i){
60             scanf("%d%d", &x, &y);
61             info.pb({x, y});
62         }
63 
64         int error = 0;
65         for(int i = 0; i < m; ++i){
66             x = info[i].fi;
67             y = info[i].se;
68             if(!Union(x, y)){
69                 error = 1;
70                 break;
71             }
72         }
73 
74         printf("Scenario #%d:\n", _case);
75         printf("%s\n\n", error == 1 ? "Suspicious bugs found!" : "No suspicious bugs found!");
76     }
77 }
78 
79 int main()
80 {
81 
82     solve();
83 
84     return 0;
85 }
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这