CF724G Xor-matic Number of the Graph

直接维护异或和之和,显然不可能。由于二进制位相互独立,我们考虑维护每一个二进制位的出现次数,再乘以位权加入结果。

对于路径的维护,我们参考P4151 [WC2011] 最大XOR和路径,分成主路径和若干个环。记路径 \(1\to n\) 的边的异或和为 \(dis[i]\),任意两点 \(x,y\) 之间的路径的边的异或和可以由 \(dis[x]\oplus dis[y]\) 得到。如果 \(1\to x\) 和 \(1\to y\) 在点 \(z\) 前有重复部分,异或后重复部分消去,留下 \(x\to z\to y\)。否则,如果没有重复部分,则异或后变为 \(x\to 1\to y\)。结论成立。

如果有环的二进制第 \(i\) 位为 \(1\),那么我们肯定可以使第 \(i\) 位为 \(1\)。如果目前答案第 \(i\) 位为 \(0\),直接异或,否则不异或。因此,任意一条 \(x\to y\) 的简单路径都第 \(i\) 位都可以为 \(1\),有 \(C_{n}^2\) 中选法。除了第 \(i\) 位,其他位可以任意选择。方案数为 \(2^{x-1}\),其中 \(x\) 为线性基大小,即 \(bas[i]\ne0\) 的数的数量。运用乘法原理,再乘上权值,这一位的贡献为:

\[C_{n}^2\times2^{x-1}\times2^i
\]

如果没有环的二进制第 \(i\) 位为 \(1\),那么走环对于这一位没有影响。此时线性基可以随便选,方案数为 \(2^x\),其中 \(x\) 为线性基大小。我们只需要统计有多少条简单路径的异或和的第 \(i\) 位为 \(1\) 即可。

结合路径的计算式,\(dis[x]\oplus dis[y]\),我们继续进行拆位。对每一个 \(dis\),如果第 \(i\) 位为 \(1\),则将 \(w_{i,1}\) 加 \(1\);如果第 \(i\) 位为 \(0\),则将 \(w_{i,0}\) 加 \(1\)。\(w_{i,1}\) 表示第 \(i\) 位为 \(1\) 的 \(dis\) 的个数,\(w_{i,0}\) 表示第 \(i\) 位为 \(0\) 的 \(dis\) 的个数。如果要让一条路径的权值 \(dis[x]\oplus dis[y]\) 第 \(i\) 位为 \(1\),则必须在 \(w_{i,1}\) 中和 \(w_{i,0}\) 中各选一个。根据乘法原理,方案数为 \(w_{i,0}\times w_{i,1}\)。运用乘法原理,再乘上权值,这一位的贡献为:

\[w_{i,0}\times w_{i,1}\times2^{x}\times2^i
\]

代码中这一部分实现方式略微不同,其中 \(ct[i]\) 表示 \(w_{i,1}\),\(pos-ct[i]\) 表示 \(w_{i,0}\)。

注意图不一定联通,对于每个联通块分别计算即可。

