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. C++逗号表达式

    c++中,逗号表达式的结果是最右边表达式的值

  2. IntelliJ Idea 快捷键精选

    Alt+Enter,导入包,自动修正Ctrl+F,查找文本Alt+Up/Down,上/下移一行:在方法间快速移动定位:与光标位置有关Alt+Shift+Up,多次up可选中一行Ctrl+X,剪切行Ct ...

  3. SQL题

    1.取出sql表中第31到40的记录(以自动增长ID为主键) sql server方案: select top 10 * from t where id not in (select top 30 i ...

  4. mybatis 中if标签判断boolean 的写法。

    mybatis 的if 比较标签在比较数值时可以这样写: <if test="value=0"> </if> 在比较字符串时可以这么写: <if te ...

  5. ajax01简介

    (Asynchronous JavaScript and XML)Ajax :异步 JavaScript 和 XML,一种允许浏览器和服务器通信进行少量数据交换而无需重新加载整个网页,以实现更新部分网 ...

  6. hibernate学习(2)

    1 实体类编写规则 2 hibernate主键生成策略 3实体类操作 (1)crud操作 (2)实体对象状态 4 hibernate的一级缓存 5 hibernate事务操作 (1)事务代码规则写法 ...

  7. release与debug的区别

    http://www.cnblogs.com/JemBai/archive/2009/01/13/1374805.html

  8. TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议【转】

    转自:http://blog.csdn.net/goodboy1881/article/details/668556 把这三个协议放到一起学习是因为这三个协议处于同一层(网际层),ARP协议用来找到目 ...

  9. Switching from Redhat Linux to Oracle Linux in about 5,000 easy steps

    Wayback When I remember being at Oracle Open World when Larry Ellison unveiled Oracle Enterprise Lin ...

  10. git常用别名设置,保存一份

    git配置别名设置,保存一份 若git  config  --global  xxxx 设置,则文件一般在 C:\Users\Administrator\.gitconfig [alias] lg = ...