Moment of Bloom(思维+生成树+树上两点间路径)

本文介绍了一道图论问题的解决方案,涉及无向图的边权更新和奇偶性判断。当图中各点在询问中出现次数为偶数时,存在使所有边权变为偶数的策略。若出现奇数次,则需要额外的询问。文章通过生成树和路径查找方法来确定解法,并给出了C++代码实现。

Moment of Bloom

[Link](Problem - E - Codeforces)

题意

给你n个点m条边的无向联通图,开始每个边的权重为0,然后给你q次询问每次给两个结点(a, b),让你给a到b的一条简单路径上的所有的边边权加一,是否存在一种走法使得走完q次询问后所有的边权均为偶数,如果不存在那么至少再加几次询问使得成立?

题解

首先考虑什么情况无解。发现当一个点在q次询问中出现了奇数次则一定无解,因为每出现一次则这个点的一个某一个临边的权重加一,出现偶数次则一定有一个临边权重是奇数所以一定无解。

所有点在q次询问中都出现了偶数次一定有解,因为图是一个连通图,我们对这个图做一个生成树,如果生成树有解那么这个图也一定有解。树中任意两点之间的路径唯一,如果所有的点均出现了偶数次,则可普遍表示成 a ↔ b , b ↔ c , c ↔ a a\leftrightarrow b,b\leftrightarrow c, c\leftrightarrow a ab,bc,ca,可以发现每一个这样的环的边都被走了两次,所以边权都是偶数,一定成立。

对于无解的情况每次添加一个询问可以改变两个点的奇偶性,假设奇数点有 s u m sum sum个,只需要添加 s u m / 2 sum/2 sum/2就一定有解。

对于所有的询问的解法,相当于找从 u → v u\rightarrow v uv的一条路径,我们可以用类似求lca的方法不断让u和v往上跳,直到跳到一起,然后再把左右路径合并即可。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e6 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int fa[N], dep[N];
int cnt[N];
vector<int> res[N];
int find(int x) {
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void dfs(int u) {
   for (int i = h[u]; ~i; i = ne[i]) {
       int j = e[i];
       if (j != fa[u]) dep[j] = dep[u] + 1, fa[j] = u, dfs(j); 
   }
}
vector<int> get(int u, int v) {
    vector<int> A, B;
    A.push_back(u), B.push_back(v);
    while(u != v) {
        if (dep[u] < dep[v]) v = fa[v], B.push_back(v);
        else                 u = fa[u], A.push_back(u);
    }
    for (int i = B.size() - 2; i >= 0; i --) A.push_back(B[i]);
    return A; 
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ ) fa[i] = i;
    while (m --) {
        int a, b;
        cin >> a >> b;
        int pa = find(a), pb = find(b);
        if (pa != pb) {
            add(a, b), add(b, a);
            fa[pa] = b;
        }
    }
       for (int i = 1; i <= n; i ++ ) fa[i] = 0;
       dfs(1);
        cin >> m;
        for (int i = 0; i < m; i ++ ) {
            int a, b;
            cin >> a >> b;
            res[i] = get(a, b);
            cnt[a] ^= 1, cnt[b] ^= 1;
        }
        int sum = 0;
        for (int i = 1; i <= n; i ++ )  sum += cnt[i];
        if (sum) {
            cout << "NO" << endl;
            cout << sum / 2 << endl;
        }
        else {
            cout << "YES" << endl;
            for (int i = 0; i < m; i ++ ) {
                cout << res[i].size() << endl;
                for (auto x : res[i]) cout << x << ' ';
                cout << endl;
            }
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值