将每个数二进制表示下的每一位分开考虑,位与位之间不会互相影响,我们对有关系的两个点建图,建好后对每一位根据异或关系染色,(颜色的确定可以根据异或运算的自反性得到 即

A xor B xor B =A)如果染色出现矛盾,就可以确定该异或关系必然不能产生合法序列,直接输出-1即可,在染色种统1的个数,因为是异或操作,所以图中染色的0和1的位置可以互换,

所以我们再统计一个该联通块中点的总数,用总数减去1的个数就可以得到0的个数,其实这里的0和1表示的是图中两类相斥点的数量,你需要贪心的选择在数量较少的那一类填1,以使得序列的和最小。注意对于每一位都有可能出现多个不连通的联通分量,因为每个联通分量合法性相互之间不会影响,所以我们可以取每个联通分量的较小的那一类的数量然后累加。

这个是#define int long long     在超时的边缘

这个是手开long long

ACcode

#include <iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<unordered_map>
#define int long long
#define endl '\n'
#define lowbit(x) (x &-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
void inline TLE() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
const int N = 2e5 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long LL;int e[2 * N], w[2 * N], ne[2 * N], h[N], cnt;
int color[31][N];void add(int a, int b, int c) {e[cnt] = b, w[cnt] = c, ne[cnt] = h[a], h[a] = cnt++;
}
int sum = 0;
int dfs(int u, int f, int itcolor)
{sum++;color[f][u] = itcolor;//cout << itcolor << endl;int res = itcolor;for (int i = h[u];~i;i = ne[i]) {int j = e[i];if (color[f][j] == -1) {res += dfs(j, f, ((w[i] >> f)&1) ^ itcolor);}else if (color[f][j] != ((w[i] >> f)&1) ^ itcolor) {cout << "-1" << endl;exit(0);}}return res;
}signed main()
{TLE();memset(color, -1, sizeof color);memset(h, -1, sizeof h);int n, m;cin >> n >> m;while (m--){int a, b, c;cin >> a >> b >> c;add(a, b, c);add(b, a, c);}int p = 1;int  res = 0;for (int i = 0;i < 31;i++){for (int j = 1;j <= n;j++)if (color[i][j] == -1){sum = 0;int t = dfs(j, i, 1);res +=p* min(t, sum - t);//cout <<sum<<" " <<t << endl;}p <<= 1LL;}cout << res << endl;return 0;
}

B - Bitwise Exclusive-OR Sequence相关推荐

  1. C语言位运算 Bitwise Operator

    Bitwise Operators in C Programming Language When I first learn C, I found it's hard to understand th ...

  2. [codeforces 339]D. Xenia and Bit Operations

    [codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...

  3. python英文翻译-Python运算符-局部英文翻译版

    Operators(运算符) are the constructs which can manipulate the value of operands. Consider the expressio ...

  4. 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 ...

  5. halcon11用于C++的HTuple.h头文件,纯手添中文翻译!

    halcon11用于C++的HTuple.h头文件,纯手添中文翻译!乐于分享,请勿在非同意下转载! /************************************************* ...

  6. Summer Training day6 coseforces339D 线段树、位操作

    D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. lua 5.3 英文手册

    http://www.lua.org/manual/5.3/manual.html lua 5.3 英文手册 Lua Lua 5.3 Reference Manual by Roberto Ierus ...

  8. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  9. python点操作符语法,Python 语法之操作符和表达式

    转载信息:http://blog.chinaunix.net/uid-20393955-id-345384.html File information 2009-10-23 磁针石:xurongzho ...

  10. 写给自己的python基础

    建议刚开始学习直接使用ANACONDA https://www.anaconda.com/download/ 配置python环境变量 https://docs.python.org/3.6/usin ...

最新文章

  1. 浙工大计算机类包含哪些专业,浙江工业大学计算机类专业培养计划
  2. Re:CMM和RUP、XP的关系是什么?
  3. VTK:等值面采样用法实战
  4. typeof instanceof
  5. tyvj 1031 热浪 最短路
  6. 没革哪有新?看云计算在医疗行业的版图
  7. linux内核源码目录结构
  8. 16个超牛逼的HTML5和JavaScript特效
  9. 《麦肯锡方法》阅读笔记2——探索分析问题的方法
  10. 建模实训报告总结_计算机三维建模实训报告
  11. PPT 2013 提升演讲逼格 视频教程
  12. 学习OpenCV 4(一)
  13. Altium Designer之PCB库部分名称区分
  14. AFNetworking 返回3840
  15. 打造完美用户体验:“天翼平台开放日”沙龙纪实
  16. python试卷河南理工大学官网_河南理工大学历年大学物理试卷及答案
  17. shufflenet中channel shuffle原理
  18. 技术的本质——读书笔记
  19. python机器学习-朴素贝叶斯(Naive Bayes)模型建立及评估(完整代码+实现效果)
  20. 创业企业家是一个职业头衔

热门文章

  1. python实验总结与分析_Python实验报告七
  2. SRGB和RGB的区别
  3. Hrbust2294修建传送门(尺取法)
  4. STM32 F103 外部晶振8M改为12M
  5. android 面试题(史上最全)
  6. 常见面试题之布隆过滤器的使用案例(海量数据)
  7. 不安全!!!都2020年了,你的网站还没上HTPPS说得过去吗?
  8. python二维列表每个子列表取一个元素的所有组合
  9. Php扫码签到功能怎么实现,扫码签到常用方法_云分组小程序
  10. 使地方坐标系BIM模型与CAD严格在LSV内对准