锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A
锟斤拷源锟斤拷牛锟斤拷锟斤拷

锟斤拷目锟斤拷锟斤拷

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.
示锟斤拷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.

r[a]记录的是a与其父亲节点的异或值
通过并查集合并的过程同时更新路径上的r值
令fa,fb分别是a,b,的父亲节点
我们知道r[a] = a^fa,r[b]=b^fa,a^b=c
那么在合并fa,fb的时候,令fb为fa的父节点
那么就有r[fa]=fa^fb
而r[a]=a^fa,所以fa=a^r[a]
同理fb=b^r[b]
所以r[fa] = r[a]^r[b]^a^b = r[a]^r[b]^c
当然对于一个节点合并问题不好解决,此时可以添加一一个新节点n作为根节点,使得n的值为0,那么任何值与0的异或都是本身
这样一来合并问题就解决了
 1 /**
2 先要知道加权并查集:https://blog.csdn.net/qq_32944513/article/details/80218138
3 然后 这里
4 设 P[i] 为 0~i 的异或值, 则给了 (L,R)=k 相当就是知道了 P[r]^P[l-1]=k;
5 这样转化一下 就和 LA4487 差不多了。
6 **/
7 #include<iostream>
8 #include<stdio.h>
9 #include<string.h>
10 #include<vector>
11 #include<map>
12 #include<algorithm>
13 using namespace std;
14 typedef long long int ll;
15 const int maxn = 100000;
16 const int inf = 1e9;
17 int n,m,fa[maxn+5],r[maxn+5],ans[maxn+5];
18 int findset(int x)
19 {
20 if(fa[x] == x) return x;
21 int fx = fa[x];
22 fa[x] = findset(fa[x]);
23 r[x]^=r[fx];
24 return fa[x];
25 }
26 int Merge(int u,int v,int z)
27 {
28 int fx = findset(u), fy = findset(v);
29 if(fx==fy)
30 {
31 return (r[u]^r[v])==z;
32 }
33 fa[fx] = fy;
34 r[fx]=r[u]^r[v]^z;
35 return true;
36 }
37 int main()
38 {
39 int num = 0;
40 scanf("%d %d",&n,&m);
41 for(int i=0;i<=n;i++) fa[i] = i,r[i] = 0;
42 for(int i=1; i<=m; i++)
43 {
44 int l,r,w;
45 scanf("%d %d %d",&l,&r,&w);
46 if(!Merge(l-1,r,w)) ans[++num] = i;
47 }
48 for(int i=1;i<=num;i++) printf("%d\n",ans[i]);
49 if(num==0) printf("-1\n");
50 return 0;
51 }

