Induction (a): And look up the set

Source: Internet
Author: User

The round in my eyes. 1: Simple and check set
int fa[maxn];int find(int x) {    return fa[x]==x?x:fa[x]=find(fa[x]);}void un(int x,int y) {    fa[find(x)]=find(y);}
Round 2: Merging by rank
int fa[maxn],siz[maxn];int find(int x) {    return fa[x]==x?x:find(fa[x]);}void un(int x,int y) {    if(siz[x=find(x)]>siz[y=find(y)])        fa[x]=y,siz[y]+=siz[x];    else fa[y]=x,siz[x]+=siz[y];}
Example 1: Repairing highways

Surface

Can be counted as a unicom block, or by rank merge + path compression

Unicom, and together, siz add up, if siz=n, the direct output time can be.

10min cut.

Code:

#include<bits/stdc++.h>const int maxn=1000+5;const int maxm=1e5+5;int fa[maxn],siz[maxn];struct Edge {    int x,y,t;}e[maxm];int find(int x) {    return fa[x]==x?x:fa[x]=find(fa[x]);}void un(int x,int y) {    if(x==y) return ;    fa[x]=y;    siz[y]+=siz[x];}bool cmp(const Edge &a,const Edge &b) {    return a.t<b.t;}int main() {    int n,m;scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1;    for(int i=1;i<=m;i++)        scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].t);    std::sort(e+1,e+m+1,cmp);    for(int i=1;i<=m;i++) {        int xx=find(e[i].x),yy=find(e[i].y);        un(xx,yy);        if(siz[yy]==n) return !printf("%d",e[i].t);    }    printf("-1");    return 0;}

For a long time, I thought it was all part of the collection.

Until:

Round 3: Types and search Sets

Maybe it's called the name, and I don't know.

And the collection can be very good to say that relatives of relatives are relatives of this relationship, but the enemy is a friend of enemies this situation seems powerless.

Using twice times the i+n, I means the same, the relative is the opposite, so there is relative relative is the same relationship.

Example (b): Food chain

Surface

The prey of my prey is my natural enemy. (really violate the laws of nature OK)

So open three times times the size of the and check set, I said the same kind, i+n means prey, i+n+n represents predators.

So it was settled.

#include<bits/stdc++.h>const int maxn=5e4+5;int fa[maxn*3];int find(int x) {    return x==fa[x]?x:fa[x]=find(fa[x]);}int main() {    int n,k;scanf("%d%d",&n,&k);    for(int i=1;i<=n*3;i++) fa[i]=i;    int ans=0;    while(k--) {        int opt,x,y;scanf("%d%d%d",&opt,&x,&y);        if(x>n || y>n) {++ans;continue;}        if(opt==1) {            if(find(x)==find(y+n) || find(y)==find(x+n)) ++ans;            else for(int i=0;i<3;i++) fa[find(x+i*n)]=find(y+i*n);        }        else {            if(find(x)==find(y) || find(x)==find(y+n)) ++ans;            else for(int i=0;i<3;i++) fa[find(x+(i+1)%3*n)]=find(y+i*n);        }    }    printf("%d",ans);    return 0;}
Example (iii): Star Wars

Surface

