zoukankan      html  css  js  c++  java
  • CodeForces

    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 4
    1 2 1
    1 3 2
    2 3 3
    3 4 1
    Output
    12
    Input
    4 4
    1 2 1
    2 3 2
    3 4 4
    4 1 8
    Output
    90
    Input
    8 6
    1 2 2
    2 3 1
    2 4 4
    4 5 5
    4 6 3
    7 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
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    谢谢博客-园,让我不再有开源AYUI的想法
    [ay原创作品]用wpf写了个模仿36Kr网站登录背景的效果
    [AY技术分享]WPF AYUI的高大上日历代码
    [C#] AY.WPF-图形编程-高中生为起点-研究报告1
    [Aaronyang]谈谈2015年AY对WPF全面技术总结40多篇WPF,炫到没朋友的AYUI来了
    C#人爱学不学9[C#5.0异步实例+WPF自己的MVVM Async应用 1/12]
    Ay.Framework.WPF 2.0建立项目到底有多快
    [aaronyang]WPF4.5
    Hystrix是什么
    通俗理解ZooKeeper是如何保证数据一致性的
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9103819.html
Copyright © 2011-2022 走看看