题意

http://codeforces.com/contest/724/problem/G
定义三元组\((u,v,s)(u<v)\):\(u\)到\(v\)的路径的\(xor\)和为\(s\)(可以不是简单路径),\((u,v,s)\)不同当且仅当\(u,v,s\)的其中一个参数不同。
求所有不同三元组的\(s\)值之和。
\(n \in [1,10^5],m\in [0, 2\cdot 10^5], w\in [0,10^{18}]\)

题解

显然在同个联通块中,当前联通块中的所有环都可以被取到。而且每一条路径一定是一条简单路径与若干个环的\(xor\)和组成。
环的部分可以利用线性基来统计可以组成的不同的\(xor\)值个数。
\(dfs\)出搜索树,做树上前缀和,那么一条简单路径的\(xor\)和就是\(s[u] \oplus s[v]\),并顺便处理出所有环的\(xor\)和扔进线性基里。
因为\(xor\)对各位互不影响所以考虑按位处理。
对于第\(k\)位,将\(s\)划分为第\(k\)位为\(0\)和为\(1\)的,数量记为\(c_0\)和\(c_1\)
可以划分为\(3\)种情况:

  • \(0 \oplus 0\),共\(\frac{c_0\times (c_0-1)}{2}\)种。
  • \(1 \oplus 1\),共\(\frac{c_1\times (c_1-1)}{2}\)种。
  • \(1 \oplus 0\),共\(c_0 \times c_1\)种。

同时线性基中也有第\(k\)位为\(0\)和第\(k\)位为\(1\)的两种情况。
几种情况分别组合一下就好。
设线性基为\(b\)。则有

  • \(b[k]=0\)时,对于情况三,贡献的不同\(xor\)和为\(2^{|b|}\)种。对于情况一和情况二,贡献为\(0\)。
  • \(b[k]=1\)时,对于情况三,贡献的不同\(xor\)和为\(2^{|b|-1}\)种。对于情况一和情况二,贡献为\(2^{|b|-1}\)种。

复杂度为\(O(n \log^2 \max(w_i))\)

