做出的第一道NOI题目?(噗,还是看题解才会的…按某篇解题说的,这题就比我年轻四岁…T T 做的第一道IOI题目是USACO上的Packing Rectangles...这题比我还老!)对我等弱渣来说难度蛮大的,做了好几次并查集题目了,统统都觉得好抽象啊…这一题的难点就是,你不能直接将所有的点分为1,2,3三组,你只知道关系。边带权值的并查集也是第一次听说,一个mod 3就搞定实在太神奇…

Father数组记录祖先,Relation数组记录和祖先的距离

Relation[i]=0 表示和祖先是同类,Relation[i]=1 表示被祖先吃,Relation[i]=2表示吃祖先。

网上对于这篇的解题非常多,开成3*n数组的解法有待我研究,同时搞不清这跟向量有什么关系…最终参考比较多的是这篇文章。http://www.cnblogs.com/FreeDestiny/archive/2011/10/28/2227398.html

但是我对路径压缩,和对于Relation数组在Find函数中的更新还不是太理解,= =让我再想想。

program vijos_p1531;
const maxn=;
var relation,father:array[..maxn] of longint;
n,k,i,j,ans,t,x,y,fx,fy:longint; function find(x:longint):longint;
var ff:longint;
begin
if father[x]=x then exit(x);
ff:=find(father[x]);
relation[x]:=(relation[x]+relation[father[x]]) mod ;
father[x]:=ff;
exit(ff);
end; begin
readln(n,k);
for i:= to n do
begin
father[i]:=i;
relation[i]:=;
end;
ans:=;
for i:= to k do
begin
readln(t,x,y);
if (x>n) or (y>n) then
begin
inc(ans);
continue;
end;
if t= then
begin
fx:=find(x);fy:=find(y);
if (fx=fy) and (relation[x]<>relation[y]) then
begin
inc(ans);
continue;
end;
if (fx<>fy) then
begin
father[fx]:=fy;
relation[fx]:=(relation[y]-relation[x]+) mod ;
end;
end;
if t= then
begin
fx:=find(x);fy:=find(y);
if (fx=fy) and ((relation[x]-relation[y]+)mod <>) then
begin
inc(ans);
continue;
end;
if (fx<>fy) then
begin
father[fx]:=fy;
relation[fx]:=(relation[y]-relation[x]+) mod ;
end;
end;
end;
writeln(ans);
end.

食物链

测试数据 #0: Accepted, time = 7 ms, mem = 1008 KiB, score = 10

测试数据 #1: Accepted, time = 7 ms, mem = 1008 KiB, score = 10

测试数据 #2: Accepted, time = 15 ms, mem = 1004 KiB, score = 10

测试数据 #3: Accepted, time = 0 ms, mem = 1008 KiB, score = 10

测试数据 #4: Accepted, time = 15 ms, mem = 1012 KiB, score = 10

测试数据 #5: Accepted, time = 15 ms, mem = 1008 KiB, score = 10

测试数据 #6: Accepted, time = 31 ms, mem = 1008 KiB, score = 10

测试数据 #7: Accepted, time = 62 ms, mem = 1008 KiB, score = 10

测试数据 #8: Accepted, time = 78 ms, mem = 1008 KiB, score = 10

测试数据 #9: Accepted, time = 62 ms, mem = 1008 KiB, score = 10

[vijos P1531] 食物链的更多相关文章

  1. [vijos P1112] 小胖的奇偶

    第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...

  2. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  3. 【BZOJ 1061】【Vijos 1825】【NOI 2008】志愿者招募

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 https://vijos.org/p/1825 直接上姜爷论文... #include< ...

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

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

  5. vijos P1915 解方程 加强版

    背景 B酱为NOIP 2014出了一道有趣的题目, 可是在NOIP现场, B酱发现数据规模给错了, 他很伤心, 哭得很可怜..... 为了安慰可怜的B酱, vijos刻意挂出来了真实的题目! 描述 已 ...

  6. vijos P1780 【NOIP2012】 开车旅行

    描述 小\(A\)和小\(B\)决定利用假期外出旅行,他们将想去的城市从\(1\)到\(N\)编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市\(i\)的海拔高度为 ...

  7. 【BZOJ 2541】【Vijos 1366】【CTSC 2000】冰原探险

    http://www.lydsy.com/JudgeOnline/problem.php?id=2541 https://vijos.org/p/1366 loli秘制大爆搜_(:з」∠)_坑了好久啊 ...

  8. 【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流

    http://www.lydsy.com/JudgeOnline/problem.php?id=1065 https://vijos.org/p/1826 好难的题啊TWT ∈我这辈子也想不出来系列~ ...

  9. tyvj1202 数数食物链

    描述 TsyD学习了生物的生态环境那一张后,老师留了一项作业,就是给一张食物网,求所有食物链的总数.(从最低营养级生物(它不能吃任何其他的生物)开始到最高营养级(它不能被任何其他生物吃) 叫做一条食物 ...

随机推荐

  1. go中间的&和*

    package main import "fmt" func main() { var a int = 1 var b *int = &a var c **int = &a ...

  2. 转:不是技术牛人,如何拿到国内IT巨头的Offer

    转自:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛 ...

  3. OpenGL的gluLookAt和glOrtho的关系

    OpenGL的gluLookAt和glOrtho的关系 一直不明白gluLookAt()和glOrtho()两者之间的关系:gluLookAt()是观察变换,glOrtho()是正交投影.glLook ...

  4. MyBatis学习笔记(二) 关联关系

    首先给大家推荐几个网页: http://blog.csdn.net/isea533/article/category/2092001 没事看看 - MyBatis工具:www.mybatis.tk h ...

  5. mysql:恢复mysql表结构

    mysql,frm格式恢复mysql表结构,以tuser.frm格式为例   新增数据库,如下,创建数据库名为ab   打开数据库,双击打开数据库   点右键新建表结构   新增表,里面只添加一个字段 ...

  6. android AutoCompleteTextView 自定义BaseAdapter

    最近项目中需要做搜索功能,实现类似 Google.Baidu 搜索的 下拉提示效果.Android为我们提供了 AutoCompleteTextView 控件来完成此功能. 网上好多例子都是简单使用 ...

  7. WebGL 入门-WebGL简介与3D图形学

    什么是WebGL? WebGL是一项使用JavaScript实现3D绘图的技术,浏览器无需插件支持,Web开发者就能借助系统显卡(GPU)进行编写代码从而呈现3D场景和对象. WebGL基于OpenG ...

  8. [saiku] 源码整合[maven整合]

    saiku源码的整合分为[普通web项目整合]和[maven整合]两种 本节主要是讲解如何整合为maven项目 转载自:http://blog.csdn.net/gsying1474/article/ ...

  9. VMware Workstation 无法连接到虚拟机

    "This PC(我的电脑)":右键"manage(管理)": "Service and Applications(服务和应用)":&quo ...

  10. sleep函数

    Linux下: #include <unistd.h> sleep(1); // 睡眠1秒 usleep(1); // 睡眠1微妙