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 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
随机推荐
- linux学习记录.5.git & github
参考 https://www.linuxidc.com/Linux/2016-11/136769.htm 安装 安装 Git: apt-get install git 查看当前版本: git --ve ...
- 键盘ASCII码
回车键 -- CR 键0x0d -- 16进制13 -- 10 进制'\r' -- 也可以 换行键 -- LF0x0a -- 16进制10 -- 10 进制'\n' -- 也可以 esc键 ...
- jQuery.Validate 验证,以及 remote验证, 多参数传递
jQuery.Validate 验证: http://www.runoob.com/jquery/jquery-plugin-validate.html 教程网址,很简单, 今天主要在这里记录一下re ...
- bind系统调用
/* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...
- OpenJ_POJ 1058 Guideposts
Problem OpenJ_POJ Solution 如果我们用 \(G\) 来表示邻接矩阵,那么答案其实就是求\(\sum_{k|i}^n \binom n i G^i\) 为了消除整除的限制,我们 ...
- qt使用动态库(DLL)
本文主要讲解在QT开发环境中如何使用VC生成的DLL及QT自身生成的DLL.至于其它情况本文不作讨论. 连接库分为2种 (1)动态连接库,通常有.h .lib .dll三个文件,功能实现在dll中 ( ...
- Access中替代case when的方法 .
最近在做一个用Access的东东,其中用到了case when的方式,但是Access是不支持这种语法的,查询知道IIf和Swith可以作为替代,总结如下: IIf(expr, truepart, f ...
- 洛谷P2458 保安站岗
传送门啦 分析: 树形dp刚刚入门,这是我做的第一个一个点同时受父亲节点和儿子节点控制的题目. 由于这个题中某一个点放不放保安与父亲和儿子都有关系(因为线段的两个端点嘛),所以我们做题时就要考虑全面. ...
- Git push将本地版本库的分支推送到远程服务器上对应的分支
在使用git commit命令将修改从暂存区提交到本地版本库后,只剩下最后一步将本地版本库的分支推送到远程服务器上对应的分支了,如果不清楚版本库的构成,可以查看我的另一篇,git 仓库的基本结构. g ...
- Flask form
用户登录 #!/usr/bin/env python # -*- coding:utf- -*- from flask import Flask, render_template, request, ...