[杂题]Codeforces 860D. Wizard's Tour

Solution

首先答案的上界是 m2
考虑 dfs 树。
一个点如果连向子树的点的边有偶数条,那么把这些点一对一的加到答案里面。
如果只有奇数条,那么就要考虑把连向父节点的边也加进来。
dfs 中记录这些东西就好了。
这样就得到的答案为 m2
学到了 tuple 怎么用。。
这个 idea 好多次用到啦。比如说这个题,还有CF上次打的某道vp (哪次忘了呀。。)

#include <bits/stdc++.h>
using namespace std;

const int N = 202020;
const int INF = 1 << 30;
typedef long long ll;
typedef tuple<int, int, int> Tuples;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int n, m, x, y, z, clc;
vector<int> G[N];
int deg[N], pre[N];
vector<Tuples> ans;

inline void AddEdge(int from, int to) {
    G[from].push_back(to); G[to].push_back(from);
}
inline int dfs(int u, int fa) {
    pre[u] = ++clc;
    int lst, ned, d = 0;
    for (int to: G[u]) {
        if (to == fa) continue;
        if (!pre[to]) ned = dfs(to, u);
        else ned = (pre[to] > pre[u]);
        if (!ned) continue;
        if (d) ans.push_back(Tuples(lst, u, to));
        d ^= 1; lst = to;
    }
    if (d && fa) {
        ans.push_back(Tuples(fa, u, lst));
        return 0;
    }
    return 1;
}

int main(void) {
    freopen("1.in", "r", stdin);
    read(n); read(m);
    for (int i = 1; i <= m; i++) {
        read(x); read(y);
        AddEdge(x, y);
        ++deg[x]; ++deg[y];
    }
    for (int i = 1; i <= n; i++)
        if (!pre[i]) dfs(i, 0);
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); i++) {
        tie(x, y, z) = ans[i];
        printf("%d %d %d\n", x, y, z);
    }
    return 0;
} 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值