图G是欧拉图,即存在欧拉回路的条件::smile:

1.图是联通的

2.对于无向图,奇度数点个数为0。对于有向图,每个顶点出度等于入度。

 

欧拉回路算法模板(链式前向星和DFS实现):

int ans[N];
int k = 0;
int vis[2*M];

void DFS(int now)
{
    for(int u=first[now];u!=-1;u=G[u].next)
    {
        if(!vis[u])
        {
            vis[u] = 1;    //标记当前边
            vis[u^1] = 1;  //标记反向的另一条边
            DFS(G[u].v);
            ans[k++] = k;
        }
    }
}

 

模板题 POJ 2230 Watchcow

把无向图看成有向图,求其欧拉回路。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100007

struct Edge
{
    int v,next;
}G[N];
int n,m,tot;
int first[10005];
int vis[N];

void addedge(int u,int v)
{
    G[tot].v = v;
    G[tot].next = first[u];
    first[u] = tot++;
}

void DFS(int now)
{
    for(int u=first[now];u!=-1;u=G[u].next)
    {
        if(!vis[u])
        {
            vis[u] = 1;    //标记当前边
            DFS(G[u].v);
        }
    }
    printf("%d\n",now);
}

int main()
{
    int u,v;
    tot = 0;
    scanf("%d%d",&n,&m);
    memset(first,-1,sizeof(first));
    while(m--)
    {
        scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    DFS(1);
    return 0;
}
View Code

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2022-01-20
  • 2021-06-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2022-01-25
相关资源
相似解决方案