又见关系并查集 以POJ 1182 食物链为例
简单的关系并查集一般非常easy依据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离。
对于此题:
若u。v不在一个集合内,则显然此条语句会合法(暂且忽略后两条。下同)。
那么将fu 变为 fv的儿子时需加一条权值为 w 的边,w 满足(w + ru)%3 = (rv+ (D == 1?
0 : 1))%3(ru。rv分别为u,v与fv的关系,即距离)。
之所以在D == 2时加 1。是由于u吃v表明着u到fv的距离比v到fv的距离大1。
同理。D == 1时,表明两者到fv的距离应该相等。
若u,v在一个集合内。仅仅须要推断ru%3 == (rv+(D == 1?):1))%3 是否成马上可。
只是这个题数据略坑啊。写成多组输入的根本过不了。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f using namespace std; struct N
{
int fa,re;
} st[50010]; int Find(int x,int &R)
{
int f,re = 0; f = x; while(f != st[f].fa)
{
re = (re+st[f].re)%3;
f = st[f].fa;
} R = re;
int tre = 0,temp,tf; while(x != st[x].fa)
{
tf = st[x].fa;
temp = st[x].re; st[x].re = (re-tre+3)%3; tre = (tre+temp)%3;
st[x].fa = f;
x = tf;
} return f;
} bool Merge(int w,int u,int v)
{
int ru,rv;
int fu = Find(u,ru);
int fv = Find(v,rv); if(fu != fv)
{
st[fu].fa = fv;
if(w == 2)
st[fu].re = ((rv+1)%3 - ru + 3)%3;
else
st[fu].re = (rv%3- ru + 3)%3;
}
else
{
if(w == 1 && ru != rv)
return false; if(w == 2 && ru != (rv+1)%3 )
return false;
} return true;
} int main()
{
int n,k; int i,j,u,v,w; scanf("%d %d",&n,&k);
{
for(i = 1; i <= n; ++i)
st[i].fa = i,st[i].re = 0; int ans = 0; while(k--)
{
scanf("%d %d %d",&w,&u,&v); if(u > n || v > n || (w == 2 && u == v))
{
ans++;
continue;
} if(Merge(w,u,v) == false)
{
ans++;
}
} printf("%d\n",ans);
} return 0;
}
又见关系并查集 以POJ 1182 食物链为例的更多相关文章
- 并查集:POJ 1182 食物链 复习
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- poj 1182 食物链(关系并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62824 Accepted: 18432 Description ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- [并查集] POJ 1182 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66294 Accepted: 19539 Description ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
随机推荐
- PatentTips - Increasing turbo mode residency of a processor
BACKGROUND Many modern operating systems (OS's) use the Advanced Configuration and Power Interface ( ...
- HDU 3240
求卡特兰数前N项的和模M. 直接求必定是不可能的,卡特兰数太大了.想了好久,本打算把位数拆成素数相乘,然后记录下各素数的个数计算.可惜,TLE....因为N太大了. 除法必定是要用到逆元的,但分母与M ...
- cf #257(Div.2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- JDBC连接mysql时出现的ssl问题
使用MySQL数据库时出现如下错误: WARN: Establishing SSL connection without server's identity verification is not r ...
- m_Orchestrate learning system---二十一、怎样写算法比较轻松
m_Orchestrate learning system---二十一.怎样写算法比较轻松 一.总结 一句话总结:(1.写出算法步骤,这样非常有利于理清思路,这样就非常简单了 2.把问题分细,小问题用 ...
- oracle 11g dbms_workload_repository手工管理AWR快照,基线
1.修改快照设置[sql] view plain copysys@ORCL> select * from dba_hist_wr_control; DBID SNAP_INTERVAL RETE ...
- python 3.x 学习笔记6 ( 迭代器 and 生成器 )
1.迭代器(Iterator): 可以被next()函数调用并不断返回下一个值的对象,成为迭代器:Iterator 可以直接用于for 循环的对象统称为可迭代对象:Iterable 迭代,顾名思 ...
- 利用SQL索引提高查询速度
1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引结构. 索引的使用要恰到好处,其使用原则如下: 在经常进行连接 ...
- php面向对象的基础:OOP的常量
常量(constant) 用来表示不会改变的值.对于从该类实例化的任何对象来说,常量值在这些对象的整个生命周期中都保持不变. class Computer{ const PI=3.1415926; } ...
- Eclipse安装Web插件
方法/步骤 本次安装教程,我把所有的步骤都写在了图片中,大家仔细查看图片即可,希望能帮到大家 1.选择菜单栏上的“Help” 选择Install New Software 在弹出的 ...