acwing 1184. 欧拉回路 【二刷顶级欧拉回路】

一个图论题的一些细节:

开始跑算法的源点是哪?起点是什么?是1,是随便找一个能连通的点,然后break出来,还是要把所有连通块都跑一边?

cnt记录点还是边,st数组记录点还是边。需不需要记录的问题,如果需要的话,记录了就一定对应着当cnt < n | m的情况,讨论了没有?

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e5 + 10;
const int M = 2 * 1e5 + 10;
int n, m, t;
int h[N], ne[M<<1], e[M<<1], idx;
int in[N], out[N];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

bool vis[M<<1];
int ans[M<<1], cnt;
void dfs(int u)
{
    for (int &i = h[u]; ~i; ) // i 是边号
    {
        if(vis[i]) {
            i = ne[i];
            continue;
        }

        vis[i] = 1;
        if(t == 1) vis[i ^ 1] = 1;

        int tmp;
        if(t == 1) {
            tmp = i / 2 + 1;
            if(i & 1) tmp = -tmp;
        } else tmp = i + 1;

        int j = e[i];
        i = ne[i];
        dfs(j);
        ans[cnt ++ ] = tmp;
    }
}


int main(){
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    memset(h, -1, sizeof h);
    cin >> t;
    cin >> n >> m;
    for (int i = 1, a, b; i <= m; i ++ ) {
        cin >> a >> b;
        add(a, b), out[a] ++, in[b] ++ ;
        if(t == 1) add(b, a);
    }
    if(t == 1) {
        for (int i = 1; i <= n; i ++ ) {
            if(in[i] + out[i] % 2 == 1) {
                cout << "NO\n";
                return 0;
            }
        }
    } else {
        for (int i = 1; i <= n; i ++ ) {
            if(in[i] != out[i]) {
                cout << "NO\n";
                return 0;
            }
        }
    }
    for (int i = 1; i <= n; i ++ ) {
        if(h[i] != -1) {
            dfs(i);
            break;
        }
    }

    if(cnt < m) {
        cout << "NO\n";
        return 0;
    } else {
        cout << "YES\n";
        for (int i = cnt - 1; i >= 0; i -- ) cout << ans[i] << ' ';
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值