做出的第一道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. OpenGL的几何变换2之内观察立方体

    我想实现的一个场景是:一个立方体,相机的坐标在立方体的中心点,相机不变,立方体旋转,可以站在立方体中心点查看立方体内部. 实际上就是立方体图像,这是在全景图片当作比较简单的方式,画面不会变形和扭曲,但 ...

  2. flume Permission denied: user=flume, access=WRITE, inode

    My flume app is attempting to write to HDFS on a path thats not been created/granted for it. The pat ...

  3. dom4j解析xml作为测试数据

    构造函数: public AppTestData(File xmlFile) throws Exception { file = xmlFile; try { saxReader = new SAXR ...

  4. C#中的ManagementClass类

    C# 提供了ManagementClass类来获取本机的一些基本信息,比如CPU的个数,CPU的频率,网卡的MAC,内存的大小,硬盘的大小等. 获取本机MAC地址: /// <summary&g ...

  5. IIS提示Server Application Unavailable

    浏览器访问网站,IIS提示Server Application Unavailable,我的解决方式是进入IIS管理界面,找到对应的站点,之后重启这个站点.

  6. js,jquery判断某一节点是否存在

    前两天工作时遇到一问题,就是模块A显示时,B是一种样式,模块A删除,B是另一种样式.记录下判断节点存在的方法. 先写下html <!doctype html> <html> & ...

  7. Flask+mongodb 实现简易个人博客

    最近学习完了<flask-web开发>,实现了一个简易的个人博客网站,由flask+mongodb+bootstrap做成, 这个软件是在阅读<Flask-Web开发>后写的一 ...

  8. C线程同步/异步

    #include<windows.h> #include<stdio.h> #include<string.h> //#include <afxmt.h> ...

  9. hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. Remove Duplicates from Sorted Array [LeetCode]

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...