第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees
题意:
链接:https://www.nowcoder.com/acm/contest/119/A
来源:牛客网
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
One day the tree manager wants to play a game with you. There are N trees lining up in a straight road. The beauty of a set of trees is defined as the bitwise XOR sum of the heights of these trees. The game will last for M rounds, and each time he will tell you an interval [Li,Ri] and its beauty. However, he mixed some fake messages with the correct ones. Your task is to find the messages that cannot logically correspond to its former correct messages. Otherwise you’ll think the message is correct.
输入描述:
The first line contains two integer N and M(1≤N,M≤10
5
), the number of trees and the rounds of game.
Then M lines followed, in each line are three integer L, R and k(1≤L≤R≤N,0≤k≤10
9
), indicating that the beauty of [L
i
,R
i
] is k.
输出描述:
If the i-th message is wrong, then print i in a single line.
If there is no mistake, print -1.
输入例子:
3 4
1 3 6
1 2 5
3 3 10000
3 3 3
输出例子:
3
-->
输入
3 4
1 3 6
1 2 5
3 3 10000
3 3 3
输出
3
说明
You can infer from the first two messages that the height of the third tree is 3.
题意:
有nn个数,每次给你一个信息l,r,kl,r,k,代表al xor al+1... xor ar=kal xor al+1... xor ar=k,问你哪些信息是错误的,如果xx信息和yy信息只可以xx对yy错或者xx错yy对,那么认为先给出的信息是对的。
思路:
并查集路径压缩。 记aa数组的前缀异或和是sumsum,那么信息l,r,kl,r,k实际上就是sumr xor suml−1=ksumr xor suml−1=k,如果已知sumr xor sumx=k1,suml−1 xor sumx=k2sumr xor sumx=k1,suml−1 xor sumx=k2,那么只要判断k1 xor k2k1 xor k2是否等于kk即可,否则设sumr xor sumx=k1,suml−1 xor sumy=k2sumr xor sumx=k1,suml−1 xor sumy=k2,那么有sumx xor sumy=k1 xor k2 xor k,sumx xor sumy=k1 xor k2 xor k,可以合并x,y,x,y,并设sum[x] xor sum[y]=k1 xor k2 xor ksum[x] xor sum[y]=k1 xor k2 xor k, 然后直接用路径压缩就好了。
#include<bits/stdc++.h>
using namespace std; const int N = 1e5 + ;
const int INF = 1e9; int f[N], a[N]; int father(int x)
{
if (x != f[x])
{
int tmp = father(f[x]);
a[x] ^= a[f[x]];
f[x] = tmp;
}
return f[x];
} int main()
{
int n, m; scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
f[i] = i;
a[i] = ;
}
bool flag = ;
for (int i = ; i <= m; i++)
{
int l, r, k; scanf("%d%d%d", &l, &r, &k);
--l;
int fl = father(l), fr = father(r);
if (fl == fr)
{
if ((a[l] ^ a[r]) != k)
{
printf("%d\n", i);
flag = ;
}
}
else
{
f[fr] = fl;
a[fr] = k ^ a[l] ^ a[r];
}
}
if (flag) printf("-1");
return ;
}
第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees的更多相关文章
- 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest
链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...
- 第十四届华中科技大学程序设计竞赛--J Various Tree
链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
随机推荐
- 关于IIntelliJ IDEA(2017)安装和破解
一.下载并安装, IntelliJ IDEA的官网:https://www.jetbrains.com 二.破解. 百度下载一个 JetbrainsCrack-2.6.2.jar 破解补丁.放在你的安 ...
- 虚拟机开启Linux时出现“我以复制虚拟机”、“我已移动虚拟机”
当出现标题的情况时,并且网络出现状况时,可以尝试一下解决办法 首先用ifconfig -a命令调出现在的网卡驱动的名称和HWaddr地址,然后再编辑/etc/sysconfig/networking/ ...
- Oralce查询后修改数据,弹窗报提示these query result are not updateable,include the ROWID to get updateable
select t.*, (select a.ANNEXNAME from base_annex a where a.id = t.closeFile) closeFileName, (select a ...
- 用intellij idea 写第一个Java程序
Java小白,还不怎么会eclipse,只会在命令行用javac编译并java运行编译后的类. 英文还不好orz 发现创建项目后,能build但就是不能run... 找了半天教程没找着,去官网溜了一下 ...
- iOS系统声音服务(System Sound Services)
系统声音服务(System Sound Services)提供了一个接口,用于播放不超过30秒的声音.它支持的文件格式有限,具体地说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. ...
- php如何查看扩展是否开启
php如何查看扩展是否开启 一.总结 一句话总结:php -m 1.查看php已安装扩展命令 ? php -m 2.phpinfo();这是最常用的方法,但那么多扩展一时还真不太好找.? 3.exte ...
- WCF实现REST服务
REST 表述性状态转移(Representational State Transfer,REST),不是一种标准,而是一种软件架构风格. 基于REST的服务与基于SOAP的服务相比,性能.效率和易用 ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- 51nod 1128 二分
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1128 1128 正整数分组 V2 基准时间限制:1 秒 空间限制:131 ...
- Spring_总结_04_高级配置(四)_bean的作用域
一.前言 本文承接上一节:Spring_总结_04_高级配置(三)之处理歧义 1.单例bean Spring应用上下文中所有的bean默认都是单例的.也就是说,不管一个bean被注入到其他bean多少 ...