HDU3949 XOR
- Linear substrate sub-topic
- Given the number of some
- These request numbers obtained by exclusive or a large value of k.
- Code:
1 #include <bits/stdc++.h> 2 #define nmax 10010 3 4 using namespace std; 5 typedef long long ll; 6 ll a[nmax],b[70],k[70]; //k[i] 第i小的数 7 int n,q; 8 9 int main(){ 10 //freopen("owo.in","r",stdin); 11 int T; 12 cin>>T; 13 for (int cas=1; cas<=T; cas++) { 14 memset(b,0,sizeof(b)); 15 memset(k,0,sizeof(k)); 16 printf("Case #%d:\n",cas); 17 scanf("%d",&n); 18 for (int i=0; i<n; i++) { 19 scanf("%lld",&a[i]); 20 //求线性基 21 for (int j=61 is ; J> = 0 ; J, ) { 22 is IF (A [I] & (1LL << J)) { 23 is IF (B [J]) A [I] = ^ B [J]; 24 the else { B [J] = A [I]; BREAK ;} 25 } 26 is } 27 } 28 // up back-substitution, the way to count the number of 29 int CNT = 0 ; 30 for ( int I = 0 ; I <= 61 is ; ++ I) IF (B [I]) { 31 is K [CNT] = B [I]; 32 cnt++; 33 for (int j=i+1; j<=61; j++) if(b[j]&(1LL<<i)) b[j]^=b[i]; 34 } 35 ll mmax=(1LL<<cnt)-1; 36 scanf("%d",&q); 37 ll inq; 38 bool flag=false; 39 if(cnt<n) flag=true; 40 for (int j=0; j<q; j++) { 41 scanf("%lld",&inq); 42 if(flag) inq--; 43 if(inq>mmax) cout<<-1<<endl; 44 else { 45 ll ans=0; 46 for (int i=0; (1LL<<i)<=inq; i++) if((1LL<<i)&inq) ans^=k[i]; 47 printf("%lld\n",ans); 48 } 49 } 50 } 51 return 0; 52 }
BZOJ 2115: [Wc2011] Chorus
- The meaning of problems, an undirected graph, the node numbers 1 ~ n, find a path from 1 to n, edges and vertices may pass several times through the right side of seeking the maximum value of that path xor
- Solution: just find a path from 1 ~ n, and can be found in any path can be represented as a ring walk on this path, so find out all the rings
- Then the primary path is to find and exclusive OR largest in all of the rings in the
- Find all the rings dfs
- Code:
. 1 #include <bits / STDC ++ H.> 2 #define Nmax of 50010 . 3 #define Mmax 100010 . 4 . 5 the using namespace STD; . 6 typedef Long Long LL; . 7 int head [Nmax of] = { 0 }, D [Nmax of] = { 0 }; // is to sequentially vis . 8 LL a [Nmax of Mmax +], tmp [Nmax of], B [ 70 ] = { 0 }; . 9 int n-, m, IDX = 0 , IDXa = 0 ; // idza is recording the number of ring 10 struct Edge { . 11 int v,ne; 12 ll w; 13 }e[mmax*2]; 14 15 inline void addedge(int u,int v,ll w){ 16 idx++; 17 e[idx].v=v; 18 e[idx].w=w; 19 e[idx].ne=head[u]; 20 head[u]=idx; 21 } 22 23 void build(){ 24 cin>>n>>m; 25 int a,b; ll c; 26 for (int i=0; i<m; i++) { 27 scanf("%d%d%lld",&a,&b,&c); 28 addedge(a,b,c); 29 addedge(b,a,c); 30 } 31 } 32 33 void dfs(int u,int cnt){ 34 d[u]=cnt; 35 for (int i=head[u]; i; i=e[i].ne){ 36 int v=e[i].v; ll w=e[i].w; 37 if(d[v]>d[u]) continue; 38 is IF (D [V]) { 39 LL X = tmp [V] ^ tmp [U] ^ W; // This value is xor ring 40 // COUT << "now U =" U << << " = V "V << <<" and X = "X << << endl; 41 is IF (X) A [++ IDXa] = X; // 0 can discard 42 is } the else { 43 is tmp [V] tmp = [U] ^ W; // before dfs (u), u has a value calculated tmp 44 is DFS (V, + CNT . 1 ); 45 } 46 } 47 } 48 49 void xxj(){ 50 for (int i=1; i<=idxa; i++) for (int j=63; j>=0; j--) if((1LL<<j)&a[i]){ 51 if(b[j]) a[i]^=b[j]; 52 else { b[j]=a[i]; break; } 53 } 54 ll ans=tmp[n]; 55 for (int i=63; i>=0; i--) ans=max(ans,ans^b[i]); 56 printf("%lld\n",ans); 57 } 58 59 int main(){ 60 build(); 61 dfs(1,1); 62 xxj(); 63 return 0; 64 }