HDU 1811 Ufset Synthesis

Source: Internet
Author: User

Long time no knock, Rusty.

Test instructions: Give a diagram, the requirement is no ring, and can start from a point of a stroke finish all points. The qualitative judgment comes out.

To restore the algorithm, to judge the Tarjan of the strong connectivity of the contradiction, can not be used to determine the simulation.

#include <iostream> #include <cstdio> #include <set> #include <stack> #include <vector>  Using namespace Std;int fa[30105]; int n,m;    int ind[30028];int Find (int x) {if (x==fa[x]) return x;    Fa[x]=find (Fa[x]); return fa[x];} int e[100154][3];int head[30023];int nume;void inline Adde (int i,int j) {e[nume][0]=j;e[nume][1]=head[i];head[i]=nume+ +;}    struct zz{int x, y; char t;}; int ins[20009];stack<int>sta;int low[20025];int dfn[20025];int times;int vis[20025];int scc[20251];int numb=0;    void Tarjan (int u) {dfn[u]=low[u]=times++;    Ins[u]=1;    Sta.push (U);        for (int j=head[u];j!=-1;j=e[j][1]) {int v=e[j][0];            if (!vis[v]) {vis[v]=1;            Tarjan (v);        if (Low[u]>low[v]) low[u]=low[v];        } else if (Ins[v]) {if (Dfn[v]<low[u]) low[u]=dfn[v];        }} if (Low[u]==dfn[u]) {numb++;        int cur;            do {cur=sta.top (); Sta.Pop ();            ins[cur]=0;        Scc[cur]=numb;    }while (Cur!=u);    }}int judge () {int ss=-1;    int Cnt=0;int numv=0;    set<int>temp;        for (int i=0;i<n;i++) {int tx=find (i);        Temp.insert (TX);           if (ind[tx]==0&&tx==i)//start without adding this back condition, WA took several rounds ...               {ss=tx;           cnt++;     }} numv=temp.size ();        for (int i=0;i<n;i++) {int tx=find (i);            if (!vis[tx]) {vis[tx]=1;        Tarjan (TX);  }} if (NUMB!=NUMV) return 1;    There is a contradiction if (cnt!=1) return 2;    int cur=ss;      while (numv!=0) {cnt=0;        for (int j=head[cur];j!=-1;j=e[j][1]) {int to=e[j][0];        ind[to]--;            if (ind[to]==0) {cur=to;        cnt++;      }} if (cnt>1) return 2;    numv--; } return 0;}             void Init () {for (int i=0;i<10005;i++) {fa[i]=i;             Head[i]=-1; Low[i]=dfn[i]=0;         scc[i]=vis[i]=ins[i]=ind[i]=0; } numb=times=nume=0;}      int main () {while (~scanf ("%d%d", &n,&m)) {int marks=0;      int Aa,bb;char temp;        Init ();      vector<zz>my;        for (int i=0;i<m;i++) {cin>>aa>>temp>>bb;            if (temp== ' = ') {int xx=find (AA);            int Yy=find (BB);        if (xx!=yy) fa[xx]=yy;            } else {ZZ cur;           Cur.x=aa;cur.y=bb;cur.t=temp;        My.push_back (cur); }} for (int i=0;i<my.size (); i++) {if (my[i].t== ' < ') {int Xx=find (my              [i].x];              int Yy=find (MY[I].Y);              if (xx==yy) Marks=1;               Adde (YY,XX);            ind[xx]++;              } else {int xx=find (my[i].x);               int Yy=find (MY[I].Y);              if (xx==yy) Marks=1;              Adde (XX,YY);          ind[yy]++; }      }      int Ans=judge ();       if (n==0) ans=0;       if (ans==2&&marks!=1) printf ("uncertain\n"); else if (ans==1| |       marks==1) printf ("conflict\n");  else printf ("ok\n"); } return 0;}


Traditional method: Ufset topology:

#include <iostream> #include <cstdio> #include <set> #include <stack> #include <vector>  using namespace Std;const int Maxn=100004;int FA[MAXN]; int n,m;    int Ind[maxn];int sums=0;vector<vector<int> >e (MAXN); int find (int x) {if (x==fa[x]) return x;    Fa[x]=find (Fa[x]); return fa[x];}    struct zz{int x, y; char t;};    int judge () {int ans=0;    stack<int>mys;        for (int i=0;i<n;i++) {int tx=find (i);           if (ind[tx]==0&&tx==i) {Mys.push (TX);       }} while (!mys.empty ()) {int cnt=0;       int Cur=mys.top ();       if (Mys.size ()!=1) ans=2;       Mys.pop ();      sums--;        for (int i=0;i<e[cur].size (); i++) {int to=e[cur][i];        ind[to]--;        if (ind[to]==0) {Mys.push (to);    }}} if (sums!=0) Ans=1; return ans;}            void Init () {for (int i=0;i<maxn-1;i++) {fa[i]=i;          ind[i]=0;  E[i].clear (); } sums=n;}      int main () {while (~scanf ("%d%d", &n,&m)) {int Aa,bb;char temp;        Init ();      vector<zz>my;        for (int i=0;i<m;i++) {cin>>aa>>temp>>bb;            if (temp== ' = ') {int xx=find (AA);            int Yy=find (BB);        if (xx!=yy) {fa[xx]=yy;sums--;}            } else {ZZ cur;           Cur.x=aa;cur.y=bb;cur.t=temp;        My.push_back (cur); }} for (int i=0;i<my.size (); i++) {if (my[i].t== ' < ') {int Xx=find (my              [i].x];               int Yy=find (MY[I].Y);               E[yy].push_back (XX);            ind[xx]++;              } else if (my[i].t== ' > ') {int xx=find (my[i].x);                int Yy=find (MY[I].Y);              E[xx].push_back (yy);          ind[yy]++;      }} int Ans=judge ();       if (n==0) ans=0; if (ans==2) printf ("UnceRtain\n ");       else if (ans==1) printf ("conflict\n");  else printf ("ok\n"); } return 0;}



HDU 1811 Ufset Synthesis

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.