#include <bits/stdc++.h>
using namespace std;
struct edge
{
long long v,nxt,d;
}e[800000];
long long n,m,u,v,d,h[800000],bas[100],book[800000],dis[800000],ct[100],p[800000],cnt=1,ans=0,siz=0,pos=0;
const long long mod=1e9+7;
void add_edge(long long u,long long v,long long d)
{
e[++cnt].nxt=h[u];
e[cnt].v=v;
e[cnt].d=d;
h[u]=cnt;
} void insert(long long x)
{
for(int i=63;i>=0;i--)
if((x>>i)&1)
{
if(bas[i]!=0)x^=bas[i];
else
{
siz++,bas[i]=x;
break;
}
}
} void dfs(long long now,long long pre,long long dn)
{
book[now]=1,dis[now]=dn,pos++;
for(int i=63;i>=0;i--)
if((dn>>i)&1)ct[i]++;
for(int i=h[now];i;i=e[i].nxt)
if(i!=(pre^1))
{
if(book[e[i].v])insert(dis[now]^dis[e[i].v]^e[i].d);
else dfs(e[i].v,i,dn^e[i].d);
}
} int main()
{
scanf("%lld%lld",&n,&m);
p[0]=1;
for(int i=1;i<=63;i++)p[i]=p[i-1]*2%mod;
for(int i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&u,&v,&d);
add_edge(u,v,d),add_edge(v,u,d);
}
for(int i=1;i<=n;i++)
if(!book[i])
{
memset(bas,0,sizeof(bas)),memset(ct,0,sizeof(ct));
pos=siz=0;
dfs(i,0,0);
long long flag=0;
for(int j=63;j>=0;j--)flag|=bas[j];
for(int j=63;j>=0;j--)
if((flag>>j)&1)ans=(ans+pos*(pos-1)%mod*500000004%mod*p[siz-1]%mod*p[j]%mod)%mod;
else ans=(ans+ct[j]*(pos-ct[j])%mod*p[siz]%mod*p[j]%mod)%mod;
}
printf("%lld",ans);
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. [CF724G]Xor-matic Number of the Graph

    题目大意:有一张$n$个点$m$条边的无向图,定义三元组$(u,v,s)$是有趣的,当且仅当有一条$u\to v$的路径,路径上所有边的异或和为$s$.问所有有趣的三元组的$s$之和.$n\leqsl ...

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

    题目传送门:CF724G. 题意简述: 一张 \(n\) 个点的无向图,边有边权. 定义三元组 \((u,v,w)(1\le u < v\le n)\) 合法当且仅当存在从点 \(u\) 到点 ...

  9. POJ 1737 Connected Graph 题解(未完成)

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  10. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

随机推荐

  1. Greenplum数据库时间操作汇总

    Greenplum数据库时间操作与mysql有一些区别,汇总以往笔记记录下来. greenplum时间格式:'yyyy-mm-dd hh24:mi:ss.us'.'yyyy-mm-dd hh:mi:s ...

  2. 使用Tortoise-ORM和FastAPI构建评论系统

    title: 使用Tortoise-ORM和FastAPI构建评论系统 date: 2025/04/25 21:37:36 updated: 2025/04/25 21:37:36 author: c ...

  3. mysql 5.7等保2.0安全配置

    一.安装密码校验插件validate_password 在使用服务器插件之前,必须将它们加载到服务器中.MySQL支持在服务器启动和运行时加载插件.还可以在启动时控制加载插件的激活状态,并在运行时卸载 ...

  4. scrcpy - Android手机投屏操作神器

    推荐一个Genymotion推出的投屏工具,跨平台,自定义码率,最重要的是开源,简直良心. Github:https://github.com/Genymobile/scrcpy 下载地址: http ...

  5. Mysql如何给字符串添加索引(前缀索引)

    在日常开发中,我们经常给字符串添加索引,那么给字段添加索引有什么技巧吗,我们看看下面的例子,我们给一个邮箱添加索引,应该如何添加呢 看看下面这条sql select * from user where ...

  6. 雷总小米十周年演讲---国外友人评价第一次看到MIUI系统

    雷总在小米十周年演讲时提到的当时讨论MIUI系统的帖子 xdadevelopers论坛 https://forum.xda-developers.com/showthread.php?t=787877 ...

  7. 信息资源管理文字题之“IT服务管理的核心流程和具体内容”

    一.为了充分利用ERP信息系统资源,LX集团采用了各种先进的信息系统管理概念和方法,包括IT服务管理. 要求:说明IT服务管理流程包括那两大核心类别,分别说明他们个包含哪些具体流程 二.答案 答:两大 ...

  8. Excel 的 vlookup 函数

    突然发现, 大多数的开发, 都不会用 Excel. 想想, 像我这种, 熟练Excel 的函数如 vlookup, sumifs, contif 还会透视表的小哥哥, 已经不多了啦.

  9. JS的哪些新特性,你都用过么?

    @charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...

  10. argparse基本功能极简介绍

    argparse基本功能极简介绍 python脚本文件可以通过命令行的方式调用,在这种调用方法中,可以通过sys.argv来把命令行参数传入脚本文件,通过这种方式传入的参数是string,并且需要将该 ...