To build a diagram, the first all destroyed, in the back from the backward time, the practice with the repair of highway computing connected block and similar to the wonderful.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>const int maxn=1e6 +5;struct Edge {int nxt,v,frm;}    E[maxn];int h[maxn],tot;void add_edge (int u,int v) {e[++tot].v=v;    E[tot].nxt=h[u];    H[u]=tot; E[tot].frm=u;} int fa[maxn];int Find (int x) {return fa[x]==x?x:fa[x]=find (fa[x]);}    int Z[maxn];bool vis[maxn];int Ans[maxn];int main () {int n,m;scanf ("%d%d", &n,&m);    for (int i=0;i<n;i++) fa[i]=i,h[i]=-1;        for (int i=0;i<m;i++) {int x,y;scanf ("%d%d", &x,&y);        Add_edge (x, y);    Add_edge (Y,X);    } int k;scanf ("%d", &k);    int cnt=n-k;        for (int i=1;i<=k;i++) {scanf ("%d", z+i);    Vis[z[i]]=true; } for (int i=0;i<m+m;i++) {if (!vis[e[i].frm) && (!VIS[E[I].V]) && (Find (E[I].FRM)!=find (e[i    ].V)) Fa[find (e[i].frm)]=find (E[I].V),--cnt;    } ans[k+1]=cnt;        for (int t=k;t>0;t--) {vis[z[t]]=false;        ++cnt; FoR (int i=h[z[t]];~i;i=e[i].nxt) {if (!vis[e[i].v]) && (Find (E[I].V)!=find (Z[t])) Fa[fin        D (E[I].V)]=find (z[t]),--CNT;    } ans[t]=cnt;    } for (int i=1;i<=k+1;i++) printf ("%d\n", Ans[i]); return 0;}

Mastered these several, feel basic can be said to be more comprehensive grasp and check set.
(actually just after the test field)

Can be persisted and checked.


I have written in my blog.

Re-post the code again

#include <bits/stdc++.h> #define FN "build" const int Maxn=1e5+5;inline int read () {int X;char ch;bool flag=false;w    Hile (!isdigit (Ch=getchar ())) (ch== '-') && (flag=true);    for (x=ch-' 0 '; IsDigit (Ch=getchar ()); x= (x<<3) + (x<<1) +ch-' 0 '); return (FLAG?-X:X);}        namespace HJT {int n;        struct Node {node *ls,*rs;    int SIZ,FA;        }POOL[MAXN*32], *ROOT[MAXN], *zero, *tail=pool;        Node *newnode () {node *nd=++tail;        nd->ls=nd->rs=zero;        nd->siz=nd->fa=0;    return nd;        } node *fmodify (node *p,int l,int r,int pos,int RT) {node *nd=newnode ();            if (l==r) {nd->fa=rt;            nd->siz=p->siz;        return nd;        } int mid=l+r>>1;            if (pos<=mid) {nd->rs=p->rs;        Nd->ls=fmodify (P-&GT;LS,L,MID,POS,RT);            } else {nd->ls=p->ls; Nd->rs=fmodify (p->rs,mid+1,r,pos,rT);    } return nd;        } node *smodify (node *p,int l,int r,int pos,int RT) {node *nd=newnode ();            if (l==r) {nd->fa=p->fa;            nd->siz=p->siz+rt;        return nd;        } int mid=l+r>>1;            if (pos<=mid) {nd->rs=p->rs;        Nd->ls=smodify (P-&GT;LS,L,MID,POS,RT);            } else {nd->ls=p->ls;        Nd->rs=smodify (P-&GT;RS,MID+1,R,POS,RT);    } return nd;        } int fquery (Node *nd,int l,int r,int pos) {if (l==r) return nd->fa;        int mid=l+r>>1;        if (Pos<=mid) return fquery (Nd->ls,l,mid,pos);    else return fquery (Nd->rs,mid+1,r,pos);        } int squery (Node *nd,int l,int r,int pos) {if (l==r) return nd->siz;        int mid=l+r>>1;        if (Pos<=mid) return squery (Nd->ls,l,mid,pos);    else return squery (Nd->rs,mid+1,r,pos); } int Find (int i,node *Nd,int x) {int ff=fquery (ND,1,N,X);        if (ff==x) return x;    return find (I,ND,FF);        } node *build (int l,int r) {node *nd=newnode ();            if (l==r) {nd->siz=1;            nd->fa=l;        return nd;        } int mid=l+r>>1;        Nd->ls=build (L,mid);        Nd->rs=build (MID+1,R);    return nd;        } void Work () {int m=read (); N=read ();        int lastans=0,tot=0;        Zero=++tail;        zero->ls=zero->rs=zero;        zero->fa=zero->siz=0;        Root[0]=build (1,n);            for (int i=1;i<=m;i++) {int opt=read ();            int U=lastans+read ();            int V=lastans+read ();                if (opt==1) {u=find (tot,root[tot],u);                V=find (TOT,ROOT[TOT],V);                    if (u==v) {++tot;                    ROOT[TOT]=ROOT[TOT-1];                Continue } int Siz1=squery (ROOT[TOT],1,N,V);                int Siz2=squery (ROOT[TOT],1,N,U);                    if (SIZ1&GT;SIZ2) {std::swap (SIZ1,SIZ2);                Std::swap (U,V);                } ++tot;                Root[tot]=fmodify (Root[tot-1],1,n,v,u);            Root[tot]=smodify (ROOT[TOT],1,N,U,SIZ1);                } else {int ff=find (U,ROOT[U],V);                int Siz1=squery (ROOT[U],1,N,FF);                Root[tot+1]=root[tot];                ++tot;                LASTANS=SIZ1;            printf ("%d\n", SIZ1);    }}}}int Main () {Freopen (FN ". In", "R", stdin);    Freopen (FN ". Out", "w", stdout);    Hjt::work (); return 0;}

Can be persisted and check the set really do not know is not counted and check set, really the difference is too big.

Induction (a): And look up the set

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.