CodeForces - 724G Xor-matic Number of the Graph

Discription

You are given an undirected graph, constisting of n vertices and m edges. Each edge of the graph has some non-negative integer written on it.

Let‘s call a triple (u,?v,?s) interesting, if 1?≤?u?<?v?≤?n and there is a path (possibly non-simple, i.e. it can visit the same vertices and edges multiple times) between vertices u and v such that xor of all numbers written on the edges of this path is equal to s. When we compute the value s for some path, each edge is counted in xor as many times, as it appear on this path. It‘s not hard to prove that there are finite number of such triples.

Calculate the sum over modulo 109?+?7 of the values of s over all interesting triples.

Input

The first line of the input contains two integers n and m (1?≤?n?≤?100?000, 0?≤?m?≤?200?000) — numbers of vertices and edges in the given graph.

The follow m lines contain three integers ui, vi and ti (1?≤?ui,?vi?≤?n, 0?≤?ti?≤?1018, ui?≠?vi) —
vertices connected by the edge and integer written on it. It is
guaranteed that graph doesn‘t contain self-loops and multiple edges.

Output

Print the single integer, equal to the described sum over modulo 109?+?7.

Examples

Input

4 41 2 11 3 22 3 33 4 1

Output

12

Input

4 41 2 12 3 23 4 44 1 8

Output

90

Input

8 61 2 22 3 12 4 44 5 54 6 37 8 5

Output

62

Note

In the first example the are 6 interesting triples:

  1. (1,?2,?1)
  2. (1,?3,?2)
  3. (1,?4,?3)
  4. (2,?3,?3)
  5. (2,?4,?2)
  6. (3,?4,?1)

The sum is equal to 1?+?2?+?3?+?3?+?2?+?1?=?12.

In the second example the are 12 interesting triples:

  1. (1,?2,?1)
  2. (2,?3,?2)
  3. (1,?3,?3)
  4. (3,?4,?4)
  5. (2,?4,?6)
  6. (1,?4,?7)
  7. (1,?4,?8)
  8. (2,?4,?9)
  9. (3,?4,?11)
  10. (1,?3,?12)
  11. (2,?3,?13)
  12. (1,?2,?14)

The sum is equal to 1?+?2?+?3?+?4?+?6?+?7?+?8?+?9?+?11?+?12?+?13?+?14?=?90.

根据 Wc 2012 Xor 那道题,我们知道,两点之间所有路径xor和是可以通过随意找一条路径然后放到环的线性基里xjb异或而得到的,这个不难画图发现(模拟中途走去环然后返回的过程)。

于是,本题就是对于每个联通分量,先随便搞出一颗dfs树,并把所有回边构成的环加到线性基里。

然后发现任意两点之间在dfs树上路径的xor和就等于他们到跟xor和再xor起来,之后再考虑上线性基,推完一波式子之后直接做就行了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100005,ha=1e9+7;
int hd[maxn],to[maxn*4],ne[maxn*4],num,cnt[69][2],tot,n,m,ans;
ll val[maxn*4],ci[69],a[69],Xor[maxn];
bool v[maxn],can[69];

inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}
inline void addline(int x,int y,ll z){
    to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;
}

inline int C(int x){ return (x*(ll)(x-1)>>1)%ha;}

inline void update(ll x){
	for(int i=0;i<=60;i++) if(x&ci[i]) can[i]=1;
}

inline void ins(ll x){
	for(int i=60;i>=0;i--) if(x&ci[i]){
		if(!a[i]){ a[i]=x,tot++,update(x); return;}
		x^=a[i];
	}
}

void dfs(int x,int fa){
	v[x]=1;
	for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
		if(!v[to[i]]) Xor[to[i]]=Xor[x]^val[i],dfs(to[i],x);
		else ins(Xor[x]^Xor[to[i]]^val[i]);
	}

	for(int i=0;i<=60;i++) cnt[i][(Xor[x]&ci[i])?1:0]++;
}

