并查集经典题
1. 向量的思考模式
2. 再计算向量时,要画图;有一个关系一开始写错了
3. 本人的norm函数一开始x >= 3写成了 x>3,应该对这种小函数多做UT(口头上的,比如)
4. 可以把father set一开始memset为-1
参考链接
http://blog.csdn.net/tiantangrenjian/article/details/7085575
http://pdjlzs.diandian.com/post/2012-02-03/15719424

#include <iostream>
using namespace std; #define ANI_MAX (50000 + 10) int ss[ANI_MAX];
int rk[ANI_MAX]; int norm(int x)
{
if (x >= 3)
{
x = x % 3;
}
else if (x < 0)
{
while (x < 0)
{
x += 3;
}
}
return x;
} void init()
{
for (int i = 0; i < ANI_MAX; i++)
{
ss[i] = i;
}
memset(rk, 0, sizeof(rk));
} int find(int x)
{
if (ss[x] == x)
{
return x;
}
else
{
int r = find(ss[x]);
rk[x] = norm(rk[x] + rk[ss[x]]);
ss[x] = r;
return r;
}
} bool merge(int x, int y, int type)
{
int fx = find(x);
int fy = find(y);
if (fx == fy)
{
if (norm(rk[y] - rk[x]) != type)
{
return true;
}
else
{
return false;
}
} ss[fy] = fx;
rk[fy] = norm(rk[x] - rk[y] + type);
return false;
} int main(void)
{
int n;
int k;
scanf("%d %d",&n,&k);
int error = 0;
init();
for (int i = 0; i < k; i++)
{
int d;
int x;
int y;
scanf("%d %d %d",&d,&x,&y);
if (x > n || y > n || (d == 2 && x == y))
{
error++;
continue;
}
else if (merge(x, y, d - 1))
{
error++;
} } cout << error << endl; return 0;
}

  

POJ1182 食物链的更多相关文章

  1. 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)

    并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构,  可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...

  2. POJ1182 食物链---(经典种类并查集)

    题目链接:http://poj.org/problem?id=1182   食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

  3. NOI2001|POJ1182食物链[种类并查集 向量]

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

  4. POJ-1182 食物链 并查集(互相关联的并查集写法)

    题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...

  5. poj1182食物链_并查集_挑战程序设计竞赛例题

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

  6. [poj1182]食物链(并查集+补集)

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

  7. poj1182(食物链)续

    意 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用 ...

  8. poj1182(食物链)

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

  9. POJ1182 食物链(并查集)

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

  10. (转)poj1182食物链

    这题主要是看了http://blog.csdn.net/c0de4fun/article/details/7318642这篇解题报告,所以内容基本是转的!感谢大牛这么详细的把过程写的很清楚! 这道题目 ...

随机推荐

  1. windows不能在本地计算机启动apache

    今天,配置eclipse PHP studio 3.0的时候更改了apache http server 中的httpd.conf文件: 将DocumentRoot 的路径设错了,为一个不存在目录 .更 ...

  2. Asp.net MVC知识积累

    一.知识积累 http://yuangang.cnblogs.com/ 跟蓝狐学mvc教程专题目录:http://www.lanhusoft.com/Article/169.html 依赖注入:htt ...

  3. 新手自学ABAP(1)--数据类型

    一.DATA语句 1.TYPE type ex:  可以利用冒号声明多个变量. DATA : gv_num1 TYPE I,   gv_num2 TYPE I. 2.LIKE num   (num可以 ...

  4. 相同的 birthday

    Description Sometimes some mathematical results are hard to believe. One of the common problems is t ...

  5. hdu 1222 狼和兔子

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  6. hive中简单介绍分区表

    所介绍内容基本上是翻译官方文档,比较肤浅,如有错误,请指正! hive中创建分区表没有什么复杂的分区类型(范围分区.列表分区.hash分区.混合分区等).分区列也不是表中的一个实际的字段,而是一个或者 ...

  7. ES5中的有9个Array方法

    Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.prototype.some Array ...

  8. WebAPi(selfhost)

    ASP.NET WebAPi(selfhost)之文件同步或异步上传   前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+We ...

  9. inputstream和outputstream读写数据模板代码

    //读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(b ...

  10. mysql存储过程中传decimal值会自动四舍五入,没有小数

    通过 call  proc(0.2,0.5);  查看结果数据库竟然是0  和 1 原因:proc的参数没有设置好 参数:原本是  in a decimal,in b decimal 应该改为:in ...