题目大意:有一张$n$个点$m$条边的无向图,定义三元组$(u,v,s)$是有趣的,当且仅当有一条$u\to v$的路径,路径上所有边的异或和为$s$。问所有有趣的三元组的$s$之和。$n\leqslant10^5,m\leqslant2\times10^5,w\leqslant10^{18}$

题解:可知,$u,v$之间路径可能的异或和为任意一条$u->v$的路径再异或上若干个环。先$dfs$求出图中所有环,丢进线性基。令$dis[u]$为任意一条$1\to u$的路径异或和,$ans=\sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^ns(dis[i]\oplus dis[j])$,$s(x)$表示$x$异或上若干线性基中的元素的和。而这可以通过枚举每一位的$01$来在$\log_2n$的时间复杂度内求出。

若现在考虑到了第$i$位,$dis$中第$i$位有$d_0$个是$0$,$d_1$个是$1$,线性基中有$m$个元素,这$m$个元素第$i$位有$b_0$个是$0$,$b_1$个是$1$。

  1. $dis$异或出的第$i$位为$1$,有$d_0\times d_1$种方法,那么线性基中异或出来要是$0$,若$b_1=0$,线性基的方案数是$2^m$,否则为$2^{m-1}$
  2. $dis$异或出的第$i$位为$0$,有$\dbinom{d_0}2+\dbinom{d_1}2$种方法,那么线性基中异或出来的要是$1$,若$b_1=0$,方案数为$0$,否则为$2^{m-1}$

卡点:现在$codeforces$网址有问题,没有交,不能保证代码的正确性

UPDATE(2019-8-9):我居然没测样例​就交了???果然出锅了。若图不连通,需要对每个连通块考虑,注意清空各个数组

C++ Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mul(a, b) (static_cast<long long> (a) * (b) % mod)
#define mul_(a, b) (static_cast<long long> (a) * (b))
const int maxn = 1e5 + 10, maxm = 2e5 + 10, mod = 1e9 + 7;
inline void reduce(int &x) { x += x >> 31 & mod; } const int M = 63;
int dnum[M + 1][2], bnum[M + 1][2], num;
long long P[M + 1];
void insert(long long x) {
for (int i = M; ~i; --i) if (x >> i & 1)
if (P[i]) x ^= P[i];
else { P[i] = x, ++num; return ; }
} int head[maxn], cnt;
struct Edge {
int to, nxt;
long long w;
} e[maxm << 1];
void addedge(int a, int b, long long c) {
e[++cnt] = (Edge) { b, head[a], c }; head[a] = cnt;
e[++cnt] = (Edge) { a, head[b], c }; head[b] = cnt;
} int n, m, ans, pw[maxn], q[maxn], tot;
long long dis[maxn];
bool vis[maxn];
void dfs(int u, long long w) {
dis[q[++tot] = u] = w, vis[u] = true;
for (int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
if (!vis[v]) dfs(v, w ^ e[i].w);
else insert(w ^ dis[v] ^ e[i].w);
}
}
inline long long C2(int x) { return mul_(x, x - 1) / 2; }
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n >> m; pw[0] = 1;
for (int i = 1; i <= n; ++i) reduce(pw[i] = pw[i - 1] + pw[i - 1] - mod);
for (int i = 0, x, y; i < m; ++i) {
static long long z;
std::cin >> x >> y >> z;
addedge(x, y, z);
}
for (int i = 1; i <= n; ++i) if (!vis[i]) {
tot = num = 0;
memset(P, 0, sizeof P);
memset(dnum, 0, sizeof dnum);
memset(bnum, 0, sizeof bnum);
dfs(i, 0);
for (int i = M; ~i; --i) if (P[i])
for (int j = M; ~j; --j) ++bnum[j][P[i] >> j & 1];
for (int i = 1; i <= tot; ++i)
for (int j = M; ~j; --j) ++dnum[j][dis[q[i]] >> j & 1];
for (int i = M; ~i; --i)
if (bnum[i][1])
ans = (ans + (mul_(dnum[i][0], dnum[i][1]) + C2(dnum[i][0]) + C2(dnum[i][1])) % mod * pw[num - 1] % mod * pw[i]) % mod;
else
ans = (ans + mul(dnum[i][0], dnum[i][1]) * pw[num] % mod * pw[i]) % mod;
}
std::cout << ans << '\n';
return 0;
}

  

