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. windows下vmware和Hyper-v共存方法

    问题描述:环境:windows server 2012 r2系统下安装Hyper-v后,再安装Vmware 在Vmware中创建虚拟机,安装虚拟机系统的时候,vmware提示:VMware Works ...

  2. node-exporter常用指标含义,比如在prometheus中查询node_load1的指标数据

    参考: https://blog.csdn.net/yjph83/article/details/84909319 https://www.gitbook.com/book/songjiayang/p ...

  3. Java自学-接口与继承 Object类

    Java中的超类 Object 步骤 1 : Object类是所有类的父类 声明一个类的时候,默认是继承了Object public class Hero extends Object package ...

  4. Python进阶----pymysql模块的使用,单表查询

    Python进阶----pymysql模块的使用,单表查询 一丶使用pymysql ​   ​   1.下载pymysql包: pip3 install pymysql ​​   ​   2.编写代码 ...

  5. VUE组件3 数据流和.sync修饰符

    单向数据流:数据通过prop从父组件传递到子组件中,当父级组件中的数据更新时,传子组件也会更新,但不能在子组件中修改.防止子组件在无意中修改,改变父级组件状态 然而,双向数据绑定在某些情况下有用.如果 ...

  6. 输入url之后经历什么?

    一.浏览器查找输入域名的IP地址(拿到 IP) 1.查找浏览器缓存(浏览器一般会缓存DNS记录一段时间,一般为2-30分钟). 2.查找系统缓存(即hosts文件,有没有对应的IP) 3.以上都没有的 ...

  7. Spring中获取外部配置文件中的属性值

    很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value注解和<context:placeholder>来获取配置文件中的配置信息.这里给出一个简单的例子. 首 ...

  8. linux解压缩的常用命令

    1.解包:tar xvf filename.tar, 打包: tar cvf filename DirName 2.解压:gunzip filename.gz, tar zxvf filename.t ...

  9. Nginx Location指令URI匹配规则详解

    server { listen 80; server_name ss.test *.ss.test; root "D:/Project/PHP/admin-h5/dist/"; s ...

  10. dfs 排列组合——找所有子集(重复元素和不重复元素)

    17. 子集 中文 English 给定一个含不同整数的集合,返回其所有的子集. 样例 样例 1: 输入:[0] 输出: [ [], [0] ] 样例 2: 输入:[1,2,3] 输出: [ [3], ...