Xor-matic Number of the Graph-CodeForces - 724G
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的更多相关文章
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- CF 724 G. Xor-matic Number of the Graph
G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...
- 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 ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- 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 ...
- Codeforces 724G - Xor-matic Number of the Graph(线性基)
Codeforces 题目传送门 & 洛谷题目传送门 一道还算不套路的线性基罢-- 首先由于图不连通,并且不同连通块之间的点显然不可能产生贡献,因此考虑对每个连通块单独计算贡献.按照 P415 ...
- 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(注意 ...
随机推荐
- python后端链接数据库-----MySQLdb
连接数据库之前请先确认好以下事宜: 1.已经建议好相应的数据库 2.在数据库中已经建立了相应的表 3.已经安装了MySQldb模块 示例: import MySQLdb # 打开数据库连接 db = ...
- Java 之 比较器( Comparator接口与 Comparable 接口)
一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...
- android studio学习----构建编译
项目创建成功后会自动下载Gradle,这个过程特别慢,建议翻墙.下载的Gradle在Windows平台会默认在 C:\Documents and Settings\<用户名>.gradle ...
- sizeof()计算
本节包含sizeof()计算结构体,位域,数组,字符串,指针,c++中的class等类型的大小,sizeof()计算的大小都是以字节为单位. 一 计算基本类型的长度 sizeof(char): 1 s ...
- python可视化_matplotlib
对于Python数据可视化库,matplotlib 已经成为事实上的数据可视化方面最主要的库,此外还有很多其他库,例如vispy,bokeh, seaborn,pyga,folium 和 networ ...
- WIN10分盘
https://jingyan.baidu.com/article/73c3ce28325dcde50243d94d.html
- Laravel 实现前后台用户分离登录
在很多时候,我们需要前台和后台进行不同的登录操作,以限制用户权限,现在用 Laravel 实现这个需求. 前戏 一.获取 Laravel 这个在文档中都有说明的,也比较简单,可以使用 composer ...
- Python 关于列表字典的键值修改
list (修改列表的索引值) 循环一个列表时,最好不要对原列表有改变大小的操作,这样会影响你的最终结果. #使用负索引进行修改列表 print('First') lis = [11, 22, 33, ...
- Twitter Storm学习笔记
官方英文文档:http://storm.apache.org/documentation/Documentation.html 本文是学习笔记,转载整合加翻译,主要是为了便于学习. 一.基本概念 参考 ...
- MySQL 快速删除大量数据
千万级数据量 方案1. 直接使用delete 因delete执行速度与索引量成正比,若表中索引量较多,使用delete会耗费数小时甚至数天的时间 方案2. (1)创建临时表,表结构与原表结构相同 ...