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.
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.
Print the single integer, equal to the described sum over modulo 109 + 7.
4 4
1 2 1
1 3 2
2 3 3
3 4 1
12
4 4
1 2 1
2 3 2
3 4 4
4 1 8
90
8 6
1 2 2
2 3 1
2 4 4
4 5 5
4 6 3
7 8 5
62
In the first example the are 6 interesting triples:
- (1, 2, 1)
- (1, 3, 2)
- (1, 4, 3)
- (2, 3, 3)
- (2, 4, 2)
- (3, 4, 1)
In the second example the are 12 interesting triples:
- (1, 2, 1)
- (2, 3, 2)
- (1, 3, 3)
- (3, 4, 4)
- (2, 4, 6)
- (1, 4, 7)
- (1, 4, 8)
- (2, 4, 9)
- (3, 4, 11)
- (1, 3, 12)
- (2, 3, 13)
- (1, 2, 14)
计算出两种的方案数之和,还要算上起点和终点取的方案数,再乘以这一位对应的数值2^i,就是第i位的贡献。统计所有的即可。具体见代码:
#include<bits/stdc++.h>
#define mod 1000000007
#define LL long long
#define N 500010
using namespace std;
struct Linear_Basis
{
LL b[63],nb[63],tot;
void init()
{
tot=0;
memset(b,0,sizeof(b));
memset(nb,0,sizeof(nb));
}
bool ins(LL x)
{
for(int i=62;i>=0;i--)
if (x&(1LL<<i))
{
if (!b[i]) {b[i]=x;break;}
x^=b[i];
}
return x>0;
}
LL Max(LL x)
{
LL res=x;
for(int i=62;i>=0;i--)
res=max(res,res^b[i]);
return res;
}
LL Min(LL x)
{
LL res=x;
for(int i=0;i<=62;i++)
if (b[i]) res^=b[i];
return res;
}
void rebuild()
{
for(int i=62;i>=0;i--)
for(int j=i-1;j>=0;j--)
if (b[i]&(1LL<<j)) b[i]^=b[j];
for(int i=0;i<=62;i++)
if (b[i]) nb[tot++]=b[i];
}
LL Kth_Max(LL k)
{
LL res=0;
for(int i=62;i>=0;i--)
if (k&(1LL<<i)) res^=nb[i];
return res;
}
} LB;
LL s[N],w[N],loop[N],cnt[2],ans;
struct Edge{int y;LL w;};
vector<Edge> g[N];
vector<int> p;
int n,m,r,tot;
bool v[N];
void dfs(int x)
{
v[x]=1;
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i].y;
LL w=g[x][i].w;
if (!v[y])
{
s[y]=s[x]^w,dfs(y);
p.push_back(y);
} else loop[++tot]=s[y]^s[x]^w;
}
}
LL qpow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod; b>>=1;
}
return ans;
}
void calc()
{
for(int i=0;i<=62;i++)
{
bool flag=0;
cnt[0]=cnt[1]=0;
for(int j=0;j<p.size();j++)
cnt[(s[p[j]]>>i)&1]++;
LL tmp=cnt[0]*(cnt[0]-1)/2+cnt[1]*(cnt[1]-1)/2; tmp%=mod; //起点终点第i位相同的方案数
for(int j=0;j<=62;j++) if (LB.b[j]&(1LL<<i)) {flag=1;break;} //判断是否有向量第i位为1
if (flag)
{
if (r) tmp=tmp*qpow(2,r-1)%mod; //如果有那么统计
ans=(ans+tmp*qpow(2,i)%mod)%mod;
}
tmp=cnt[0]*cnt[1]%mod; //起点和终点第i为不同的方案数
if (flag)
{
if (r) tmp=tmp*qpow(2,r-1)%mod;
} else tmp=tmp*qpow(2,r)%mod;
ans=(ans+tmp*qpow(2,i)%mod)%mod;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
ans=0;
for(int i=1;i<=m;i++)
{
int u,v; LL p;
scanf("%d%d%lld",&u,&v,&p);
g[u].push_back(Edge{v,p});
g[v].push_back(Edge{u,p});
}
for(int i=1;i<=n;i++)
if (!v[i])
{
LB.init(); p.clear(); //不同连通分量的东西不能够混用
tot=r=0; p.push_back(i); dfs(i);
for(int j=1;j<=tot;j++) LB.ins(loop[j]);
for(int j=0;j<=62;j++) if (LB.b[j]) r++;
calc();
}
printf("%lld\n",ans);
}
}