A Bug's Life

Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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. 

 

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.
         
 题意: 有一堆虫,正常来说只能和异性有关系,现在给你一堆关系,问你这些中可能存在“同性恋”吗
思路1: (种类并查集) 除了正常的并查集之外,此题还要维护一个关系数组记录它与其父亲的性别关系,这个关系一般自己定义,
此题可以认为是同性为 1 , 异性为0 ,那么要找到它与其父亲的父亲的关系就可以使(rank[x]+rank[x+1] )%2 ,依次递归下去就可以得到它和他最上面的祖先的关系
并查集一般就考虑并,和查两部分,其中压缩路径的时候要注意将关系数组也要更新,
 #include <cstdio>
//存储的是其父亲的下表
int bugs[];
int relation[];//1:相同性别 0:不同性别
//初始化
void init(int len)
{
for(int i = ;i <= len; i++)
{
bugs[i] = i;
relation[i] = ;
}
}
//找到根
int find(int bug)
{
if(bugs[bug]==bug)return bug;
int tem = bugs[bug];
bugs[bug] = find(bugs[bug]);//递归更新域,返回最终的父亲节点,把所有的孩子都更新了
//注意这里,求当前位置和父亲的关系,记录之前父亲的位置为tem,然后因为是递归,
//此时的relation[tem]已经在递归中更新过了,也就是孩子和父亲的关系+父亲和爷爷的关系+1然后模2就得到
//孩子和爷爷的关系,这里用0和1表示,0表示不同性别,1表示相同性别
relation[bug] = (relation[bug]+relation[tem]+)%;
return bugs[bug];
} void union_set(int a,int b,int x,int y)
{
//合并,让前边的集合的根指向后边集合的根,成为一个集合
bugs[x]=y;
//更新前边集合根和新的集合根之间的关系,
//注意这里,relation[a]+relation[x]与relation[b]
//相对于新的父节点必须相差1个等级,因为他们不是gay
relation[x] = (relation[b]-relation[a])%; //这里的种类函数还是不理解,大概是根据一个关系的环状推出关系
} int main()
{
int S;
int n,inter;
int bug1,bug2,parent1,parent2;
bool flag;//false:无同性恋,true:有同性恋
scanf("%d",&S);
for(int i=; i<=S;i++)
{
scanf("%d%d",&n,&inter);
flag = false;
init(n);//初始化,使其父节点为自己
for(int j = ; j <= inter; j++)
{
scanf("%d%d",&bug1,&bug2);
if(flag)continue;// 因为当有同性的时候是依次读入的数据,要保证数据时读完的所以用continue
parent1 = find(bug1);
parent2 = find(bug2);
if(parent1==parent2)
{
if(relation[bug1]==relation[bug2])//同性
flag = true;
}
union_set(bug1,bug2,parent1,parent2);
}
if(flag)
printf("Scenario #%d:\nSuspicious bugs found!\n",i);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n",i);
printf("\n");
}
return ;
}

下面是dfs的思路,将所有的关系都用链接表的形式存起来,然以后肯定会有环的情况,当时奇环的时候(定点数是基数的时候)就存在同性恋,要是偶环就是不存在同性恋,扫描的时候统计节点数,当再次扫描到同一个点的时候看扫描过得点数是奇还是偶。

 #include <cstdio>
