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. Python中的单例设计

    01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟的解决方案 使用 设计模式 是为了可重用代码.让代码更容易被他人理解.保 ...

  2. Java自学-JDK环境变量配置

    JDK环境变量配置 分下载,配置,验证三个步骤进行JDK环境变量配置. 步骤 1 : 首先看配置成功后的效果 点WIN键->运行(或者使用win+r) 输入cmd命令 输入java -versi ...

  3. HTTP发展简史

    HTTP发展简史 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  4. .net core web api 默认的模型验证

    转载自 https://www.codercto.com/a/45938.html

  5. 国内不fq安装K8S二: 安装kubernet

    目录 2 安装kubelet 2.1 环境准备 2.2 设置国内的源 2.3 重要的设置 2.4 获取镜像 2.5 使用kubeadm init初始化集群 2.6 安装Pod Network 2.7 ...

  6. [STM32].NVIC嵌套中断向量的理解

    转自:http://www.21ic.com/embed/jiaocheng/sheji/201209/5634.html 一.STM32 (Cortex-M3) 中的优先级概念 STM32(Cort ...

  7. noVNC 搭建

    安装VNCSERVER 在使用noVNC之前,必须得先装vncserver,方法如下: yum -y install vnc-server 安装完成后,执行vncserver配置密码 命令:vncse ...

  8. seaborn---调色板

    一.样式控制 1.set([context,style,palette,font,...]) 一步设定美学参数. 2.axes_style([style,rc]) 返回参数字典,用于绘图的美学风格. ...

  9. 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)

    题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...

  10. 《快活帮》第九次团队作业:Beta冲刺与验收准备

    项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十三 团队作业9:BETA冲刺与团队项目验收 团队名称 快活帮 作业学习目标 (1)掌 ...