转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html   ---by 墨染之樱花

题目链接:http://poj.org/problem?id=2492

题目描述:找基佬游戏(汗-_-b)有个hentai科学家研究虫子种群,不断地给出二元组xy,表示x和y是异性交往,但是可能会出现矛盾(找到基佬),比如1与2是异性恋,2与3是异性恋,却又告诉你1和3是异性恋。问种群中存不存在基佬败类

思路:与poj1182“食物链”几乎一样,还简单一点,毕竟只有两类物品。par[i]表示父节点,d[i]表示偏移量,0为同性,1为异性。不过要注意的一点是所有合并的过程要对二取模,比如x找到根结点s的路径是x1,x2...xn那么,d[x]=(d[x]+d[x1]+...d[xn])%2。poj1703与此题几乎一样,代码改改就能交了

#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 2000+10
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second int par[MAXN];
int d[MAXN];
int n,m; int find(int x)
{
int s,tot=;
for(s=x;par[s]>=;s=par[s])
tot+=d[s];
while(s!=x)
{
int temp=par[x];
par[x]=s;
int tmp=d[x];
d[x]=tot%;
tot-=tmp;
x=temp;
}
return s;
} int main()
{_
int T;
scanf("%d",&T);
REP(k,T)
{
scanf("%d%d",&n,&m);
CLR(par,-);CLR(d,);
bool flag=;
REP(i,m)
{
int x,y;
scanf("%d%d",&x,&y);
if(flag)
{
int r1=find(x),r2=find(y);
if(r1!=r2)
{
par[r2]=r1;
d[r2]=!(d[x]^d[y]);
}
else
if(!(d[x]^d[y]))
flag=;
}
}
printf("Scenario #%d:\n",k+);
if(!flag)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return ;
}

附poj1703 “Find them, Catch them”代码(c++OLE,g++AC...-_-b):

#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 100000+10
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second int par[MAXN];
int d[MAXN];
int n,m; int find(int x)
{
int s,tot=;
for(s=x;par[s]>=;s=par[s])
tot+=d[s];
while(s!=x)
{
int temp=par[x];
par[x]=s;
int tmp=d[x];
d[x]=tot%;
tot-=tmp;
x=temp;
}
return s;
} int main()
{_
int T;
scanf("%d",&T);
REP(k,T)
{
scanf("%d%d",&n,&m);
CLR(par,-);CLR(d,);
REP(i,m)
{
char c;
int x,y;
scanf("%s%d%d",&c,&x,&y);
int r1=find(x),r2=find(y);
if(c=='D')
{
if(r1!=r2) //成环不管矛不矛盾直接舍弃,否则会反复迭代造成TLE
{
par[r2]=r1;
d[r2]=!(d[x]^d[y]);
}
}
else
{
if(r1!=r2)
printf("Not sure yet.\n");
else if(d[x]^d[y])
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
}
}
return ;
}

poj2492 A Bug's Life【基础种类并查集】的更多相关文章

  1. hdu1829&&poj2492 A Bug's Life 基础种类并查集

    把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...

  2. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

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

    http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...

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

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

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

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

  6. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

  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. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  9. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

随机推荐

  1. 关于XPath的基本语法

    关于XPath基础语法 关于XPath基础语法 更详细的请看: XPath语法 XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) ...

  2. windows下搭建python+selenium环境

    1.安装python https://www.python.org/ 2.安装setuptools(python的基础包工具) 下载地址:https://pypi.python.org/pypi/se ...

  3. 对discuz的代码分析学习(二)首页文件

    如果当前地址栏存在查询字符,并且是一个数字,条件成立. 查询字符:www.baidu.com/index.php?aaa=bbb              aaa=bbb就是查询字符如果条件成立,则把 ...

  4. JSTL与EL常用标签(转)

    JSTL与EL EL相关概念 JSTL一般要配合EL表达式一起使用,来实现在jsp中不出现java代码段.所以我们先来学习EL表达式 EL主要用于查找作用域中的数据,然后对它们执行简单操作:它不是编程 ...

  5. 关于strcpy的实现.

    #include <stdio.h> #include <stdlib.h> int strlen(const char *str) { ; while(*str++!='\0 ...

  6. Delphi内存操作API函数(备查,并一一学习)

    Delphi内存操作API函数System.IsMemoryManagerSet;System.Move;System.New;System.ReallocMem;System.ReallocMemo ...

  7. distributor之Interrupt Set/Clear-Active Registers, GICD_IS/CACTIVERn

    set active寄存器.顾名思义就是把一个中断置为active状态,clear active寄存器就是清除active状态,在这里我们有必要说明一下中断状态的一些概念: active状态:假设此时 ...

  8. GBK转utf-8,宽字符转窄字符

    //GBK转UTF8 string CAppString::GBKToUTF8(const string & strGBK) { string strOutUTF8 = "" ...

  9. POJ 2406 Power Strings(字符串的最小循环节)

    题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...

  10. USACO Section 5.4 TeleCowmunication(最小割)

    挺裸的一道最小割.把每台电脑拆成一条容量为1的边,然后就跑最大流.从小到大枚举每台电脑,假如去掉后 最大流=之前最大流+1,那这台电脑就是answer之一了. -------------------- ...