#include <cstring>
using namespace std;
#define N 2005
#define M 1000005 int head[N];
struct Edge{
int v, next;
}edge[*M];
int Ecnt; void init()
{
Ecnt = ;
memset(head, -, sizeof(head));
} void add(int u, int v)
{
edge[Ecnt].v = v;
edge[Ecnt].next = head[u];
head[u] = Ecnt++;
edge[Ecnt].v = u;
edge[Ecnt].next = head[v];
head[v] = Ecnt++;
}//存双向边 bool visited[N];
int f[N];//统计它是男生还是女生,定义 男生是1 女生是0 搜索的时候按0,1,0,1 的顺序标记点,如果再次访问到原先的点的时候应该按顺序标的值与其之前标的不一样的话就是奇环
bool dfs(int u)
{
for(int i = head[u]; i != -; i = edge[i].next)//遍历每一条边
{
int v = edge[i].v;//下一个点
if(!visited[v])//如果没有访问过这个点
{
visited[v] = ;
f[v] = (-f[u]); //按0,1 交替顺序的标记点
bool res = dfs(v);//如果在这之前就已经发现有奇环的话就直接返回FALSE
if(res == false) return false;
}
else if(f[u] == f[v]) return false;//最后又访问到了这个点,就判断这两个点是否是同一个值
}
return true;
} int main()
{
int T, n, m;
scanf("%d", &T);
int cas = ;
while(T--)
{
printf("Scenario #%d:\n", cas++);
scanf("%d %d", &n, &m);
init();
int s, t;
for(int i = ; i < m; i++)
{
scanf("%d %d", &s, &t);
add(s, t);
}
memset(visited, , sizeof(visited));
memset(f, -, sizeof(f));
bool ans = true;
for(int i = ; i <= n; i++)
{
if(!visited[i])
{
visited[i] = ;
f[i] = ;
bool res = dfs(i);
if(res == false)
{
ans = false;
break;
}
}
}
if(ans) puts("No suspicious bugs found!\n");
else puts("Suspicious bugs found!\n");
}
return ;
}
 

A Bug's Life(种类并查集)(也是可以用dfs做)的更多相关文章

  1. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  2. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  3. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

  4. HDU 1829 A Bug's Life(种类并查集)

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  5. hdu1829A Bug's Life(种类并查集)

    传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...

  6. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  7. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  8. POJ2492:A Bug's Life(种类并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 45757   Accepted: 14757 题 ...

  9. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

随机推荐

  1. iis 10 ftp 被动模式配置

    第一步: 进入 Server Level 的FTP Firewall Support 第二步: 在 Data Channel Port Range 下配置 Passive mode 的端口号范围,注意 ...

  2. Docker安装入门 -- 应用镜像

    Docker安装入门 -- 应用镜像 WordPress  1.docker build -t csphere/wordpress:4.2 .  2.docker run -d -p 80:80 -- ...

  3. lesson - 8 课程笔记 tar / gzip /bzip2 / xz /

    作用:为linux的文件和目录创建档案,也可以在档案中改变文件,或者向档案中加入新的文件即用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的  语法:tar[必要参数][选择参数][ ...

  4. 系统内置委托:Func/Action

    lSystem.Func 代表有返回类型的委托 lpublic delegate TResult  Func<out TResult>(); lpublic delegate TResul ...

  5. BCL和CoreFx的区别

    bcl是.netframework clr 的基础库corefx是.net core clr的基础库

  6. thinkinginjava学习笔记02_对象

    对象 1. 对象通过一个引用来操作,但是java中的对象是按值传递的,基本上可以在操作中认为对象本身,在内部结构中仍然要记得是对象实体的引用:如:String s = "abcd" ...

  7. 15个超强悍的CSS3圆盘时钟动画赏析

    在网页上,特别是个人博客中经常会用到时钟插件,一款个性化的时钟插件不仅可以让页面显得美观,而且可以让访客看到当前的日期和时间.今天我们给大家收集了15个超强悍的圆盘时钟动画,很多都是基于CSS3,也有 ...

  8. JavaWeb一小时急速入门总结反思

    0.条件一个可以开发javaee的开发工具1.配置首先需要下载tomcat,用来跑javaweb的服务器,下载后自行百度配置.然后需要servlet.jar包,其实就是一个java网络层api包2.基 ...

  9. 横向、纵向时间轴timeline系列

    近期移动端项目用到了很多时间轴.纵向的.开始可以实现,但是不利于维护.整理下, 以作为备份留存学习参考.子元素的 标签的 :before实现圆点,:after实现边线border纵向时间轴,单一右边内 ...

  10. Go Language 开发环境搭建

    [前言] Go语言是Google公司2009年发布的新一代的开发语言,其最大的亮点是原生自带并发,在合适的场合使用合适的代码,我们的系统将会更加完美.下面我们进行go语言的安装和配置. 一.下载安装 ...