官方题解:

 1 /*
2 考虑给定的区间[L,R]中的数和美丽值k的二进制分解。对于第j位,如果k在这一位是1,
3 说明[L,R]之间在这一位为1的数有奇数个,接下来令f[i][j]表示第j位为1的数在前i个数里有多少个,
4 那么这个条件也就是f[l-1][j]与f[r][[j]奇偶性不同;若k在这一位为0,
5 说明f[l-1][j]与f[r][j]奇偶性相同。注意上述条件都是等价的,
6 也就是说如果所有的j位f[l-1][j]和先前确定的与f[r][j]的关系不矛盾的话,
7 就等价于题目所给的合乎逻辑。这个过程可以用并查集来完成。
8 对于每一个前缀和开一个实点一个虚点,如果两个前缀和奇偶性相同,
9 就实对实虚对虚相连;如果奇偶性不同,就各自实对虚相连。
10 这样就能保证与任意节点奇偶性偶数次不同相当于奇偶性相同。然后判断的时候可以直接查询。
11 */
12 #include<cstdio>
13 #include<cstring>
14 #include<iostream>
15
16 using namespace std;
17
18 const int maxn=1e5+10;
19
20 int n,m,l,r,k,fl,cnt;
21 int f[maxn*2][32];
22
23 int find(int t,int k)
24 {
25 int p=t,r;
26 while(t!=f[t][k])t=f[t][k];
27 while(p!=t)r=f[p][k],f[p][k]=t,p=r;
28 return t;
29 }
30
31 void unio(int l,int r,int j)
32 {
33 if(find(l,j)!=find(r,j))
34 f[find(l,j)][j]=find(r,j);
35 }
36
37 int main()
38 {
39 scanf("%d%d",&n,&m);
40 for(int i=0; i<=n*2+1; i++)
41 for(int j=0; j<=30; j++)
42 f[i][j]=i;
43
44 for(int i=1; i<=m; i++)
45 {
46 fl=0;
47 scanf("%d%d%d",&l,&r,&k);
48 l--;
49
50 for(int j=0; j<=30; j++)
51 if((k&(1LL<<j))!=0&&(find(l,j)==find(r,j)||find(l+n+1,j)==find(r+n+1,j)))
52 {
53 printf("%d\n",i);
54 fl=1;
55 break;
56 }
57 else if((k&(1LL<<j))==0&&(find(l,j)==find(n+r+1,j)||find(r,j)==find(n+l+1,j)))
58 {
59 printf("%d\n",i);
60 fl=1;
61 break;
62 }
63 if(fl)continue;
64
65 cnt++;
66 for(int j=0; j<=30; j++)
67 if((k&(1LL<<j))!=0)unio(l,r+n+1,j),unio(r,l+n+1,j);
68 else unio(l,r,j),unio(l+n+1,r+n+1,j);
69 }
70
71 if(cnt==m)printf("-1");
72
73 return 0;
74 }

参考博客:戳这里

牛客网-Beauty of Trees 【加权并查集】的更多相关文章

  1. 牛客网-Beautiful Land 【01背包 + 思维】

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Now HUST got a big land whose capacity is C to p ...

  2. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

  3. 牛客网《BAT面试算法精品课》学习笔记

    目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...

  4. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

  5. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  6. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  7. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

  8. 牛客网华为机试题之Python解法

    牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...

  9. 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)

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

随机推荐

  1. Poj-P1088题解【动态规划/记忆化搜索】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...

  2. RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ

    目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...

  3. MongoDB 总结

    目录 1. 逻辑结构 2. 安装部署 2.1 系统准备 2.2 mongodb安装 2.2.1 创建所需用户和组 2.2.2 创建mongodb所需目录结构 2.2.3 上传并解压软件到指定位置 2. ...

  4. 【图像处理】RGB Bayer Color分析

    Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示,由一半的G,1/4的R,1/4的B组 ...

  5. 【Soul网关探秘】http数据同步-Admin通知前处理

    引言 本篇开始研究 Soul 网关 http 数据同步,将分为三篇进行分析: <Admin通知前处理> <变更通知机制> <Bootstrap处理变更通知> 希望三 ...

  6. 回滚原理 Since database connections are thread-local, this is thread-safe.

    mysql django 实践: django @transaction.atomic 机制分析  1.数据库清空表Tab 2.请求django-view        @transaction.at ...

  7. MSSQL 注入笔记

    前置知识: 登录名:登录sql server服务器的用户,而不是操作"数据库用户名". 固定服务器角色:就是上面登录名所属的权限组.其中重要的就是"sysadmin&qu ...

  8. 虚函数表-C++多态的实现原理

    目录 1.说明 2.虚函数表 3.代码示例 参考:http://c.biancheng.net/view/267.html 1.说明 我们都知道多态指的是父类的指针在运行中指向子类,那么它的实现原理是 ...

  9. 如何在windows下切换node版本

    安装nvm 最近的项目中,一个是用vue项目开发,一个是使用react开发,但是ant design pro使用了umi框架,所需要的node版本>10.0.0,vue那个项目中又不兼容node ...

  10. 二:SpringBoot-配置Log4j2,实现不同环境日志打印

    SpringBoot-配置Log4j2,实现不同环境日志打印 日志打印之外观模式 1.日志配置 2.Log4j2的配置文件 3.简单的测试程序 日志打印之外观模式 每一种日志框架都有自己单独的API, ...