inline void solve(){
	for(int o=1;o<=n;o++) if(!v[o]){
        memset(cnt,0,sizeof(cnt));
        memset(a,0,sizeof(a)),tot=0;
        memset(can,0,sizeof(can));
		dfs(o,0);

        /*
		for(int i=0;i<=60;i++)
	        for(int j=1;j<=n;j++) cnt[i][(Xor[j]&ci[i])?1:0]++;
	    */

    	for(int i=0,now;i<=60;i++){
            now=0;

            if(can[i]){
            	ADD(now,cnt[i][0]%ha*(ll)cnt[i][1]%ha);
            	ADD(now,add(C(cnt[i][0]),C(cnt[i][1]))%ha);
            	now=now*(ll)(ci[tot-1]%ha)%ha;
    		}
    		else ADD(now,ci[tot]%ha*(ll)cnt[i][0]%ha*(ll)cnt[i][1]%ha);

    		ADD(ans,now*(ll)(ci[i]%ha)%ha);
    	}
    }
}

int main(){
	ci[0]=1;
	for(int i=1;i<=60;i++) ci[i]=ci[i-1]+ci[i-1];

	scanf("%d%d",&n,&m);
	int uu,vv; ll ww;
	for(int i=1;i<=m;i++){
	    scanf("%d%d%I64d",&uu,&vv,&ww);
		addline(uu,vv,ww),addline(vv,uu,ww);
    }

	solve();

	printf("%d\n",ans);
	return 0;
}

原文地址:https://www.cnblogs.com/JYYHH/p/9103819.html

时间: 2024-11-02 20:28:51

CodeForces - 724G Xor-matic Number of the Graph的相关文章

Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组(u,v,s)表示u到v的(不一定为简单路径)路径上xor值为s.求出这张无向图所有不重复三元组的s之和.1≤n≤10^5,1≤m≤2*10^5. 想法: 如果做过[Wc2011 xor]这道题目(题解),那么问题变得简单起来了. ①假设我们钦定一个(u,v),设任意一条u->v的路径xor值为X,

724G - Xor-matic Number of the Graph(线性基)

724G - Xor-matic Number of the Graph 题意: 待补~~ 参考http://www.cnblogs.com/ljh2000-jump/p/6443189.html

CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上差分 ] | Codeforces Round #429(Div 1) 题意: 选择一个边集合,满足某些点度数的奇偶性 分析: 将d = 1的点连成一颗树,不在树上的点都不连边. 可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求 即整棵树上至多只有1个点不满足自身要求,

Codeforces Round #261 (Div. 2)——Pashmak and Graph

题目链接 题意: n个点,m个边的有向图,每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 (2?≤?n?≤?3·105; 1?≤?m?≤?min(n·(n?-?1),?3·105)) 分析: 因为路径上会有重复点,而边不会重复,所以最开始想的是以边为状态进行DP,get TLE--后来想想,在以边为点的新图中,边的个数可能会很多,所以不行. 考虑一下裸的DP如何做:路径上有重复点,可以将状态详细化,dp[i][j]表示i点以j为结束边值的最长路,但是数据不允许这样.想想

Codeforces 464C Substitutes in Number(高效+快速幂)

题目链接:Codeforces 464C Substitutes in Number 题目大意:给定一个字符串,以及n中变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一 个数,取模1e9+7. 解题思路:将操作倒过来处理,这样维护每个数来的val,len两个,val表示对应数值取模1e9+7,len表示对应有多少 位,再计算的过程中要使用. #include <cstdio> #include <cstring> #include <vector>

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 long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define PLI pair<LL, int> #define ull unsigned lo

CodeForces - 617E XOR and Favorite Number (莫队+前缀和)

Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1,

CF.724G.Xor-matic Number of the Graph(线性基)

题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple. 求图中所有interesting triple中s的和. \(Solution\) 同[WC2011]Xor,任意两点路径的Xor和是它们间(任意一条)简单路径的和Xor一些环的和.so可以先处理出环上的和,构造线性基.两点间的一条简单路径可以直接求个到根节点的dis[]. 有了各点的dis,然后考虑用组合

(莫队算法)CodeForces - 617E XOR and Favorite Number

题意: 长度为n的数列,m次询问,还有一个k.每次询问询问询问从数列的L到R内有多少个连续子序列异或起来等于k. 分析: 因为事先知道这题可以用莫队写,就正好用这题练习莫队. 预处理每个前缀异或和. 然后莫队按分块排序后,不断更新,用一个数组cnt[]记录当前L到R前缀和的数量. R向右拉,新增的数量就是cnt[pre^k],pre表示当前这个R位置的前缀异或和,然后更新一下cnt. 其他的也类似. 算是一个比较好的入门题. 代码: 1 #include <cstdio> 2 #include