简单的关系并查集一般非常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 食物链为例的更多相关文章

  1. 并查集:POJ 1182 食物链 复习

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...

  2. poj 1182 食物链 (带关系的并查集)

      食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...

  3. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  4. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  5. poj 1182 食物链(关系并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62824   Accepted: 18432 Description ...

  6. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  7. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  8. [并查集] POJ 1182 食物链

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description ...

  9. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

随机推荐

  1. MVC总结

    一.转自https://zhuanlan.zhihu.com/p/35680070 MVC无人不知,可很多程序员对MVC的概念的理解似乎有误,换言之他们一直在错用MVC,尽管即使如此软件也能被写出来, ...

  2. 接口测试及接口Jmeter工具介绍

    一.接口类型及数据传递的格式 接口类型: 1.HTTP接口:通过GET或POST来获取数据,在数据处理上效率比较高 2.WebServer接口:通过SOAP协议来获取数据,比起http来说处理更加复杂 ...

  3. Linux5355端口被0.0.0.0监听

    Linux后台有个systemd-resolv进程,占用5355等端口 博主在一次网络安全加固行动中,netstat -anp发现Linux后台有一个被0.0.0.0监听的端口,5355,显示被sys ...

  4. 【【henuacm2016级暑期训练】动态规划专题 M】Little Pony and Harmony Chest

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每一位显然只要取1..60这些数字. 然后需要保证每个这些数字里面,每个数字所用到的质因子都它所唯一拥有的.别人不能用 因为如果别人 ...

  5. 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. ZOJ 3288 Domination

    D - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Descr ...

  7. springmvc 异常Interceptor

    无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了. springMVC提 ...

  8. kentico中的urls

    alias是默认的访问页面 page aliases中可以手动指定访问一个url,然后跳转到当前的页面

  9. ICMP 隧道——将流量封装进 IMCP 的 ping 数据包中,旨在利用 ping 穿透防火墙的检测

    利用 ICMP 隧道穿透防火墙 转自:http://xiaix.me/li-yong-icmp-sui-dao-chuan-tou-fang-huo-qiang/ 以前穿透防火墙总是使用 SSH 隧道 ...

  10. 机器翻译引擎的基本原理 ——LSTM

    机器翻译引擎的基本原理  摘自:infoq 谷歌机器翻译 Zero-shot:零次 Training:训练 Google Neural Machine Translation:谷歌神经机器翻译 我们每 ...