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(注意 ...
随机推荐
- Spring Security 解析(五) —— Spring Security Oauth2 开发
Spring Security 解析(五) -- Spring Security Oauth2 开发 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决 ...
- Jenkins+Gitee异常解决
Failed to connect to repository : Command "git ls-remote -h username@mygit.com:cc/myproject.git ...
- Java 之 List 接口
一.List 接口介绍 java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了 List 接口的对象称为 List 集合. 在 List 集 ...
- Junit测试。
Junit使用: 白盒测试 步骤: 1.定义测试类. 2.定义测试方法:可以单独运行. 3.给方法加@Test,导入junit依赖环境. 判定结果: 红色:失败 绿色:成功. 一般不看输出,而是使用 ...
- url请求时,参数中的+在服务器接收时为空格,导致AES加密报出javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
报错的意思的是使用该种解密方式出入长度应为16bit的倍数,但实际的错误却不是这个,错误原因根本上是因为在http请求是特殊字符编码错误,具体就是base64生成的+号,服务器接收时成了空格,然后导致 ...
- rest framework 之视图
一.APIView APIView 直接继承 View(Django 内置的 View),也就是说 APIView 是最贴近原生 Django 的 View 的. 因此可定制程度高,根据请求方法不同执 ...
- PostMan变量与断言应用(对标Jmeter)
常见的接口测试工具有PostMan/Jmeter/SoapUI,当然,也有一些公司为了更贴近自身的应用开发了一些小工具. 从功能上对比,Jmeter更为强大,既能做压测还能测接口,扩展性也比较好. B ...
- 警告:Establishing SSL connection without server’s identity verification is not recommended
SpringBoot启东时红色警告: Mon Jun 04 00:53:48 CST 2018 WARN: Establishing SSL connection without server's i ...
- 一些坑 Java 执行命令行命令 Spring Boot 打包为jar ResourceUtils.getFile 等出现的问题
Java 执行命令行命令 这个没技术含量的东西耗费了我半个多小时 String command = ....; Process process = Runtime.getRuntime().exec( ...
- ArcGIS Server 注册托管数据库
需要已经安装好ArcGIS for Desktop.ArcGIS for Server和ArcSDE,并且已经创建了地理数据库 我试了用管理网站添加,总是不成功,后来用ArcCatalog添加成功.这 ...