#41 Shortest Path (division + linear basis)

Source: Internet
Author: User

Consider how the different or shortest circuits should be asked. So this is a WC problem, and DFS finds all the useful rings in the linear basis again, because the weights of each ring can be taken and not affected by other parts.

Now for a tree, think of him as the original Dfs tree. Each additional edge is added with a ring. After calculating the weights, the problem now becomes the maximum difference or value for a number and a number in the interval of any selection.

The way to compare violence is to build a line tree directly, each time logn*log2v out the interval linear basis. This will take 50 points.

The merging of linear radicals is too slow. Consider whether you can do it offline. Each time a query is taken out of the midpoint of the interval, the suffix prefix linear base around the midpoint is processed, and the linear base is merged on the query. So it became LOGV (LOGN+LOGV).

Half a day to find M and N was reversed.

Sure enough it was noip simulation.

#include <iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<algorithm>#include<cassert>using namespacestd;intRead () {intx=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9') {if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') x= (x<<1) + (x<<3) + (c^ -), c=GetChar (); returnx*F;}#defineN 300010intn,m,q,p[n],deep[n],value[n],ans[n],t=0;struct Base{    intsize,a[ to]; voidInsintx) { for(RegisterintI= -; ~i;i--)        {            if(!x) Break; if(x& (1<<i))if(!a[i]) {a[i]=x;size++; Break;} Elsex^=A[i]; }    }    Base operator+(Const Base&AMP;B)Const    {        BaseC; if(size>b.size) {c.size=size; for(intI=0;i< to; i++) c.a[i]=A[i]; if(size== to)returnC;  for(RegisterintI= -; ~i;i--) C.ins (B.a[i]); }        Else{c.size=b.size; for(intI=0;i< to; i++) c.a[i]=B.a[i]; if(b.size== to)returnC;  for(RegisterintI= -; ~i;i--) C.ins (A[i]); }        returnC; }}pre[n],suf[n];structdata{intTo,nxt,len;} Edge[n<<1];structdata2{inti,l,r,x;} Q[n],u[n];voidAddedge (intXintYintZ) {t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].len=z,p[x]=t;}voidDfsintKint  from){     for(intI=p[k];i;i=edge[i].nxt)if(edge[i].to!= from) {deep[edge[i].to]=deep[k]^Edge[i].len;    DFS (EDGE[I].TO,K); }}intWorkintXBasep) {     for(intI= -; ~i;i--) x=min (x,x^P.a[i]); returnx;}voidSolveintLintRintLowintHigh ) {    if(l>r| | Low>high)return; if(low==High ) {         for(inti=l;i<=r;i++) Ans[q[i].i]=min (q[i].x,q[i].x^Value[low]); return; }    intMid=low+high>>1;  for(inti=mid;i>=low;i--)    {        if(I==mid) pre[i]= (Base){0,{0}}; Elsepre[i]=pre[i+1];    Pre[i].ins (Value[i]); }     for(intI=mid+1; i<=high;i++)    {        if(i==mid+1) suf[i]= (Base){0,{0}}; Elsesuf[i]=suf[i-1];    Suf[i].ins (Value[i]); }    inthead=l-1, tail=r+1;  for(inti=l;i<=r;i++)    if(Q[i].l<=mid&&q[i].r>mid) Ans[q[i].i]=work (q[i].x,pre[q[i].l]+SUF[Q[I].R]); Else if(Q[i].r<=mid) u[++head]=Q[i]; Elseu[--tail]=Q[i];  for(inti=l;i<=r;i++) q[i]=U[i];    Solve (L,head,low,mid); Solve (Tail,r,mid+1, high);}intMain () {#ifndef Online_judge freopen ("c.in","R", stdin); Freopen ("C.out","W", stdout); Const Charll[]="%i64d\n";#else    Const Charll[]="%lld\n";#endifN=read (), M=read (), q=read ();  for(intI=1; i<n;i++)    {        intX=read (), Y=read (), z=read ();    Addedge (x, Y, z), Addedge (y,x,z); } DFS (1,1);  for(intI=1; i<=m;i++)    {        intX=read (), Y=read (), z=read (); Value[i]=deep[x]^deep[y]^Z; }     for(intI=1; i<=q;i++)    {        intS=read (), T=read (), L=read (), r=read (); Q[I].I=i,q[i].x=deep[s]^deep[t],q[i].l=l,q[i].r=R; } Solve (1Q1, M);  for(intI=1; i<=q;i++) printf ("%d\n", Ans[i]); return 0;}

#41 Shortest Path (division + linear basis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.