[vijos P1112] 小胖的奇偶
第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫。看了很多人的解题都有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] 小胖的奇偶的更多相关文章
- 【带权并查集】【离散化】vijos P1112 小胖的奇偶
每个区间拆成r和l-1两个端点,若之内有偶数个1,则这两个端点对应的前缀的奇偶性必须相同,否则必须相反. 于是可以用带权并查集维护,每个结点储存其与其父节点的奇偶性是否相同,并且在路径压缩以及Unio ...
- Vijos 小胖的奇偶
第一遍做 #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> ...
- Vijos 1144 小胖守皇宫 【树形DP】
小胖守皇宫 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步 ...
- 【动态规划】【最长公共子序列】Vijos P1111 小胖的水果
题目链接: https://vijos.org/p/1111 题目大意: 多组数据,给两个字符串s1,s2,求把s1,s2拆开从前往后合并后最短是多少 apple + peach = appleach ...
- 小胖的奇偶(Viojs1112)题解
原题: 题目描述 huyichen和xuzhenyi在玩一个游戏:他写一个由0和1组成的序列. huyichen选其中的一段(比如第3位到第5位),问他这段里面有奇数个1 还是偶数个1.xuzheny ...
- Vijos 1111 小胖的水果 LCS
描述 xuzhenyi到大同水果店去买水果,但老板huyichen告诉他每次只能买一种,但是xuzhenyi想吃两种,于是在讨价还价之后,huyichen说只要xuzhenyi能把他想要的两种水果合并 ...
- vijos 1110小胖邮递员;bzoj 1210: [HNOI2004]邮递员
Description Smith在P市的邮政局工作,他每天的工作是从邮局出发,到自己所管辖的所有邮筒取信件,然后带回邮局.他所管辖的邮筒非常巧地排成了一个m*n的点阵(点阵中的间距都是相等的).左上 ...
- VijosP1112:小胖的奇偶
描述 huyichen和xuzhenyi在玩一个游戏:他写一个由0和1组成的序列. huyichen选其中的一段(比如第3位到第5位),问他这段里面有奇数个1还是偶数个1.xuzhenyi回答你的问题 ...
- BZOJ 3714: [PA2014]Kuglarz
Description 魔术师的桌子上有n个杯子排成一行,编号为1,2,-,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品.花费c_ij元,魔术师就会告诉你杯子i,i+ ...
随机推荐
- POJ 2418 各种二叉排序树
题意很明确,统计各个字符串所占总串数的百分比,暴力的话肯定超时,看了书上的题解后发现这题主要是用二叉排序树来做,下面附上n种树的代码. 简单的二叉排序树,不作任何优化(C语言版的): #include ...
- mustache.js渲染带事件的模板
http://zccst.iteye.com/blog/2183111 最近遇到这样一个问题,预览内容可点击,问题是通过$.Mustache.render("templateId" ...
- jackson annotations注解详解 (zhuan)
http://blog.csdn.net/sdyy321/article/details/40298081 ************************************** 官方WIKI: ...
- html文本框(input)不保存缓存记录
有的时候js往文本框里赋值,结果在火狐中刷新后,刚才赋的值还在,导致好多麻烦的事情,现总结一下解决办法如下: <input name="bestlovesky" type= ...
- Convention插件 struts零配置
http://blog.csdn.net/spyjava/article/details/13631961系列课程使用 注解:http://www.yiibai.com/struts_2/struts ...
- Python装饰模式实现源码分享
1.一般来说,通过继承可以获得父类的属性,还可以通过重载修改其方法. 2.装饰模式可以不以继承的方式而动态地修改类的方法. 3.装饰模式可以不以继承的方式而返回一个被修改的类. 4.基本实现 程序演示 ...
- Canu FAQ常见问题
链接:Canu FAQ Q: What resources does Canu require for a bacterial genome assembly(细菌基因组组装)? A mammal ...
- MySQL redo lock 死锁问题排查 & 解决过程
版权声明:本文由张青林原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/181 来源:腾云阁 https://www.qclo ...
- 【转】 Live555
Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现了对多种音视频编 ...
- 关于http响应内容压缩的一点小积累。
1.在tomcat的server.xml配置文件中,添加上背景颜色为绿色的配置,服务器就会自动压缩 <Connector port="80" maxHttpHeaderSiz ...