#include <bits/stdc++.h>
using namespace std;namespace io {
char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
inline char gc() {if(p1 != p2) return *p1++;p1 = buf;p2 = p1 + fread(buf, 1, 1 << 21, stdin);return p1 == p2 ? EOF : *p1++;
}
#define G gc#ifndef ONLINE_JUDGE
#undef G
#define G getchar
#endiftemplate<class I>
inline void read(I &x) {x = 0; I f = 1; char c = G();while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }while(c >= '0' && c <= '9') {x = x * 10LL + c - '0'; c = G(); }x *= f;
}template<class I>
inline void write(I x) {if(x == 0) {putchar('0'); return;}I tmp = x > 0 ? x : -x;if(x < 0) putchar('-');int cnt = 0;while(tmp > 0) {buf1[cnt++] = tmp % 10 + '0';tmp /= 10;}while(cnt > 0) putchar(buf1[--cnt]);
}#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')} using namespace io;#define ll long long
const int N = 100010;
const ll mod = 1e9 + 7;int cnt = 1, head[N];
struct edge {int to, nxt; ll v;} e[N<<2];
int n, m, tot;
ll s[N], b[N], ans, pw[N];
bool vis[N];struct lb {ll p[64];vector<ll> a;void clear() {a.clear();memset(p, 0, sizeof(p));}void insert(ll x) {for(ll i = 63; i >= 0; --i) if((x >> i) & 1LL) {if(p[i]) x ^= p[i];else {p[i] = x;return;}}}void init() {for(int i = 0; i <= 63; ++i) {if(p[i])for(int j = i - 1; j >= 0; --j) {if((p[j] >> i) & 1) p[i] ^= p[j];}}for(int i = 0; i <= 63; ++i) if(p[i]) a.push_back(p[i]);}ll solve(ll k, ll c, ll v) {bool flag = 0; v %= mod;for(int i = 0; i < (int)a.size(); ++i) {if((a[i] >> k) & 1) flag = 1;}if(c) {if(flag) return 1LL * pw[(int)a.size() - 1] * v % mod;return 1LL * pw[(int)a.size()] * v % mod;} else {if(flag) return 1LL * pw[(int)a.size() - 1] * v % mod;return 0;}}}B;void ins(int u, int v, ll w) {e[++cnt] = {v, head[u], w};head[u] = cnt;
}void dfs(int u, int in_edge) {vis[u] = 1;b[++tot] = s[u];for(int i = head[u]; i; i = e[i].nxt) {int v = e[i].to;if(in_edge == (i ^ 1)) continue;if(vis[v]) {B.insert(s[u] ^ s[v] ^ e[i].v);continue;}s[v] = s[u] ^ e[i].v;dfs(v, i);}
}ll power(ll x, ll y) { ll ans = 1;while(y) {if(y & 1) ans = ans * x % mod;x = x * x % mod; y >>= 1;} return ans;
}
ll inv2 = power(2, mod - 2);void solve() {ll c[2] = {0, 0};for(ll k = 0; k < 64; ++k) {c[0] = c[1] = 0;for(int i = 1; i <= tot; ++i) c[(b[i] >> k) & 1LL]++;if(c[0]) (ans += B.solve(k, 0, (1LL << k) % mod * c[0] % mod * (c[0] - 1LL) % mod) * inv2 % mod) %= mod;if(c[1]) (ans += B.solve(k, 0, (1LL << k) % mod * c[1] % mod * (c[1] - 1LL) % mod) * inv2 % mod) %= mod;(ans += B.solve(k, 1, (1LL << k) % mod * c[0] % mod * c[1] % mod)) %= mod;}
}int main() {read(n); read(m);for(int u, v, i = 1; i <= m; ++i) {ll w;read(u); read(v); read(w);ins(u, v, w), ins(v, u, w);}pw[0] = 1;for(int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * 2LL % mod;for(int i = 1; i <= n; ++i) {if(!vis[i]) {B.clear(); tot = 0; dfs(i, 0);B.init();solve();}}outn((ans%mod+mod)%mod);
}
/*
10 20
1 2 0
4 3 3
6 8 7
10 1 4
3 8 0
10 7 0
9 7 9
7 1 10
6 7 2
8 5 10
4 5 7
10 4 2
6 9 10
6 10 10
10 5 5
4 1 4
9 8 0
2 3 7
4 7 4
3 1 7
*/

转载于:https://www.cnblogs.com/henry-1202/p/11429038.html

CF724G. Xor-matic Number of the Graph相关推荐

  1. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  2. CF724G Xor-matic Number of the Graph(线性基+组合数)

    题目描述 给你一个无向图,有n个顶点和m条边,每条边上都有一个非负权值. 我们称一个三元组(u,v,s)是有趣的,当且仅当对于u,v,有一条从u到v的路径(可以经过相同的点和边多次),其路径上的权值异 ...

  3. 【HDU3949 + BZOJ2115 + CF724G】【异或线性基例题】| 倍增 | 第k小异或和 | DFS处理环 |【CGWR】| N

    三道关于异或线性基的有趣的题目 [1] HDU 3949. XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  4. Graph Convolutional Neural Network - Spatial Convolution 图卷积神经网络 — 空域卷积详解

    文章目录 往期文章链接目录 Note Convolutional graph neural networks (ConvGNNs) GCN Framework GCN v.s. RecGNN What ...

  5. 人群运动--Scene-Independent Group Profiling in Crowd

    Scene-Independent Group Profiling in Crowd CVPR2014 http://www.ee.cuhk.edu.hk/~jshao/CUHKcrowd.html ...

  6. Prim算法生成迷宫

    初始化地图 function initMaze(r,c){let row = new Array(2 * r + 1)for(let i = 0; i < row.length; i++){le ...

  7. 用python画关系网络图-python networkx 包绘制复杂网络关系图的实现

    1. 创建一个图 import networkx as nx g = nx.Graph() g.clear() #将图上元素清空 所有的构建复杂网络图的操作基本都围绕这个g来执行. 2. 节点 节点的 ...

  8. 用python画关系网络图-python networkx 包绘制复杂网络关系图

    目录 创建一个图 节点 边 查看图上点和边的信息 图的属性设置 点的属性设置 边的属性设置 不同类型的图(有向图Directed graphs , 重边图 Multigraphs) 图的遍历 图生成和 ...

  9. acm之简单博弈 Nim Bash Wythoff

    前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍 ...

  10. 【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

    题干: An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a pa ...

最新文章

  1. ubuntu16.04 uninstall cuda 9.0 completely and install 8.0 instead
  2. 《编程之美》读书笔记08:2.9 Fibonacci序列
  3. pptpd免radius限速、限连接+自由定制功能脚本
  4. 工业用微型计算机(26)-伪操作(2)
  5. Python requests 抓取网页状态返回418(亲测)
  6. keepalived高可用集群配置
  7. 名图1.8智能隐藏功能_7年后再度回归 全新一代名图“大”不同_搜狐汽车
  8. 训练机器人看脸读“心”,真的靠谱吗?
  9. Ubuntu中出现“Could not get lock /var/lib/dpkg/lock”的解决方法
  10. html中如何把选择文件的那个框放到右侧_如何关闭烦人的Mac通知?
  11. flutter usb串口_Flutter 踩坑记录
  12. matlab程序复制出现乱码,matlab程序复制到Word文档里变成乱码,该如何改?
  13. 计算机网络遴选的试题,税收信息化基础知识试题含答案
  14. 本学期数据结构学习总结
  15. JS - Promise使用详解--摘抄笔记
  16. Hash算法及常见碰撞解决方法
  17. linux下运行icem脚本,肿么安装linux版的icem
  18. 腾讯云对象存储的完整教程,java将文件上传到腾讯云上后返回可以访问的连接
  19. 互联网征信中的信用评分模型(转)
  20. Android的警示对话框AlertDialog简单使用实例(附Demo)

热门文章

  1. 纯虚函数的类为什么不能被实例化
  2. InitInstance函数
  3. 拓端tecdat|R语言逻辑回归、Naive Bayes贝叶斯、决策树、随机森林算法预测心脏病
  4. 拓端tecdat|在python 深度学习Keras中计算神经网络集成模型
  5. 拓端tecdat|matlab使用Copula仿真优化市场风险数据VaR分析
  6. Windows下IIS中不能添加网站
  7. Linux下source命令作用
  8. L1-003 个位数统计 (15 point(s))
  9. 【标注图像】windows下使用labelImg
  10. The remote system refused the connection.