[CF724G]Xor-matic Number of the Graph
题目大意:有一张$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$。
- $dis$异或出的第$i$位为$1$,有$d_0\times d_1$种方法,那么线性基中异或出来要是$0$,若$b_1=0$,线性基的方案数是$2^m$,否则为$2^{m-1}$
- $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的更多相关文章
- 「CF724G」Xor-matic Number of the Graph「线性基」
题意 求所有点对\(u,v\),\(u\)到\(v\)所有不同的异或路径的异或值之和,对\(10^9+7\)取模 题解 求出一个dfs树,那么\(u\)到\(v\)的路径一定是树上路径异或一些环.这些 ...
- CF724G 【Xor-matic Number of the Graph】
题目就不翻译了吧,应该写的很清楚了... 首先 \(,\) 不懂线性基的可以戳这里.知道了线性基\(,\) 但是从来没有写过线性基和图论相结合的\(,\) 可以戳这里. 好\(,\) 点完了这些前置技 ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- CF 724 G. Xor-matic Number of the Graph
G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...
- 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 ...
- CF724G Xor-matic Number of the Graph(线性基+组合数)
题目描述 给你一个无向图,有n个顶点和m条边,每条边上都有一个非负权值. 我们称一个三元组(u,v,s)是有趣的,当且仅当对于u,v,有一条从u到v的路径(可以经过相同的点和边多次),其路径上的权值异 ...
- CodeForces - 724G:Xor-matic Number of the Graph
两点之间的任意路径都可表示为 随便某一条路径xor任何多个环, 然后可以用线性基来做,这样不会重复的, 另外必须一位一位的处理,xor是不满足结合律的 #include<cstdio> ...
- codeforces724G Xor-matic Number of the Graph
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 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 ...
随机推荐
- 73: luogu 2014 树形dp
$des$ 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有 ...
- Python入门(一)-打开世界之Hello World
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来用Python向世界说声Hello World,人生 ...
- 蚂蚁金服开源机器学习工具SQLFlow,机器学习比SQL还简单
来自:开源最前线(ID:OpenSourceTop) 综合自:AI前线.https://github.com/sql-machine-learning/sqlflow 5月6日,蚂蚁金服副 CTO 胡 ...
- sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock
原因:历史软件(包)更新(安装)未完成就退出了系统 解决办法:杀死该进程 sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock ...
- Linux 磁盘的分区
如果我们想在系统中新增一块硬盘,需要做什么呢? 1. 对磁盘进行分区,新建可用分区 2. 对该分区进行格式化,以创建系统可用的文件系统 3. 若想要仔细一点,可以对刚才新建好的文件系统进行检验 4. ...
- XmlIgnore的解释和使用
XmlIgnore是一个自定义属性,用来指明在序列化时是否序列化一个属性.如下面的例子: public class Group { public string GroupName; [XmlIgnor ...
- CentOS下启动和停止Tomcat
启动Tomcat: 进入tomcat目录/bin,然后./startup.sh 停止Tomcat: 进入tomcat目录/bin,然后./shutdown.sh
- SpringBoot使用jasypt加解密密码
在我们的服务中不可避免的需要使用到一些秘钥(数据库.redis等) 开发和测试环境还好,但生产如果采用明文配置讲会有安全问题,jasypt是一个通用的加解密库,我们可以使用它. <depende ...
- Java基础 try...catch(多个异常) 多个异常采取同样的解决措施
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- ABS函数 去掉金额字段值为负数问题
)) from OrderDetail