G. Xor-matic Number of the Graph

链接

题意:

  给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s,求所有三元环权值之和。

分析:

  求出所有的三元环,建立线性基,然后逐位求每一位的贡献。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline LL read() {
LL x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , mod = 1e9 + ;
struct Edge{ int to, nxt; LL w; } e[];
int head[N], En;
LL dis[N], b[], mi[N], Ans;
bool vis[N];
vector<LL> B;
vector<int> A; inline void add_edge() {
int u = read(), v = read(); LL w = read();
++En; e[En].to = v, e[En].w = w; e[En].nxt = head[u]; head[u] = En;
++En; e[En].to = u, e[En].w = w; e[En].nxt = head[v]; head[v] = En;
}
void dfs(int u) {
A.push_back(u);
vis[u] = ;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[v]) B.push_back(dis[u] ^ e[i].w ^ dis[v]);
else {
dis[v] = dis[u] ^ e[i].w;
dfs(v);
}
}
}
void Insert(LL x) {
for (int i = ; ~i; --i) {
if ((x >> i) & ) {
if (b[i]) x ^= b[i];
else {
b[i] = x;
break;
}
}
}
}
void Clear() {
memset(b, , sizeof(b));
A.clear(), B.clear();
}
inline void mul(LL &x,LL y) { x *= y; x %= mod; }
inline void add(LL &x,LL y) { x += y; if (x >= mod) x -= mod; } void Calc() {
for (int i = ; i < (int)B.size(); ++i) Insert(B[i]);
LL cnt[] = {, }, r = , now;
for (int i = ; ~i; --i) if (b[i]) r ++;
for (int k = ; ~k; --k) {
bool flag = ; cnt[] = cnt[] = ;
for (int i = ; ~i; --i) if ((b[i] >> k) & ) flag = ;
for (int i = ; i < (int)A.size(); ++i) cnt[(dis[A[i]] >> k) & ] ++;
now = cnt[] * (cnt[] - ) / + cnt[] * (cnt[] - ) / ; now %= mod;
if (flag) {
if (r >= ) mul(now, mi[r - ]);
mul(now, mi[k]);
add(Ans, now);
}
now = cnt[] * cnt[];
if (flag) { if (r >= ) mul(now, mi[r - ]); }
else mul(now, mi[r]);
mul(now, mi[k]);
add(Ans, now);
}
Clear();
} int main() {
int n = read(), m = read();
mi[] = ;
for (int i = ; i <= ; ++i) mi[i] = mi[i - ] * % mod;
for (int i = ; i <= m; ++i) add_edge();
for (int i = ; i <= n; ++i) {
if (!vis[i]) {
dfs(i);
Calc();
}
}
cout << Ans;
return ;
}

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

  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. 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 ...

  3. CF - 392 C. Yet Another Number Sequence (矩阵快速幂)

    CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 ...

  4. 2019南昌网络赛G. tsy's number

    题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)} ...

  5. CF 1051 G. Distinctification

    G. Distinctification 链接 分析: 线段树合并 + 并查集. 最后操作完后a连续递增的一段,b一定是递减的.最后的答案是$\sum (a_{new}-a_{odd}) \times ...

  6. CF 1093 G. Multidimensional Queries

    G. Multidimensional Queries 链接 分析: 考虑如何去掉绝对值符号. $\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|$,由于k ...

  7. CF 914 G Sum the Fibonacci —— 子集卷积,FWT

    题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...

  8. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

    There is a tree with nn nodes. For each node, there is an integer value a_ia​i​​, (1 \le a_i \le 1,0 ...

  9. CF 960 G

    难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数

随机推荐

  1. webservice双向验证

    ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtoco ...

  2. 【gp数据库】查询系统表看模式下所有表的分布键信息

    Greenplum是关系型的分布式数据库,需要存储的数据库在进入数据库时,将先进行数据分布的处理工作,讲一个表的数据平均分不到每个节点上,并为每个表指定一个分发列(distribute Column) ...

  3. 优化REST Framework 的 路由 APIView 和ViewSetMixin

    APIview: 我们经常写的是view  这个APIview继承了我们的view,并且对请求进来的信息进行设置, 在APIView这个例子中,调用了drf本身的serializer以及Respons ...

  4. (转)图形学理论知识 BRDF 双向反射分布函数(Bidirectional Reflectance Distribution Function)

    BRDF理论 BRDF表示的是双向反射分布函数(Bidirectional Reflectance Distribution Function),它描述了光线如何在物体表面进行反射,可以用来描述材质属 ...

  5. zabbix日常监控(监控缓存)

    实现的方法大体类似: 多谢博主的文章,免了不少时间! 摘抄博文地址:https://www.cnblogs.com/sixiweb/p/6893858.html https://www.cnblogs ...

  6. 字符串,元组,列表; 切片&range

    总结:字符串: "" 组成元组: () 组成列表: [] 组成 切片 中括号冒号: [5: 0: -2] # print(s2[5:0:-2]) 此步长为-2,则从右往左取, 则a ...

  7. SDN 第一次上机作业

    第一题 拓扑: 测试连通性: 第二题 拓扑: 测试连通性: 第三题 拓扑: 测试连通性:

  8. Ubuntu 14.04安装QQ2012

    GTkqq ,pidginQQ........等多多少少都存在一定的缺陷和问题. linuxQQ 有各种版本,这里介绍两种:linuxQQ(基本已不支持) 和 wineQQ (推荐使用) 1 ---- ...

  9. Kubernetes 详解

    Kubernetes主要由以下几个核心组件组成: etcd保存了整个集群的状态: apiserver提供了资源操作的唯一入口,并提供认证.授权.访问控制.API注册和发现等机制: controller ...

  10. nuxt 脚手架创建nuxt项目中不支持es6语法的解决方案

    node本身并不支持es6语法,我们通常在vue项目中使用es6语法,是因为,我们使用babel做过处理, 为了让项目支持es6语法,我们必须同时使用babel 去启动我们的程序,所以再启动程序中加 ...