[CF724G]Xor-matic Number of the Graph的更多相关文章

  1. 「CF724G」Xor-matic Number of the Graph「线性基」

    题意 求所有点对\(u,v\),\(u\)到\(v\)所有不同的异或路径的异或值之和,对\(10^9+7\)取模 题解 求出一个dfs树,那么\(u\)到\(v\)的路径一定是树上路径异或一些环.这些 ...

  2. CF724G 【Xor-matic Number of the Graph】

    题目就不翻译了吧,应该写的很清楚了... 首先 \(,\) 不懂线性基的可以戳这里.知道了线性基\(,\) 但是从来没有写过线性基和图论相结合的\(,\) 可以戳这里. 好\(,\) 点完了这些前置技 ...

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

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

  4. CF 724 G. Xor-matic Number of the Graph

    G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) G - Xor-matic Number of the Graph 线性基好题

    G - Xor-matic Number of the Graph 上一道题的加强版本,对于每个联通块需要按位算贡献. #include<bits/stdc++.h> #define LL ...

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

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

  7. CodeForces - 724G:Xor-matic Number of the Graph

    两点之间的任意路径都可表示为  随便某一条路径xor任何多个环, 然后可以用线性基来做,这样不会重复的, 另外必须一位一位的处理,xor是不满足结合律的 #include<cstdio> ...

  8. codeforces724G Xor-matic Number of the Graph

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 200. Number of Islands (Graph)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. [转]gulp排除已压缩文件思路

    文章转载至[gulp排除已压缩文件思路] gulp默认排除语法的弊端 有个时候我们需要时用gulp排除已经压缩过的js,css等.如果以压缩文件是以".min.js"之类命名规范的 ...

  2. 多维矩阵转一维数组(c++)【转载】

    在由二维矩阵转为一维数组时,我们有两种方式:以列为主和以行为主. 以列为主的二维矩阵转为一维数组时,转换公式为: index=column+row×行数 以行为主的二维矩阵转为一维数组时,转换公式为: ...

  3. 解决PHP7无法监听9000端口问题/502错误解决办法

    问题背景 昨晚帮配置nginx+php服务的时候,发生了一个奇怪的事情netstat -anp|grep 9000查看9000端口,一直没有监听,于是nginx无法通过fastcgi来代理php请求. ...

  4. ubuntu之路——day17.2 RGB图像的卷积、多个filter的输出、单个卷积层的标记方法

    和单层图像的卷积类似,只需要对每一个filter构成的三层立方体上的每一个数字与原图像对应位置的数字相乘相加求和即可. 在这个时候可以分别设置filter的R.G.B三层,可以同时检测纵向或横向边缘, ...

  5. 第05组Alpha冲刺(1/4)

    队名:天码行空 组长博客连接 作业博客连接 团队燃尽图(共享): GitHub当日代码/文档签入记录展示(共享): 组员情况: 组员1:卢欢(组长) 过去两天完成了哪些任务:SVN管理工具搭建 展示G ...

  6. RPC接口测试(二) RPC 与HTTP的区别

    RPC 与HTTP的相同点 两种风格的API区别,总结一下其实非常简单: 1,RPC面向过程,只发送 GET 和 POST 请求.GET用来查询信息,其他情况下一律用POST.请求参数是动词,直接描述 ...

  7. rrr

    # coding:utf-8 import pika username = 'feng' password = 'nihao' host = '192.168.1.144' credentials = ...

  8. 爬虫中Requests模块

    Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...

  9. 数据分析入门——pandas之数据合并

    主要分为:级联:pd.concat.pd.append 合并:pd.merge 一.numpy级联的回顾 详细参考numpy章节 https://www.cnblogs.com/jiangbei/p/ ...

  10. Python3.7安装(解决ssl问题)

    摘自:https://blog.csdn.net/love_cjiajia/article/details/82254371 python3.7安装(解决ssl的问题) 1) 安装准备 yum -y ...