Codeforces.724G.Xor-matic Number of the Graph(线性基)
\(Description\)
给定一张带边权无向图。若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意是三元组,不是两元组)。
求图中所有interesting triple中s的和。
\(Solution\)
同[WC2011]Xor,任意两点路径的Xor和是它们间(任意一条)简单路径的和Xor一些环的和。so可以先处理出环上的和,构造线性基。两点间的一条简单路径可以直接求个到根节点的dis[]。
有了各点的dis,然后考虑用组合逐位统计答案。
统计dis在这位上为0/1的点数,令size为线性基上向量个数。
如果两个点的dis是一个0一个1,那么要在线性基上取一个0。若线性基在这一位上有1,则保留,在剩下的\(size-1\)个向量中任意组合,根据得到的结果可以确定这个1是否取,这样有不同的\(2^{size-1}\)种方案;如果这位没有1,那就是\(2^{size}\)种方案。
如果两个点dis同为1/0,那要取一个1,如果线性基在这一位有1,同上 有\(2^{size-1}\)种方案。
注意图可能不连通。
//343ms 11200KB
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 300000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define Bit 59
#define mod (1000000007)
typedef long long LL;
const int N=1e5+5,M=4e5+5;
int n,m,Enum,H[N],nxt[M],to[M],cnt[2],t,pw[66],size;
LL len[M],dis[N],base[66],q[N];
bool vis[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline LL readll()
{
LL now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline void AddEdge(LL w,int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum, len[Enum]=w;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum, len[Enum]=w;
}
void Insert(LL x)
{
for(int i=Bit; ~i; --i)
if(x>>i & 1)
{
if(base[i]) x^=base[i];
else {base[i]=x, ++size; break;}
}
}
void DFS(int x,int f)
{
vis[x]=1, q[++t]=dis[x];
for(int v,i=H[x]; i; i=nxt[i])
if(!vis[v=to[i]]) dis[v]=dis[x]^len[i], DFS(v,x);
else if(v!=f) Insert(dis[x]^dis[v]^len[i]);
}
LL Calc()
{
LL ans=0;
for(int i=Bit; ~i; --i)
{
bool flag=0; LL tmp;
for(int j=0; j<=Bit; ++j)
if(base[j]>>i&1) {flag=1; break;}
cnt[0]=cnt[1]=0;
for(int j=1; j<=t; ++j) ++cnt[q[j]>>i&1];
if(!flag&&(!cnt[1]||!cnt[0])) continue;
if(flag)
{
tmp=((1ll*cnt[0]*(cnt[0]-1)>>1)+(1ll*cnt[1]*(cnt[1]-1)>>1))%mod;
ans+=1ll*tmp*pw[size-1]%mod*pw[i]%mod;
}
tmp=1ll*cnt[0]*cnt[1]%mod;
if(flag) ans+=1ll*tmp*pw[size-1]%mod*pw[i]%mod;
else ans+=1ll*tmp*pw[size]%mod*pw[i]%mod;
}
return ans%mod;
}
int main()
{
n=read(), m=read(), pw[0]=1;
for(int i=1; i<=Bit; ++i) pw[i]=pw[i-1]<<1, pw[i]>=mod&&(pw[i]-=mod);
while(m--) AddEdge(readll(),read(),read());
LL ans=0;
for(int i=1; i<=n; ++i)
if(!vis[i]) memset(base,0,sizeof base), size=t=0, DFS(i,i), ans+=Calc();
printf("%I64d",ans%mod);
return 0;
}
Codeforces.724G.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 题意:给你一张无向图.定义一个无序三元组 ...
- CodeForces - 724G:Xor-matic Number of the Graph
两点之间的任意路径都可表示为 随便某一条路径xor任何多个环, 然后可以用线性基来做,这样不会重复的, 另外必须一位一位的处理,xor是不满足结合律的 #include<cstdio> ...
- 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 ...
- codeforces 724G - Xor-matic Number of the Graph 线性基+图
题目传送门 题意:给出衣服无向带权图,问有多少对合法的$<u,v,s>$,要求$u$到$v$存在一条路径(不一定是简单路径)权值异或和等于$s$,并且$u<v$.求所有合法三元组的s ...
- codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基
题目传送门 题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为0. 思路:先处理出前缀异或,这样 ...
- Educational Codeforces Round 58 (Rated for Div. 2) G 线性基
https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...
- Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)
题目链接 \(Description\) 给定两个长为\(n\)的数组\(x_i,y_i\).每次你可以选定\(i,j\),令\(x_i=x_i\ \mathbb{xor}\ x_j\)(\(i,j\ ...
- bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基
题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...
- bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
随机推荐
- C. Connect Three(构造)
题目链接:http://codeforces.com/contest/1087/problem/C 题目大意:给你三个点的坐标,让你用尽可能少的方块,让这三个点连起来. 具体思路: 我们先对这三个点进 ...
- SolrJ查询条件组合查询实现——(十六)
带查询条件的实现原理: 查询按钮被包在一个大表单,表单还有三个隐藏域,一个商品筛选,一个 价格,一个排序,每次点击查询时候清空三个隐藏域,就带着一个大条件去查询;点击下面的筛选条件时,给隐藏域的筛选条 ...
- CKEDITOR的内容js转码,C#控制器解码接收
<script type="text/javascript" src="<%=Url.Content("~/Resource/ckeditor/ck ...
- 『实践』百度地图给map添加右键菜单(判断是否为marker)
var map; var s;//经度 var w;//纬度 $(document).ready(function(){ $(".mune").load("jsp/c ...
- mybatis比hibernate处理速度快的原因
mybatis:是面向结果集的.当要展示的页面需要几个字段时,springmvc会提供这几个字段并将其拼接成结果集,在转化为相应的对象. hibernate:是面向对象的.要展示的页面需要某些字段时, ...
- 在VS中让一个JS文件智能提示另一个JS文件中的成员2--具体引用
我们知道,在html中,利用<script language="javascript" type="text/javascript" src=" ...
- 使用插件实现Jenkins参数化构建
一.插件安装 1.打开插件管理,在此界面可以安装插件 二.参数化 1.在“可选插件”中查找如下两个插件然后安装,安装后重启Jenkins Build With Parameters 输入框式的参数 P ...
- 错误/异常:The project cannot be built until build path errors are resolved 和 Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'MyJavaCode';的解决方法
错误1: The project cannot be built until build path errors are resolved 解决方法: 把java的类库加载进去即可,在工程上右键 选择 ...
- LeetCode403. Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- django 建立一个简单的应用
本人的用的版本是python 2.7.3和django 1.10.5,Windows10系统 1.首先通过命令建立项目和app 找到django的安装路径,我的路径是:C:\Python27\Lib\ ...