[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+ ...
随机推荐
- C++——输入、输出和文件
一.C++输入和输出概述 1.1.流和缓冲区 C++程序把输入和输出看作字节流.输入时,程序从输入流中抽取字节:输出时,程序将字节插入到输出流中.对于面相文本的程序,每个字节代表一个字符,更通俗地说, ...
- Bootstrap文本对齐风格
在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑ 左对齐,取值left ☑ 居中对齐,取值center ☑ 右对齐,取值r ...
- MyBatis学习笔记(四) 注解
使用MyBatis注解开发,可以省去类配置文件,简洁方便.但是比较复杂的SQL和动态SQL还是建议书写类配置文件. 注解还是不推荐使用的.只是了解了解!简单的CRUD可以使用注解.简单写写. ...
- 20160805_CentOS6_控制台切换
1. Ctrl + Alt + F1~F6 Ctrl + Alt + F1 是 图形界面(如果装了的话),后面的是 控制台界面 2. 3.
- javascript隐式转换详解
Javascript是web前端开发的必学技术,今天和大家分享的就是javascript的基础知识隐式转换,希望可以帮助大家更好的学习. 转换成布尔类型假 undefined->falSe nu ...
- easyui-datebox 和easyui-datetimebox 设置默认时间当前时间
//显示当前日期 formatterDate = function (date) { var day = date.getDate() > 9 ? date.getDate() : " ...
- flasCC技术点记录
[待测试特性] 一.C接口导出相关 1.重载函数. 2.虚函数. 3.template相关 二.内存相关 1.as直接往c分配的内存写数据. 2.c直接往as对象写数据. 三.C访问AS 1.访问as ...
- mvc伪静态<四> 伪静态后静态页面或者引用的css和图片失效
引用的css和图片失效的解决办法 把样式引用文件的相对路径改成绝对路径就可以了 比如原先的引用路径为:<link href="~/Content/css/style.css" ...
- plot a critical difference diagram , MATLAB code
plot a critical difference diagram , MATLAB code 建立criticaldifference函数 function cd = criticaldiffer ...
- uva 242
242 - Stamps and Envelope Size Time limit: 3.000 seconds Stamps and Envelope Size Philatelists hav ...