A Bug's Life(种类并查集)(也是可以用dfs做)
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
Output
Sample Input
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
#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做)的更多相关文章
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- 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 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- Linux中dos2unix批量转换
有时候遇到多层目录下的文件格式需要转换,dos2unix 没有-r之类的递归指令,所以需要与find还有管道结合. find -type f | xargs dos2unix -o
- CET——4 常用短语
在网上看到的,先拔到自己这来,四级大大,求过!!!!
- androidSD卡操作
1.获取SD卡目录:File file = Environment.getExternalStorageDirectory(); 2.获取SD卡路径:String path = Environment ...
- js回到顶部------转载
[1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 [注意] ...
- ES6 数组的扩展
1. Array.from() Array.from()将类数组(array-like)对象与可遍历的对象转化为数组并返回. 下面是一个类数组 let arr = { '0':'a', '1':'b' ...
- flask入门篇
flask,Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . Flask简单易学,属于轻量级的,学起来 ...
- Effective Java 第三版——13. 谨慎地重写 clone 方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 视觉SLAM的方案总结
MoNoSLAM:https://github.com/hanmekim/SceneLib2 以扩展卡尔曼滤波为后端,追踪前端非常稀疏的特征点,以相机的当前状态和所有路标点为状态量,更新其均值和协方差 ...
- Fiddler工具的界面说明
1.Fiddler界面说明 2.session的模块说明 包含部分如下: 注:标蓝色区域的内容为平时较常用的部分 3.session不同图标和不同颜色的含义 默认颜色的含义如下: 部分图标的含义如下: ...
- golang 数组反转
我做hackerearth上题目记录,具体的题目描述是这样的: Given the size and the elements of array A, print all the elements i ...