Correct posture of poi[2011]garbage Euler circuit

Source: Internet
Author: User

The main idea is to say that given a picture, each hop side has a number $0$ or $1$, each can be a simple ring on all sides $\oplus 1$, the construction scheme so that the number of each hop edge is a value, the scheme involved in the edge of not more than 5m

If the two rings in the scheme have intersecting parts, the intersecting parts do not change, and then 2 rings are merged into one ring. So there's a scheme that doesn't intersect all the rings.

Then delete the need to go through an even number of edges, for each unicom block for the Euler circuit (not the road), and then use the stack to get out all the ring, time complexity $o (n + m) $

TLE ..., for the card constant also angry write manual stack ... And then I found the Euler circuit section, which I wrote at the beginning.

void dfs (int  u) {    1;      for (int i = head[u], V; v = e[i].node, i; i = e[i].next)         if (! Flag[i])        {            11;            DFS (v);            mem[++mem[0]] = i;        }}

Always think that this is right, and then found that they are too naive, each point is to start from scratch to find the edge is not marked, the complexity can degenerate.

The correct posture should be to record which side of the last marker of each point is, and then start scanning from that edge (manual stack version)

voidDfsintRT) {S[top=1].u = RT, s[1].I = Head[rt], s[1].t =0;  while(top) {intU = s[top].u, i = s[top].i, tmp = top; s[top].t++; if(s[top].t = =2) {mem[++mem[0]] = i; s[top].t =1;} I=Cur[u]; Vis[u]=1;  for(intV v = e[i].node, I; i =e[i].next)if(!Flag[i]) {Flag[i]= flag[i ^1] =1; s[++top].u = v; S[TOP].I =Cur[v]; S[TOP].T=0; S[TMP].I = i; Cur[u] =i;  Break; }        if(TMP = = top) top--; }}
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <vector>7#include <ctime>8 using namespacestd;9 #defineRep (I, L, R) for (int i = l; I <= R; i++)Ten #defineDrep (i, R, L) for (int i = r; I >= l; i--) OnetypedefLong Longll; A Const intN = 1e5 +8, M = 1e6 +8; - intN, M, tot, Head[n], mem[m <<1], top, SZ, Cnt[n], q[m <<1], Deg[n], vis[n], cur[n]; - BOOLFlag[m <<1]; the intAns[m *5], Len, sta[m]; - intXGW; - //double T1, T2; - intGetint () + { -     CharCintnum =0, W =1; +      for(c = GetChar ();!isdigit (c) && c! ='-'; c =GetChar ()); A     if(c = ='-') C = GetChar (), W =-1; at      for(; IsDigit (c); c = GetChar ()) num = num *Ten+ C-'0'; -     returnNum *W; - } - structedge{intNext, node;} E[m <<1]; -InlinevoidAddintXinty) - { inE[++tot].next = Head[x], head[x] = tot, E[tot].node =y; -E[++tot].next = Head[y], head[y] = tot, E[tot].node =x; to } + structRGZ - { the     intu, I, t; * }s[m]; $ voidDfsintRT)Panax Notoginseng { -S[top =1].u = RT, s[1].I = Head[rt], s[1].t =0; the      while(top) +     { A         intU = s[top].u, i = s[top].i, tmp = top; s[top].t++;  the         if(s[top].t = =2) {mem[++mem[0]] = i; s[top].t =1;} +i =Cur[u]; -Vis[u] =1; $          for(intV v = e[i].node, I; i =e[i].next) $         if(!Flag[i]) -         { -Flag[i] = flag[i ^1] =1; thes[++top].u = v; S[TOP].I =Cur[v]; -s[top].t =0; S[TMP].I = i; Cur[u] =i;Wuyi              Break; the         } -         if(TMP = = top) top--; Wu     } - } AboutInlineBOOLCalcintRT) $ { -mem[0] =0; top =0; - DFS (RT); -     if(!mem[0])return 1; AQ[top =1] = e[mem[mem[0]] ^1].node; cnt[q[1]]++; +Drep (i, mem[0],1) the     { -         intU =E[mem[i]].node; $cnt[u]++;  the         if(Cnt[u] = =2) the         { the++sz; Ans[++len] = u; STA[SZ] =Len; the              while(Cnt[u] >1) -Ans[++len] = Q[top], Cnt[q[top]]--, top--; in         } theQ[++top] =u; the     } About     if(Top >1)return 0; the } the voidSolve () the { +Rep (I,1, N) -     if(Deg[i] &1) the     {Bayiprintf"nie\n"); the         return; the     } -Rep (I,1, n) cur[i] =Head[i]; -Rep (I,1, N) the         if(Deg[i] &&!Vis[i]) the             if(!Calc (i)) the             { theprintf"nie\n"); -                 return; the             } theprintf"%d\n", SZ); Sta[sz +1] = len +1; theRep (I,1, SZ)94     { theprintf"%d", Sta[i +1]-Sta[i]-1); theRep (J, Sta[i], Sta[i +1] -1) printf ("%d", Ans[j]); thePutchar ('\ n');98     } About } - intMain ()101 {102     //freopen ("Input.txt", "R", stdin);103     //freopen ("Output.txt", "w", stdout);104scanf"%d%d", &n, &m); tot =1; theRep (I,1, M)106     {107         intU = getint (), V = getint (), x = Getint (), y =getint ();108         if(Y! = x) Add (U, v), deg[u]++, deg[v]++;109     } the solve ();111     //fclose (stdin); fclose (stdout); the     return 0;113}
Poi2011garbage

Correct posture of poi[2011]garbage Euler circuit

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.