第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫。看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢?hash也很久没写了,导致自己测试的时候有两个点没过是因为hash弄错了。

我的程序里f[x]= same[x] (x<=max)

= diff[x-max] (x>max)

总算,最后还是通过了。又是一道想不到思路就写不出的题。据说这题和食物链很像,那么食物链就是我的下一个目标~总感觉并查集是有点抽象的,我还没想到它怎么和树形结构具体联系起来。

BTW,vijos的数据挺弱的…

program vijos_p1112;
const BLOCK=;
var hash,f:array[..] of longint;
n,m,i,a,b,t:longint;
s:string;
function haha(x:longint):longint;
var t:longint;
begin
t:=x mod ;
while (hash[t]<>-) and (hash[t]<>x) do inc(t);
exit(t);
end; function find(x:longint):longint;
begin
if f[x]=x then exit(x) else exit(find(f[x]));
end; procedure union(x,y:longint);
var fx,fy:longint;
begin
fx:=find(x);
fy:=find(y);
if fx<>fy then f[fx]:=fy;
end; begin
readln(n);
readln(m);
if m= then
begin
writeln();
halt;
end;
for i:= to do
begin
f[i]:=i;
hash[i]:=-;
end;
for i:= to m do
begin
readln(s);
t:=pos(' ',s);
val(copy(s,,t-),a);
delete(s,,t);
t:=pos(' ',s);
val(copy(s,,t-),b);
delete(s,,t);
if s='even' then
begin
if find(haha(a-))=find(haha(b+BLOCK)) then
begin
writeln(i-);
halt;
end
else
begin
union(haha(a-),haha(b));
union(haha(a-+BLOCK),haha(b+BLOCK));
end;
end
else
begin
if find(haha(a-))=find(haha(b)) then
begin
writeln(i-);
halt;
end
else
begin
union(haha(a-),haha(b+BLOCK));
union(haha(a-+BLOCK),haha(b));
end;
end;
end;
writeln(m);
end.

小胖的奇偶

测试数据 #0: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1364 KiB, score = 10
测试数据 #4: Accepted, time = 15 ms, mem = 1364 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 1360 KiB, score = 10
测试数据 #6: Accepted, time = 15 ms, mem = 1368 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 1364 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 1364 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 1364 KiB, score = 10

话说把奇怪的写失败了的东西也扔上来吧0 0

program vijos_p1112;
var same,diff:array[..] of integer;
haha,f:array[..] of longint;
n,m,i,j,a,b,t:longint;
s:string;
function hash(x:longint):longint;
var t:longint;
begin
t:=((x mod )*(x mod )) mod +;
while (haha[t]<>) and (haha[t]<>x) do
inc(t);
haha[t]:=x;
exit(t);
end; {function find(x:longint):longint; same &diff de find ya fen kai
begin
if x=f[x] then exit(x) else exit(find(f[x]));
end; } function sfind(x:longint):longint;
begin
if hash(x)=same[hash(x)] then exit(hash(x)) else exit(sfind(same[hash(x)]));
end; function dfind(x:longint):longint;
begin
if hash(x)=diff[hash(x)] then exit(hash(x)) else exit(dfind(diff[hash(x)]));
end; procedure union(x,y:longint;a,b:word);
var fx,fy:longint;
begin
if a= then fx:=sfind(x) else fx:=dfind(x);
if b= then fy:=sfind(y) else fy:=dfind(y);
if (fx<>fy) then f[fx]:=fy;
end; function pd:boolean;
var buzhidao:integer;
begin
if s='even' then
begin
if sfind(same[hash(a-)])=dfind(diff[hash(b)]) then
begin
writeln(i-);
halt;
end;
end
else
begin
if sfind(same[hash(a-)])=sfind(same[hash(b)]) then
begin
writeln(i-);
halt;
end;
end;
pd:=true;
end; begin
readln(n);
readln(m);
for i:= to m* do
begin
same[i]:=i;
diff[i]:=i+;
end;
for i:= to m do
begin
readln(s);
t:=pos(' ',s);
val(copy(s,,t-),a);
delete(s,,t);
t:=pos(' ',s);
val(copy(s,,t-),b);
delete(s,,t);
if s='even' then
begin
if pd then
begin
union(same[hash(a-)],same[hash(b)],,);
union(diff[hash(a-)],diff[hash(b)],,);
end;
end
else
begin
if pd then
begin
union(same[hash(a-)],diff[hash(b)],,);
union(diff[hash(a-)],same[hash(b)],,);
end;
end;
end;
end.

小胖的奇偶-demo

