第十四届华中科技大学程序设计竞赛决赛同步赛 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 ...
随机推荐
- Idea根据表自动生成实体
Idea根据表自动生成实体: 首先说下这种方式有个缺点,就是如果表里面有日期.时间类型,那么需要手动的设置映射类型 第一步:在Idea中配置好数据库: 在Idea窗口右边,点击Database按钮 配 ...
- QT 使用QSettings读写ini配置文件
利用Qsettings包一个类 RWIniFile, writeIni方法写文件, readIni方法读文件 rwinifile.h #ifndef RWINIFILE_H #define RWINI ...
- bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...
- 啥是ETL、ELT
ETL就是Extract.Transfrom.Load即抽取.转换.加载三个英文单词首字母的集合.抽取:就是从源系统抽取需要的数据,这些源系统可以是同构也可以是异构的:比如源系统可能是Excel电子表 ...
- 小谈CSS定位
定义和用法 position 属性规定元素的定位类型. 说明 这个属性定义建立元素布局所用的定位机制.任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身是什么类型.相对定位元素会 ...
- TY_GASPX SQL
SELECT company_name,cp_province,cp_city,cp_area,worktype_name,SUM(allpass) as allCount FROM [dbo].[E ...
- poj1463 树形dp
树形dp裸题,不过输入是真的恶心,要字符串读入考虑数字大于等于10的情况 dp[i][j]表示i的子树在j状态的最小的边集覆盖,j为0表示不选当前结点,1表示选 转移方程(u->x是u的所有子节 ...
- App Store下载Mac应用失败的解决办法
1. 更换DNS服务器 国内可以用alidns: 223.5.5.5 223.6.6.6 也可以用电信的: 114.114.114.114 国外的可以考虑Google 8.8.8.8 8.8.4.4 ...
- String随笔
1.古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:,请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想.程序流程图.源代码.结果截图. 设计思想:1)定义一个String类型 ...
- [转载]]Java开发如何在线打开Word文件
此方案使用了PageOffice产品实现在线打开Word文档: 1. 首先从PageOffice官网下载产品开发包,http://www.zhuozhengsoft.com/dowm/ ,下载Page ...