Bzoj 3569 dzy Loves Chinese II Gaussian elimination element

Source: Internet
Author: User

Topic: Given a "魞 歄 connected graph", many times asked when the figure of a K-bar disappears when the graph is linked to force online

We find any spanning tree of this graph and assign its weight to a random number for each non-tree edge.

For each tree edge we set the weight of this tree to be the XOR of all Benquan that cover the edge of this tree.

Then the graph is not connected when and only if a tree edge is deleted and all the edge sets that cover the edge of this tree are treated as an XOR and zero for all the edge sets that cover the edge of the tree just now.

The problem then turns into the existence of an XOR and zero subset of the edges for a given K-bar edge. Decisive Gaussian elimination due to the use of randomization so the collision rate is very low

A good way to learn ... The idea was ingenious.

Remember to set a random number seed or you'll get stuck.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 100100using namespace std;struct abcd{int to,next;} Table[1001001];int head[m],tot=1;int n,m,q,last_ans;int a[500500],b[m];int stack[20],top;void Add (int x,int y) {table[    ++tot].to=y;    TABLE[TOT].NEXT=HEAD[X]; Head[x]=tot;}    void DFS1 (int x,int from) {static bool v[m];    int i;    V[x]=1;        for (I=head[x];i;i=table[i].next) {if (table[i].to==from) continue;        if (!v[table[i].to]) DFS1 (table[i].to,x);            else if (!~a[i>>1]) {A[i>>1]=rand ();            b[table[i].to]^=a[i>>1];        b[x]^=a[i>>1];    }}}void DFS2 (int x,int from) {static bool v[m];    int i;    V[x]=1;            for (I=head[x];i;i=table[i].next) if (!v[table[i].to]) {DFS2 (table[i].to,x);            a[i>>1]=b[table[i].to];        B[X]^=B[TABLE[I].TO]; }}void gauss_elimination() {int i,j,k=0;        for (J=1<<30;j;j>>=1) {for (i=k+1;i<=top;i++) if (stack[i]&j) break;        if (i==top+1) continue;        Swap (stack[i],stack[++k]);    for (i=1;i<=top;i++) if (stack[i]&j&&i!=k) stack[i]^=stack[k];         }}int Main () {srand (19980402);    int i,x,y,k;    cin>>n>>m;    for (i=1;i<=m;i++) scanf ("%d%d", &x,&y), add (x, y), add (y,x);    memset (a,-1,sizeof a);    DFS1 (1,0);    DFS2 (1,0);    cin>>q;        for (i=1;i<=q;i++) {scanf ("%d", &k); top=0;        for (; k;k--) scanf ("%d", &x), Stack[++top]=a[x^last_ans];        Gauss_elimination ();        last_ans+= (BOOL) stack[top]; printf ("%s\n", Stack[top]? "    Connected ":" Disconnected "); }}



Bzoj 3569 dzy Loves Chinese II Gaussian elimination element

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.