题目传送门

题意:给出衣服无向带权图,问有多少对合法的$<u,v,s>$,要求$u$到$v$存在一条路径(不一定是简单路径)权值异或和等于$s$,并且$u<v$。求所有合法三元组的s的和。

思路:

  参考了一篇大佬的博客。

  这类题的核心思想就是,两点之间的所有可能的路径,都是由一条简单路径加上若干个环组成的。u,v两点所有路径的异或值的集合,等价于,u,v一条简单路径的异或值,与整个连通图的所有环组成的线性基异或的集合。

  所以按位考虑每个二进制1给整幅图带来的价值。

  特别要注意的一点是,在处理线性基的过程中,最多处理到63位,如果处理到64位及以上,当$i>=64$,某些$(x>>i)$,或者$(x<<i)$,就会发出许多诡异的错误,wa了很久。

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdio>
#include<vector>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,b,a) for(int i=b;i>=a;i--)
#define clr(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pii pair<int,int >
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll rd()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
const ll mod=1e9+;
int n,m;
struct edge{
int to;
ll w;
};
vector<edge >ve[maxn];
int vis[maxn];
ll del[maxn],p[],siz,num[],ans=,tot=;
void init(){
rep(i,,n){
ve[i].clear();
vis[i]=;
}
ans=,siz=,clr(p,);
}
bool insert(ll x){
dep(i,,){
if((x>>i)&){
if(!p[i]){
p[i]=x;
return true;
}
x^=p[i];
}
}
return false;
}
void dfs(int u,ll res){
del[u]=res;
for(ll i=;i>=;i--){
if(((res>>i)&)){
num[i]++;
}
}
tot++;
vis[u]=;
for(auto &st:ve[u]){
if(!vis[st.to]){
dfs(st.to,res^st.w);
}else{
if(insert(res^st.w^del[st.to])){
siz++;
}
}
}
}
int main(){
while(cin>>n>>m){
init();
rep(i,,m){
int u,v;
ll w;
u=rd(),v=rd(),w=rd();
ve[u].pb({v,w});
ve[v].pb({u,w});
}
rep(u,,n){
if(!vis[u]){
tot=;
clr(p,),siz=;
clr(num,);
dfs(u,);
dep(i,,){
bool ok=;
dep(j,,){
if((p[j]>>i)&)ok=;
}
if(ok){
ans=(ans+tot*(tot-)/%mod*((1ll<<(siz-))%mod)%mod*((1ll<<i)%mod)%mod)%mod;
}else{
ans=(ans+num[i]*(tot-num[i])%mod*((1ll<<siz)%mod)%mod*((1ll<<i)%mod)%mod)%mod;
}
}
}
}
printf("%lld\n",ans);
}
}

codeforces 724G - Xor-matic Number of the Graph 线性基+图的更多相关文章

  1. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

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

    两点之间的任意路径都可表示为  随便某一条路径xor任何多个环, 然后可以用线性基来做,这样不会重复的, 另外必须一位一位的处理,xor是不满足结合律的 #include<cstdio> ...

  3. Codeforces.724G.Xor-matic Number of the Graph(线性基)

    题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...

  4. 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 ...

  5. codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基

    题目传送门 题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为0. 思路:先处理出前缀异或,这样 ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) G 线性基

    https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...

  7. 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\ ...

  8. BZOJ 2115: [Wc2011] Xor [高斯消元XOR 线性基 图]

    啦啦啦 题意: N 个点M条边的边带权的无向图,求1到n一条XOR和最大的路径 感觉把学的东西都用上了.... 1到n的所有路径可以由一条1到n的简单路径异或上任意个简单环得到 证明: 如果环与路径有 ...

  9. bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基

    题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...

随机推荐

  1. Java 编程技巧之数据结构

    前言: 介绍几种常见的java数据结构及应用. 使用HashSet判断主键是否存在 HashSet 实现 Set 接口,由哈希表(实际上是 HashMap )实现,但不保证 set  的迭代顺序,并允 ...

  2. 用户Bug修补报告

    用户Bug修补报告 虽然经过许多天的奋斗,我们的U-Help已经正式投入使用,不过在使用过程中遇到了大大小小的问题,我们通过努力修补了其中的相当一部分,以下是用户Bug修补报告. 7.31:发布bet ...

  3. Adapter的实现

    Adapter概念: Adapter是连接后端数据和前端显示的适配接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView, GridView)等地方都需要用到Adapte ...

  4. Oracle之分页问题

    前面的Top-N问题使用了reownum,但是又遇到个分页问题,将表emp的4行为1页输出,前4行很好做: select rownum,empno,ename,sal from emp ; 但是4-- ...

  5. 容斥原理+补集转化+MinMax容斥

    容斥原理的思想大家都应该挺熟悉的,然后补集转化其实就是容斥原理的一种应用. 一篇讲容斥的博文https://www.cnblogs.com/gzy-cjoier/p/9686787.html 当我们遇 ...

  6. 【服务端开发-杂】REST 和 Graphql

    基本知识链接: 1.[译]对比GraphQL与REST——两种HTTP API的差异:https://www.jianshu.com/p/2ad286397f7a 2. 怎样用通俗的语言解释REST, ...

  7. VS2013+phread.h环境配置

    原文链接:http://blog.csdn.net/qianchenglenger/article/details/16907821 本人使用的是windows7 旗舰版64位 目前用的是pthrea ...

  8. Java:新建数组

    Array Initialization int[] a; = int a[]; int[] a = new int[100]; a[]的值会被初始化为0 `int[] smallPrimes = { ...

  9. c++ fork进程与同步锁

    首先定义在多进程环境中的锁,采用读写锁,即可以同时读,但只能单独写. 头文件processLock.h #ifndef PROCESSLOCK_H #define PROCESSLOCK_H #inc ...

  10. vue的proxy和defineProperty区别

    Object.defineProperty(obj,"name",{ set:function(val){ if(var==='lisi'){ console.log(" ...