http://poj.org/problem?id=2492

题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的。

思路 :这个题跟POJ1703其实差不多,也是需要一个数组来存跟父亲节点的关系,需要两个集合来存是否有关系,在最后稍微变一下形就OK了。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h> using namespace std; int pre[];//代表着父亲结点,如果D后边是a b,则pre[b]=a ;
int father[] ;//代表着这个点和父亲结点的关系,是属于同一类还是不同团伙
int m,n ; int find(int x)
{
int temp = pre[x] ;
if (pre[x] == x)
return x ;
pre[x] = find(pre[x]);
father[x] = father[x] == father[temp] ? : ;//等于0表示属于同一类
return pre[x];
} void unionn(int x,int y,int xx,int yy)
{
pre[xx] = yy ;
father[xx] = father[x] == father[y] ? : ;
}
int main()
{
int t ;
scanf("%d",&t) ;
int m,n ,kase = ;
while(t--)
{
scanf("%d %d",&n,&m) ;
for(int i = ; i <= n ; i++)
{
pre[i]=i;
father[i] = ;
}
bool flag = false ;
int a,b ;
for(int i = ; i <= m ; i++)
{
scanf("%d %d",&a,&b) ;
int aa = find(a),bb = find(b);
if(!flag)
{
if(aa != bb)
unionn(a,b,aa,bb) ;
else
{
if(father[a] == father[b])
flag = true ;
}
}
}
printf("Scenario #%d:\n",kase) ;
kase++ ;
if(flag)
printf("Suspicious bugs found!\n\n") ;
else printf("No suspicious bugs found!\n\n") ;
}
return ;
}

POJ 2492 A Bug's Life(并查集)的更多相关文章

  1. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

  2. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

  3. POJ 2492 A Bug's Life (并查集)

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

  4. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  5. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  6. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  7. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  9. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

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

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

随机推荐

  1. angularjs Failed to read the 'selectionStart' property from 'HTMLInputElement':

    在找angularjs input(type='number')在获取焦点的时候,文本框内容选中效果,参考了:Select text on input focus,我直接复制他的code之后,在ion ...

  2. [wordpress]后台自定义菜单字段和使用wordpress color picker

    Wordpress Version 4.4.2 参考链接 插件使用wordpress color picker:Add A New Color Picker To WordPress 后台菜单自定义字 ...

  3. 【转】ArrayList的toArray,也就是list.toArray[new String[list.size()]];,即List转为数组

    [转]ArrayList的toArray ArrayList提供了一个将List转为数组的一个非常方便的方法toArray.toArray有两个重载的方法: 1.list.toArray(); 2.l ...

  4. IPayablebillItf

    package nc.itf.arap.payablebill; import nc.vo.pub.AggregatedValueObject; import nc.vo.pub.BusinessEx ...

  5. 编译gd-2.0.35.tar.gz时报错:gd_png.c:16:53: error: png.h: No such file or directory

    编译gd-2.0.35.tar.gz时报错: gcc -DHAVE_CONFIG_H -I. -I. -I. -I/usr/local/freetype/include/freetype2 -I/us ...

  6. oracle 12 C启动问题

    启动时出现以下报错信息: SQL> startup; ORA-48146: missing read, write, or exec permission on directory during ...

  7. eclipse下的tomcat内存设置大小

    在eclipse中设置,居然可以了, 设置步骤如下: 1.点击eclipse上的debug图标旁边的下拉箭头 2.然后选择Run Configurations, 3.系统弹出设置tomcat配置页面, ...

  8. lucene4.0 基于smb文件服务器的全文检索

    使用lucene 4.0版本的全文检索 所需要的jar包  网速太慢,下次有空再把jar传上来 1.FileIndex  建立索引,查询,删除,更新 package com.strongit.tool ...

  9. 给div设置一个关闭按钮.

    造轮子好难. 用惯了框架提供的组件,某天自己要做个伪组件(或者在他人创建的页面效果上添加新功能)会发现很难. 所以,碰到了,就一定要做下记录.以供日后查阅. 如图,弹出DIV右上角的关闭按钮是我此次添 ...

  10. 14_Request对象

    [HttpServletRequest简介] HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过 ...