Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
2017-11-02 20:13
537 查看
传送门
点x到点y的路径可以是一条从x到y的路径与若干个环的异或和
首先搞出一棵dfs树,并且得到树上每个环的xor值。 我们发现,两点间就是本来的dis xor 某些环的xor值,即可组合得到一些新的异或值。位运算的题目,我们显然按位来做。 首先,对于两个这一位同时为1或者同时为0的,我们考虑若要有贡献,必须是从环上得到一个这一位为1的xor值,如果线性基这一位都是0则无贡献,否则我们可以考虑,假设线性基中有r个向量,那么我们把这一位为1的一个向量排除在外,剩下的随便选,任意组合,也就是2r−1,得到一个权值,再根据得到的权值这一位是1还是0,来决定被排除在外的这个向量选不选,所以贡献就是2r−1。 同理,如果一个是1一个是0,那么我同样是分类讨论,向量中有无这一位是1的,分别算贡献即可。
点x到点y的路径可以是一条从x到y的路径与若干个环的异或和
首先搞出一棵dfs树,并且得到树上每个环的xor值。 我们发现,两点间就是本来的dis xor 某些环的xor值,即可组合得到一些新的异或值。位运算的题目,我们显然按位来做。 首先,对于两个这一位同时为1或者同时为0的,我们考虑若要有贡献,必须是从环上得到一个这一位为1的xor值,如果线性基这一位都是0则无贡献,否则我们可以考虑,假设线性基中有r个向量,那么我们把这一位为1的一个向量排除在外,剩下的随便选,任意组合,也就是2r−1,得到一个权值,再根据得到的权值这一位是1还是0,来决定被排除在外的这个向量选不选,所以贡献就是2r−1。 同理,如果一个是1一个是0,那么我同样是分类讨论,向量中有无这一位是1的,分别算贡献即可。
/// .-~~~~~~~~~-._ _.-~~~~~~~~~-. /// __.' ~. .~ `.__ /// .'// \./ \\`. /// .'// | \\`. /// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`. /// .'//.-" `-. | .-' "-.\\`. /// .'//______.============-.. \ | / ..-============.______\\`. /// .'______________________________\|/______________________________`. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <vector> #include <iostream> #include <string> #include <map> #include <stack> #include <cstring> #include <queue> #include <list> #include <stdio.h> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <functional> #include <stdlib.h> #include <time.h> #include <bitset> using namespace std; #define pi acos(-1) #define s_1(x) scanf("%d",&x) #define s_2(x,y) scanf("%d%d",&x,&y) #define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X) #define S_1(x) scan_d(x) #define S_2(x,y) scan_d(x),scan_d(y) #define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z) #define PI acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x,y) memset(x,y,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0); #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define fOR(n,x,i) for(int i=n;i>=x;i--) #define fOr(n,x,i) for(int i=n;i>x;i--) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) #define bug printf("***********\n"); #define db double #define ll long long #define mp make_pair #define pb push_back typedef long long LL; typedef pair <int, int> ii; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,1,-1,-1,1}; const int dy[]={0,1,0,-1,-1,1,-1,1}; const int maxn=1e5+10; const int maxx=1e3+10; const double EPS=1e-8; const double eps=1e-8; const int mod=1e9+7; template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} template <class T> inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;} while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;} inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false; while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;} else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}} if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}} if(IsN) num=-num;return true;} void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');} void print(LL a){ Out(a),puts("");} //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); //cerr << "run time is " << clock() << endl; LL p[65]; int tot,cnt,r; int head[maxn]; LL vis[maxn],A[maxn*4]; vector<int>G; struct edge { int v,next; LL w; }e[maxn*4]; void add(int u,int v,LL z) { e[tot].v=v; e[tot].w=z; e[tot].next=head[u]; head[u]=tot++; } LL _pow(LL a,LL b) { LL ret=1; W(b) { if(b&1) ret=ret*a%mod; a=a*a%mod; b>>=1; } return ret; } void init() { me(head,-1); me(vis,-1); tot=0; } void dfs(int fa,LL s) { if(vis[fa]==-1) { vis[fa]=s; } else { A[++cnt]=vis[fa]^s; return ; } G.pb(fa); for(int i=head[fa];~i;i=e[i].next) { int v=e[i].v; dfs(v,s^e[i].w); } } LL calc() { me(p,0); FOR(1,cnt,i) { fOR(63,0,j) { if(!(A[i]>>j&1)) continue; if(!p[j]){p[j]=A[i];break;} A[i]^=p[j]; } } r=0; fOR(63,0,i) if(p[i]) r++;//计算线性基有效的向量个数 LL ans=0; fOR(63,0,i)//按位算贡献 { int c[2]={0},sign=0; fOR(63,0,j) if(p[j]>>i&1) sign=1;//是否存在某个向量的这一位为1 for(int j=0;j<G.size();j++) { int u=G[j]; if(sign) ans+=_pow(2,i+r-1)*j%mod; else ans+=_pow(2,i+r)*c[!(vis[u]>>i&1)]%mod; ans%=mod; c[vis[u]>>i&1]++; } } return ans; } int n,m; void solve() { W(cin>>n>>m) { init(); FOR(1,m,i) { int u,v;LL val; S_3(u,v,val); add(u,v,val); add(v,u,val); } LL ans=0; FOR(1,n,i) { if(vis[i]==-1) { G.clear(); cnt=0; dfs(i,0); ans+=calc(); ans%=mod; } } print(ans); } } int main() { //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int t=1; //init(); //s_1(t); for(int cas=1;cas<=t;cas++) { //printf("Case #%d: ",cas); solve(); } }
相关文章推荐
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- Intel Code Challenge Final Round Xor-matic Number of the Graph -- 线性基
- codeforces724G Xor-matic Number of the Graph -- 线性基
- 线性基 Codeforces724G Xor-matic Number of the Graph
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- A. Checking the Calendar(思路)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)A. Checking the Calendar
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
- CodeForces - 724G:Xor-matic Number of the Graph
- Codeforces Round Intel Code Challenge Final Round A.Checking the Calendar