Algorithm review--Euler circuit (uoj117)

Source: Internet
Author: User

Topic:

Exercises

Euler's Loop correlation theorem (see other materials for definitions and certifications):

1. Euler circuit

(1) The direction graph: All points of the degree is equal to the degree of the graph is Eulerian graph (existence of the Euler circuit) necessary and sufficient conditions.

(2) No direction diagram: The degree of all points is even the necessary and sufficient conditions for the graph to be Eulerian graph (existence of the Euler circuit).

2. Euler pathway

(1) The direction of the graph: In addition to two points (one point out of the degree of +1==, the other point into the degree of +1==) the other point out degree is equal to the degree of the graph is a semi-Eulerian graph (existence Oraton road) necessary and sufficient conditions.

(2) No direction diagram: In addition to two points (two points are odd) the other point of the degree is even for the figure is half Eulerian graph (existence Oraton Road) sufficient and necessary conditions.

The above theorem is used to determine whether a Euler circuit or a path exists.

Then there are two inferences:

Yes, that's it. • Back to this problem, a very naked template problem ... However, the data of the Uoj are taught to behave ·

Pay attention to the, or it will time out. Optimized with cur similar to network streams (see Code for details)

Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<ctime>#include<cctype>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intn=1e5+5;Const intm=5e5+5;intfirst[n],go[m*2],next[m*2],tot=0;intn,m,t,ru[n],chu[n],stack[m*2],cnt;BOOLVisit[m];inlinevoidComb (intAintb) {next[++tot]=first[a],first[a]=tot,go[tot]=b;} InlinevoidDFS1 (intu) {   for(int&e=first[u];e;e=Next[e]) {    if(!Visit[e]) {Visit[e]=true; if(e%2==1) Visit[e+1]=true; Elsevisit[e-1]=true; intt=e;      DFS1 (Go[e]); stack[++cnt]=T; }}}inlinevoidDFS2 (intu) {   for(int&e=first[u];e;e=Next[e]) {    if(!Visit[e]) {Visit[e]=true; intt=e;      DFS2 (Go[e]); stack[++cnt]=T; }  }}intMain () {//freopen ("a.in", "R", stdin);scanf"%d",&T); intb; scanf ("%d%d",&n,&m); if(t==1)//the situation of the non-direction diagram  {      for(intI=1; i<=m;i++) {scanf ("%d%d",&a,&b);      Comb (A, b);      Comb (b,a); RU[B]++; Chu[a]++; }     for(intI=1; i<=n;i++)      if((Ru[i]+chu[i])%2==1) {cout<<"NO"<<Endl; return 0; }     for(intI=1; i<=n;i++)    {      if(First[i]) {DFS1 (i);  Break; }    }    if(cnt!=m) {cout<<"NO"<<Endl; return 0; } cout<<"YES"<<Endl;  for(inti=cnt;i>=1; i--)    {        if(stack[i]%2==1) cout<< (stack[i]+1)/2<<" "; Elsecout<<stack[i]/2*(-1) <<" "; }    return 0; }  Else  {     for(intI=1; i<=m;i++) {scanf ("%d%d",&a,&b);      Comb (A, b); RU[B]++; Chu[a]++; }     for(intI=1; i<=n;i++)      if(ru[i]!=Chu[i]) {cout<<"NO"<<Endl; return 0; }      for(intI=1; i<=n;i++)     {       if(First[i]) {DFS2 (i);  Break; }     }    if(cnt!=m) {cout<<"NO"<<Endl; return 0; } cout<<"YES"<<Endl;  for(inti=cnt;i>=1; i--) cout<<stack[i]<<" "; return 0; }}

Algorithm review--Euler circuit (uoj117)

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.