题目大意:有一张$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. 数据分析师(Data Analyst),数据工程师(Data Engineer),数据科学家(Data Scientist)的区别

    数据分析师(Data Analyst):负责从数据中提取出有用的信息,以帮助公司形成业务决策.工作内容包括:对数据进行提取,清洗,分析(用描述统计量,趋势分析,多维度分析,假设检验等统计常用方法对数据 ...

  2. 66、Spark Streaming:数据处理原理剖析与源码分析(block与batch关系透彻解析)

    一.数据处理原理剖析 每隔我们设置的batch interval 的time,就去找ReceiverTracker,将其中的,从上次划分batch的时间,到目前为止的这个batch interval ...

  3. 微信小程序根据状态换图

    在index.wxml中添加图片 <image bindtap="click" src="{{show?'/images/.png':'/images/.png'} ...

  4. 意图Intent

    意图点击官方链接 前言 对意图Intent,学习安卓需掌握.以官方链接:http://www.android-doc.com/reference/android/content/Intent.html ...

  5. GoCN每日新闻(2019-10-25)

    GoCN每日新闻(2019-10-25) GoCN每日新闻(2019-10-25) 1. [译]Golang应付百万级请求/分钟 https://juejin.im/post/5db1464b6fb9 ...

  6. Java-根据经纬度计算距离(百度地图距离)

    最近碰到一个需求,需要根据两个点的经纬度查询两点的距离.感觉以后还会用到,所以小记一波. 第一步:添加Maven依赖. <dependency> <groupId>org.ga ...

  7. JVM内存的划分

    JVM内存的划分有五片: 1.   寄存器: 2.   本地方法区: 3.   方法区: 4.   栈内存: 5.   堆内存.

  8. mysql 创建分组

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  9. Java 12 骚操作, switch居然还能这样玩!

    Java 13 都快要来了,12必须跟栈长学起! Java 13 即将发布,新特性必须抢先看! Java 12 中对 switch 的语法更友好了,建议大家看下栈长在Java技术栈微信公众号分享的&l ...

  10. css3实现水平垂直居中------(特别注意,里边的固定还是不固定)

    a,----定位方式(父元素宽高固定,子元素宽高固定) <div class="Father"> <div class="children"& ...