poj2492 A Bug's Life【基础种类并查集】
转载请注明出处,谢谢: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【基础种类并查集】的更多相关文章
- hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...
- hdu1829 A Bug's Life 基础种类并查集
题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...
- POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- 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 ...
- A Bug's Life(hdu1829种类并查集)
题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
随机推荐
- cocos2d-x学习日志(12) --弹出对话框的设计与实现
我们时常需要这么些功能,弹出一个层,给与用户一些提示,这也是一种模态窗口,在没有对当前对话框进行确认的时候,不能继续往下操作. 功能分析 我们设计一个对话框,对话框上有几个按钮(个数可定制),当然有个 ...
- hadoop搭建杂记:Linux下虚拟机集群网络搭建
VirtualBox搭建hadoop伪分布式模式 VirtualBox搭建hadoop伪分布式模式 master: ip:192.168.56.120 机器名: master 启动NameNode 启 ...
- Ubuntu14.04 64bit安装Android-Studio
用PPA安装Android-Studio炒鸡简单,墙内亲测可用,就是速度慢了点.(睡觉时开着电脑装-) 安装Android-Studio 打开Terminal,执行: sudo add-apt-rep ...
- FileNameExtensionFilter文件过滤
package com.soft.test; import javax.swing.*; import javax.swing.filechooser.*; import java.awt.event ...
- Linux系统学习笔记之 1 一个简单的shell程序
不看笔记,长时间不用自己都忘了,还是得经常看看笔记啊. 一个简单的shell程序 shell结构 1.#!指定执行脚本的shell 2.#注释行 3.命令和控制结构 创建shell程序的步骤 第一步: ...
- MYSQL 执行计划
Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...
- 探索 Windows Azure 网站中的自动伸缩功能
去年10月,我们发布了若干针对 WindowsAzure平台的更新,其中一项更新是添加了基于日期的自动伸缩调度支持(在不同的日期设置不同的规则). 在这篇博客文章中,我们将了解自动伸缩的概念,并 ...
- VC中使用GetModuleFileName获取应用程序路径
.\\与API函数GetModuleFileName获取应用程序目录有何不一样? 采用.\\也能获得应用程序目录,采用GetModuleFileName也能获得,二者有何不同? 一样!一个是相对路 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- Candy----HDU4465----数学题
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465 题目意思: 有两个箱子,每个箱子装有N个糖果 打开第一个箱子的概率是P,另外一个就是1-P 当小 ...