codeforces 724G - Xor-matic Number of the Graph 线性基+图
题意:给出衣服无向带权图,问有多少对合法的$<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 线性基+图的更多相关文章
- 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> ...
- Codeforces.724G.Xor-matic Number of the Graph(线性基)
题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...
- 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 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 [高斯消元XOR 线性基 图]
啦啦啦 题意: N 个点M条边的边带权的无向图,求1到n一条XOR和最大的路径 感觉把学的东西都用上了.... 1到n的所有路径可以由一条1到n的简单路径异或上任意个简单环得到 证明: 如果环与路径有 ...
- bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基
题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...
随机推荐
- while循环与getopts处理
- 中州韵输入法(rime)导入搜狗词库
rime是一个非常优秀的输入法,linux平台下的反应速度远超搜狗,也没有隐私风险.2012年开始接触它,到后来抛弃了它,因为rime自带的词库真的太弱了,也懒得折腾.最近发现一个词库转换软件叫ime ...
- html使用字符串拼接js函数时传字符串参数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- jenkins连接gitlab,提示returned status code 128,附解决办法
在项目中配置git仓库地址,报无权限 Failed to connect to repository : Command "D:\Program Files\Git\mingw64\bin\ ...
- JVM内存模型及GC回收算法
该篇博客主要对JVM内存模型以及GC回收算法以自己的理解和认识做以记录. 内存模型 GC垃圾回收 1.内存模型 从上图可以看出,JVM分为 方法区,虚拟机栈,本地方法栈,堆,计数器 5个区域.其中最为 ...
- Linux 中安装JDK及配置环境
- DMA实验总结
一.RCC设置 没什么好写的之前USART的基本一样 /************************************************************************ ...
- paper 135:关于C#泛型的知识点
计划着要用一个月的时间把 C#语言Windows程序设计 搞定,现在是零零散散的知识点,日积月累吧!朋友们,看这里咯~呵呵 原文地址:http://www.blogjava.net/Jack2007 ...
- vue实现选项卡切换效果
效果如下: 说明: 这里我使用的原理是利用vue中的v-show/显示隐藏指令,当为true的时候显示,为false的时候隐藏 1html代码: <head> <meta chars ...
- Branch policies on Azure Repos
https://docs.microsoft.com/en-us/azure/devops/repos/git/branch-policies-overview?view=azure-devops B ...