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

来源:互联网 发布:管家婆软件售后电话 编辑:程序博客网 时间:2024/05/16 05:22

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;} 
原创粉丝点击