[vijos P1112] 小胖的奇偶的更多相关文章

  1. 【带权并查集】【离散化】vijos P1112 小胖的奇偶

    每个区间拆成r和l-1两个端点,若之内有偶数个1,则这两个端点对应的前缀的奇偶性必须相同,否则必须相反. 于是可以用带权并查集维护,每个结点储存其与其父节点的奇偶性是否相同,并且在路径压缩以及Unio ...

  2. Vijos 小胖的奇偶

    第一遍做 #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> ...

  3. Vijos 1144 小胖守皇宫 【树形DP】

    小胖守皇宫 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步 ...

  4. 【动态规划】【最长公共子序列】Vijos P1111 小胖的水果

    题目链接: https://vijos.org/p/1111 题目大意: 多组数据,给两个字符串s1,s2,求把s1,s2拆开从前往后合并后最短是多少 apple + peach = appleach ...

  5. 小胖的奇偶(Viojs1112)题解

    原题: 题目描述 huyichen和xuzhenyi在玩一个游戏:他写一个由0和1组成的序列. huyichen选其中的一段(比如第3位到第5位),问他这段里面有奇数个1 还是偶数个1.xuzheny ...

  6. Vijos 1111 小胖的水果 LCS

    描述 xuzhenyi到大同水果店去买水果,但老板huyichen告诉他每次只能买一种,但是xuzhenyi想吃两种,于是在讨价还价之后,huyichen说只要xuzhenyi能把他想要的两种水果合并 ...

  7. vijos 1110小胖邮递员;bzoj 1210: [HNOI2004]邮递员

    Description Smith在P市的邮政局工作,他每天的工作是从邮局出发,到自己所管辖的所有邮筒取信件,然后带回邮局.他所管辖的邮筒非常巧地排成了一个m*n的点阵(点阵中的间距都是相等的).左上 ...

  8. VijosP1112:小胖的奇偶

    描述 huyichen和xuzhenyi在玩一个游戏:他写一个由0和1组成的序列. huyichen选其中的一段(比如第3位到第5位),问他这段里面有奇数个1还是偶数个1.xuzhenyi回答你的问题 ...

  9. BZOJ 3714: [PA2014]Kuglarz

    Description 魔术师的桌子上有n个杯子排成一行,编号为1,2,-,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品.花费c_ij元,魔术师就会告诉你杯子i,i+ ...

随机推荐

  1. Sqlserver_insert语法

    1. INSERT INTO SELECT 通过 SQL,您可以从一个表复制信息到另一个表.   INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中. 我们可 ...

  2. jQuery中对属性的增删改查

    获取元素的属性 $('input').attr('type') .attr()  可以获取和设置自定义属性 .prop()  只能获取和设置固有属性 在设置属性值时 建议不要修改type属性,有的浏览 ...

  3. about opencl

    Platform:LG G3,Adreno 330 1. 8M(3264x2448) memmap方式读入时间24ms,读出时间12ms,时间与内存大小基本成线性关系.使用memmap 与 memco ...

  4. requestAnimationFrame兼容性扩展

    /** * requestAnimationFrame兼容性扩展,两方面工作: * 1.把各浏览器前缀进行统一 * 2.在浏览器没有requestAnimationFrame方法时将其指向setTim ...

  5. phalcon: Profiling分析 profilter / Plugin结合,dispatcher调度控制器 监听sql执行日志

    个人觉得profilter 跟 logger 功能差不多,logger的功能在于写入,profilter功能在于sql后及时显示分析.都是对sql执行的的分析:一个是写入log文件,一个是直接在页面展 ...

  6. The Network Adapter could not establish the connection问题研究

    最近一个项目会报上述错误,但也不是经常发生,所以很难跟踪,影响不是很大,但每次看到日志中这个错误就会不舒服,还是要想办法解决才是. 错误提示信息很明确是网络适配器不能创建连接. 查了很多资料,并且Or ...

  7. eclipse配置tomcat,访问http://localhost:8080出现404错误

     问题:通过eclipse来启动tomcat会碰到“访问http://localhost:8080出现404错误”这样的问题,需要在eclipse中进行一系列的设置才行. 解决:打开eclipse的s ...

  8. virtualbox macosx10.9改变分辨率方法

    VBoxManage setextradata "osx10.9" VBoxInternal2/EfiGopMode 5 VBoxManage setextradata " ...

  9. [Hadoop 周边] Hadoop和大数据:60款顶级大数据开源工具(2015-10-27)【转】

    说到处理大数据的工具,普通的开源解决方案(尤其是Apache Hadoop)堪称中流砥柱.弗雷斯特调研公司的分析师Mike Gualtieri最近预测,在接下来几年,“100%的大公司”会采用Hado ...

  10. 用过的正则(更新ing)

    http://www.debuggex.com/   这个很好用20120912 //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6} ...