题目大意:有一张$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. js如何将汉字转化为拼音

    github地址,上面有封装好的转换工具:https://github.com/sxei/pinyinjs 里面有几个库,根据功能,库的文件大小也不一样,可以根据需求去引入使用. 里面封装好了方法: ...

  2. [HAOI2018]染色(NTT)

    前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...

  3. Android中LayoutInflater()方法

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. Git基本介绍(三大分区及核心内部构造)

    1. Git三大工作区(工作区.暂存区和版本库) 工作区(WORKING DIRECTORY): 直接编辑文件的地方,肉眼可见直接操作: 暂存区(STAGIN AREA):数据(快照)暂时存放的地方: ...

  5. 拉格朗日插值法(c++)【转载】

    摘自<c++和面向对象数值计算>,代码简洁明快,采用模板函数,通用性增强,对其中代码稍加改动 #include<iostream> #include <vector> ...

  6. java spring学习

    目的:为后面学习spring mvc ssm spring boot 打基础. 从单词就能看到有s,记录自学过程,感慨spring 一篇文章都写不完 介绍(来源百度百科): Spring是一个开源框架 ...

  7. CTF SSTI(服务器模板注入)

    目录 基础 一些姿势 1.config 2.self 3.[].() 3.url_for, g, request, namespace, lipsum, range, session, dict, g ...

  8. ubuntu之路——day14 只用python的numpy在底层实现多层神经网络

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...

  9. 大数据应用期末总评(hadoop综合大作业)

    作业要求源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3363 一.将爬虫大作业产生的csv文件上传到HDFS (1)在/usr ...

  10. Jmeter工具功能介绍

    可以去官方学习:http://jmeter.apache.org/ 1.可以修改语言 2.部分图标功能 新建 打开一个jmeter脚本 保存一个jmeter脚本 剪切 复制 粘贴 展开目录树 收起目录 ...