Codeforces723E - One-Way Reform(Euler回路)

22 篇文章 0 订阅
1 篇文章 0 订阅

题目链接

http://codeforces.com/contest/723/problem/E

思路

首先基于下面两个事实:
1. 连通图度数为奇数的点一定为偶数个(每条边都能贡献2个度数,因此总度数一定为偶数,从而度数为奇数的点一定为偶数个)
2. Euler回路中所有点的入度都等于出度
这道题要求的就是所有边定向后入度等于出度的点,由此想到可以跑Euler回路,因为原图中存在度数为奇数的点,可以新建一个节点n + 1连接这些点,所有点的度数变成偶数(也可以由性质1,将所有奇数度数点相连),在跑欧拉回路的时候对所有边定向即可

细节

因为要删掉已经跑过的边,于是用set来存边

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)

const int maxn = 205;
set<int> s[maxn];
vector<PII> ans;
int n, m;

void init() {
    for (int i = 0; i < maxn; i++) s[i].clear();
    ans.clear();
}

void dfs(int u) {
    set<int>::iterator it = s[u].begin();
    while (s[u].size()) {
        int v = *s[u].begin();
        s[u].erase(v); s[v].erase(u);
        ans.push_back(mp(u, v));
        dfs(v);
    }
}

int main() {
    int T;
    scan(T);
    while (T--) {
        init();
        scan2(n, m);
        for (int i = 0; i < m; i++) {
            int u, v;
            scan2(u, v);
            s[u].insert(v); s[v].insert(u);
        }
        for (int i = 1; i <= n; i++) {
            if (s[i].size() & 1) 
                s[n + 1].insert(i), s[i].insert(n + 1);
        }
        printf("%d\n", n - s[n + 1].size());
        for (int i = 1; i <= n; i++) dfs(i);
        for (int i = 0; i < ans.size(); i++) {
            PII t = ans[i];
            if (t.first == n + 1 || t.second == n + 1) continue;
            printf("%d %d\n", t.first, t.second); 
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值