Xor-matic Number of the Graph-CodeForces - 724G

线性基棒题

建议做这题前先看看线性基的概念,然后A掉这道题--->路径最大异或和

这两个题都用到了一个相同的性质:

任何一条路径的异或值都可以随意地与任意多个环相接

对于这道题来说,每一条路径都有它独立的贡献,并且都需要和每一个异或和不同的环相接

处理环异或和不同自然使用线性基判断插入是否成功

所以是一个全局统计的题目

如何统计?

将所有点到根节点的异或前缀和存下来,相互之间异或,就得到了所有路径的异或值,然后再添加环就很简单了

如何存呢?当然是按位算贡献

详见代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define reg register
#define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i) char IO;
inline int rd(){
int s=0,f=0;
while(!isdigit(IO=getchar())) if(IO=='-') f=1;
do s=(s<<1)+(s<<3)+(IO^'0');
while(isdigit(IO=getchar()));
return f?-s:s;
} const int N=5e5+10,E=2e5+10,P=1e9+7; int n,m; struct Edge{
int to,nxt;
ll w;
}e[E<<1];
int head[N],ecnt;
void AddEdge(int u,int v,ll w){
e[++ecnt]=(Edge){v,head[u],w};
head[u]=ecnt;
}
#define erep(u,i) for(int i=head[u];i;i=e[i].nxt) ll dis[N];
int vis[N];
int cnt[70][2];
ll tmp[70][2];
vector <int> Points;
vector <ll> cir;
void dfs(int u){
Points.push_back(u);
rep(i,0,60) if(dis[u]&(1ll<<i)) cnt[i][1]++;
else cnt[i][0]++;
vis[u]=1;
erep(u,i) {
int v=e[i].to;
if(vis[v]) {
cir.push_back(dis[v]^dis[u]^e[i].w);//说明这条边在一个环上
continue;
}
dis[v]=dis[u]^e[i].w;
dfs(v);
}
} ll Ans;
ll d[70];
bool Ins(ll d[],ll x){
drep(i,60,0) if(x&(1ll<<i)) {
if(d[i]) x^=d[i];
else {
d[i]=x;
return true;
}
}
return false;
}//线性基
void Solve(int rt){
Points.clear();cir.clear();
memset(cnt,0,sizeof cnt);memset(d,0,sizeof d); memset(tmp,0,sizeof tmp);
dfs(rt);
rep(k,0,(int)Points.size()-1) {
ll t=dis[Points[k]];
rep(i,0,60) if(t&(1ll<<i)) cnt[i][1]--;
else cnt[i][0]--;
rep(i,0,60) {
if(t&(1ll<<i)) tmp[i][0]+=cnt[i][1],tmp[i][1]+=cnt[i][0];
else tmp[i][0]+=cnt[i][0],tmp[i][1]+=cnt[i][1];
tmp[i][0]%=P,tmp[i][1]%=P;//依次与其他点形成贡献
}
rep(i,0,60) if(t&(1ll<<i)) cnt[i][1]++;
else cnt[i][0]++;
}
rep(i,0,(int)cir.size()-1) {//加入环
ll t=cir[i];
if(!Ins(d,t)) continue;
rep(i,0,60) {
if(t&(1ll<<i)) {
ll t=(tmp[i][0]+tmp[i][1])%P;
tmp[i][0]=tmp[i][1]=t;
} else tmp[i][0]*=2,tmp[i][1]*=2;
tmp[i][0]%=P,tmp[i][1]%=P;
}
}
rep(i,0,60) {
ll b=(1ll<<i)%P;
(Ans+=tmp[i][1]%P*b%P)%=P;
}
}
int main() {
n=rd(),m=rd();
rep(i,1,m) {
int u=rd(),v=rd();
ll w; scanf("%lld",&w);
AddEdge(u,v,w);
AddEdge(v,u,w);
}
rep(i,1,n) if(!vis[i]) Solve(i);//图不保证联通
printf("%lld\n",Ans*500000004%P);//答案会被多算一次,所以乘上Inv(2,1e9+7)
}

Xor-matic Number of the Graph-CodeForces - 724G的更多相关文章

  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. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  3. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

  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. Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列

    Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...

  7. Codeforces Global Round 4 Prime Graph CodeForces - 1178D (构造,结论)

    Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wan ...

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

    Codeforces 题目传送门 & 洛谷题目传送门 一道还算不套路的线性基罢-- 首先由于图不连通,并且不同连通块之间的点显然不可能产生贡献,因此考虑对每个连通块单独计算贡献.按照 P415 ...

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

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

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

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

随机推荐

  1. vue 鼠标右击事件

    使用@contextmenu.prevent即可 参考:https://www.cnblogs.com/sxz2008/p/6953082.html

  2. Java之路---Day18(List集合)

    2019-11-05-23:03:28 List集合: java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了List 接口的对象称为Lis ...

  3. SQLi_Labs通关文档【1-65关】

    SQLi_Labs通关文档[1-65关] 为了不干扰自己本机环境,SQL-LAB我就用的码头工人,跑起来的,搭建也非常简单,也就两条命令 docker pull acgpiano/sqli-labs ...

  4. 从 Vue 的视角学 React(一)—— 项目搭建

    虽然 Vue 在国内一家独大,但在全球范围内,React 依然是最流行的前端框架 最近和一些朋友聊天,发现很多项目都选择了 React 技术栈,而且公司的新项目也决定使用 React 我一直以来都是走 ...

  5. vagrant 无法挂载共享目录

    Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem " ...

  6. kbmmw 网络研讨会视频回放(更新至11.9)

    kbmmw 近期举行了几次网络视频直播,为了方便大家观看,放在了优酷上面. 1.Firemonkey for Linux and RAD Server 和kbmmw smartbing for Tli ...

  7. svn进行上传项目

    当svn的服务器搭建成功后,就可以进行上传项目了. 右键,选择客户端的repo-browser, 输入地址 然后就可以浏览所有项目: 然后在版本仓库上,右键,add folder, 添加对应的文件夹即 ...

  8. tp5 模型中配置数据库连接信息

    namespace app\api\model; use think\Model; class BaseModel extends Model { protected $connection = [ ...

  9. 【RabbitMQ】RabbitMQ的安装以及基本概念的介绍

    一.如何安装 https://www.cnblogs.com/756623607-zhang/p/11469962.html 二.基本概念介绍 ·Broker:可以理解为消息队列服务器的实体,它是一个 ...

  10. Underscore——JS函数库

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826065.html underscore是什么——它是一个js函数库 jQuery统一了不同浏览器之间的 ...