A - Beauty of Trees

题意:

链接:https://www.nowcoder.com/acm/contest/119/A
来源:牛客网

Beauty of Trees
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
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

-->

示例1

输入

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的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  2. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  3. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

  4. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  5. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  7. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  8. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  9. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

随机推荐

  1. 【P2325】王室联邦(树的遍历+贪心)

    在肖明 #神#的推荐下,我尝试了这个题,一开始想的是暴力枚举所有的点,然后bfs层数,试着和肖明 #神#说了这种方法之后, #神#轻蔑的一笑,说这不就是一个贪心么,你只需要先建树,然后从底下向上遍历, ...

  2. 偏远小渔村选手的noip2017游记

    这次noip估计是我初中最后一次比赛了,毕竟初三狗还要准备中考,要是中考挂了就GG了. 在最终成绩的榜上,我看到我成绩400,非常意外(你们这群大佬赛前天天奶我,还好不是毒奶),更意外的是全省竟然只有 ...

  3. MVC 嵌套页面Html.Partial

    return View()相关简介 在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewResultBase 同时它还有个兄弟PartialViewResult.一个 ...

  4. Https通信工具类

    记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; im ...

  5. hdoj1006--Tick and Tick

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  6. js轮训

    Later.js,一个独立的JavaScript类库,提供了循环事件触发的高级应用,可以为项目替换以上两种方法. 通常,我们要完成复杂的时间预定,需要大量的if...else语句.Later.js提供 ...

  7. 冒泡排序的PHP实现 Bubble Sort

    冒泡排序Bubble Sort的PHP实现.代码中函数说明: out_arr,用于将数组输出成一个字符串,以便查看 bubblesort,第一种实现方案,从后往前依次选出需要的值,这里是较大的 bub ...

  8. Python压缩脚本编辑

    这真是一点小问题,搞死人了.主要还是两个问题, 1WinRAR,这要配置到环境变量里去.不然无法实现功能. 2 其次就是转义   r'D:\FISRT' 3  source = [r'D:\ONE'] ...

  9. Handsontable 的数据保存(增删改查+导出excel)

    项目用到handsontable 插件 根据官网 API写的handsontable初始化, 数据展示, ajax请求, 参数封装, Controller参数接受 全局容器 var AllData = ...

  10. Pdf 解密后复制文字乱码

    1.安装cajviewer 这个工具 2.用CAJviewer打开pdf文档 3.选择图像4.点文字识别,这时候就弹窗一个框,里面是可复制的文本,而且准确率比较高