[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+ ...
随机推荐
- 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)
在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...
- drupal里面的ajax最粗浅的理解-流程
1, form里面的ajax所在地表单元素有一个事件,激发system/ajax,相应的有ajax_form_callback(), 会把被改变的元素值传到form_state[values]中, ...
- 【转】 Android自定义捕获Application全局异常
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- Docker 使用指南 (六)—— 使用 Docker 部署 Django 容器栈
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98 来源:腾云阁 https://www.qclou ...
- android AutoCompleteTextView和Spinner选中项加亮
package com.example.spinnerexample; import java.util.ArrayList; import java.util.List; import androi ...
- Java虚拟机类加载机制——案例分析
转载: Java虚拟机类加载机制--案例分析 在<Java虚拟机类加载机制>一文中详细阐述了类加载的过程,并举了几个例子进行了简要分析,在文章的最后留了一个悬念给各位,这里来揭开这个悬 ...
- 【LINUX】Linux学习小结
****xargs命令**** 当需要将参数列表转换成小块分段传递给其他命令时,可以使用xargs命令.栗子如下: 若想在启动lampp之后用kill方式杀掉全部的进程就可以用下面的命令: ps -e ...
- Tomcat Manager用户配置详解
Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Ma ...
- Android客户端向服务器端发送数据的流程(1)
原理: android客户端通过使用org.apache.http.impl.client.DefaultHttpClient类来发送数据; 方法介绍: HttpClient是android中提供的一 ...
- C++ 如何重复利用一个内存地址块
#include<iostream> using namespace std; ; }; int main(int argv ,char * args[]) { double *p1,*p ...