Bitwise Exclusive-OR Sequence
Bitwise Exclusive-OR Sequence
题意:
有n个点, m个关系 每个关系 a b c 表示点a的值 异或 点b的值,为c
分析
首先可能是多个图, 对于每个图中的每一位, 取0 或者取1 都是可以确定图上的其他数字取0 或者取1 。 所以我们可以去枚举取0 或者取1 ,来取个min 得到结果。
所以这里我们用并查集, 最大数为 2的30次, 所以我们对于每一位都做一个并查集, 就是30个并查集。 f[a] 表示当前位置取1 f[a+n] 表示当前位取0
这样就可以枚举区间了。
代码
#include <bits/stdc++.h>using namespace std;
typedef long long LL;const int N = 2e5+10;LL n, m, fa[N], sz[N];
LL a[N];struct Node
{LL a, b;LL val;
}num[N];LL find(LL x)
{if(fa[x] != x) fa[x] = find(fa[x]);return fa[x];
}void init()
{for(LL i = 1; i <= n; i ++){fa[i] = i, sz[i] = 1;fa[i+n] = i+n, sz[i+n] = 0;}
}void merge(LL a, LL b)
{sz[a] += sz[b];fa[b] = a;
}int main()
{cin >> n >> m;for(int i = 1; i <= m; i ++){scanf("%lld%lld%lld", &num[i].a, &num[i].b, &num[i].val);}LL res = 0;for(int i = 0; i < 30; i ++){init(); // 每一次做并查集都要初始化!for(int j = 1; j <= m; j ++){int aa = find(num[j].a);int bb = find(num[j].b);int aaa = find(num[j].a + n);int bbb = find(num[j].b + n);if((num[j].val >> i) & 1 ) // 不同为1 ,一个取0一个取1{if(aa == bb){cout << -1 << endl;return 0;}if(aa == bbb) continue;merge(aa, bbb);merge(bb, aaa);}else // 相同为0, 都取1 或者都取0{if(aa == bbb ){cout << -1 << endl;return 0;}if(aa == bb) continue;merge(bb, aa);merge(bbb, aaa);}}for(int j = 1; j <= n; j ++) // 对于每个数字计算结果{res += (LL)min(sz[find(j)], sz[find(j+n)]) * (1 << i);sz[find(j)] = 0;sz[find(j+n)] = 0;}}cout << res << endl;return 0;
}
/*
3 3
1 2 1
2 3 1
1 3 1 */
Bitwise Exclusive-OR Sequence相关推荐
- C语言位运算 Bitwise Operator
Bitwise Operators in C Programming Language When I first learn C, I found it's hard to understand th ...
- [codeforces 339]D. Xenia and Bit Operations
[codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...
- python英文翻译-Python运算符-局部英文翻译版
Operators(运算符) are the constructs which can manipulate the value of operands. Consider the expressio ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
zoj4047 A Live Love DreamGrid is playing the music game Live Love. He has just finished a song consi ...
- halcon11用于C++的HTuple.h头文件,纯手添中文翻译!
halcon11用于C++的HTuple.h头文件,纯手添中文翻译!乐于分享,请勿在非同意下转载! /************************************************* ...
- Summer Training day6 coseforces339D 线段树、位操作
D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...
- lua 5.3 英文手册
http://www.lua.org/manual/5.3/manual.html lua 5.3 英文手册 Lua Lua 5.3 Reference Manual by Roberto Ierus ...
- Xor Sum 2(位运算)
D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...
- python点操作符语法,Python 语法之操作符和表达式
转载信息:http://blog.chinaunix.net/uid-20393955-id-345384.html File information 2009-10-23 磁针石:xurongzho ...
- 写给自己的python基础
建议刚开始学习直接使用ANACONDA https://www.anaconda.com/download/ 配置python环境变量 https://docs.python.org/3.6/usin ...
最新文章
- python argparse_Python 命令行之旅:初探 argparse
- azure存储压力测试
- IIS 7 php 7.0 部署WE MALL
- 【声学基础】概述——辐射
- 深入理解javascript作用域系列第三篇
- Precedence Problems of C Operators
- 宗地图绘制要求和规范_国内土地市场宗地图的绘制标准制图规范-【领仕网】...
- 2020年电工(初级)考试及电工(初级)考试平台
- 无人机成测绘界“全能战士”
- signature=9b2caa13f2468eba05d2d57d9a88606d,【音响聚焦】顶级Hi-End音响发烧器材介绍(功放篇)...
- Crack Theory for IPA
- [论文]欠驱动水下机器人的平面轨迹规划与跟踪控制设计
- 使用 In-Trangle Test 检测极点
- 【Excel】选择性粘贴
- css图片精灵定位_CSS精灵图片(CSS sprite)使用心得(转)
- 你旁边的电闸有辐射或者其它危害吗?
- java 狗带风波_狗狗风波
- 编译原理程门立雪再回首时雪拥蓝关
- 【Python】基金/股票 最大回撤率计算与绘图详解(附源码和数据)
- 此计算机未配置为允许委派用户凭据,Kerberos 协议转换和受限委派