首页 > 其他 > 详细

欧拉回路 & 欧拉路径

时间:2018-09-21 23:51:30      阅读:161      评论:0      收藏:0      [点我收藏+]

欧拉路径 & 欧拉回路

概念

欧拉路径: 如果图 G 种的一条路径包括所有的边,且仅通过一次的路径.
欧拉回路: 能回到起点的欧拉路径.
混合图: 既有无向边又有无向边的图.

板子题
[USACO Section 3.3] 骑马修栅栏 Riding the Fences


Code

#include<bits/stdc++.h>
using namespace std;
int g[1501][1501];
int du[1501],sta[1501];
int n,e,top,i,j,x,y,st=1,m,mi,p;
void dfs(int i)
{
    for(int j=1;j<=m;++j)
        if(g[i][j])
        {
            g[i][j]--;
            g[j][i]--;
            dfs(j);
        }
    sta[++top]=i;
}

int main()
{
    scanf("%d",&e);
    for(i=1;i<=e;++i)
    {
        scanf("%d%d",&x,&y);
        ++g[y][x]; ++g[x][y];
        du[x]++; du[y]++;
        m=max(max(x,y),m);
    }
    for(i=1;i<=m;++i)
        if(du[i]%2)
        {st=i;break;}
    dfs(st);
    for(i=top;i>=1;--i)
        printf("%d\n",sta[i]);
    return 0;
}

欧拉回路 & 欧拉路径

原文:https://www.cnblogs.com/Kv-Stalin/p/9688710.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!