首页 > 其他分享 >sol. CF1680F Lenient Vertex Cover

sol. CF1680F Lenient Vertex Cover

时间:2023-09-10 23:22:44浏览次数:35  
标签:int Cover Vertex dep tag 奇环 otimes Lenient id

CF1680F Lenient Vertex Cover

下面用 \(G\) 表示一个环的边集,记作环 \(G\)。

我们令一个环 \(G\) 的价值为它经过的返祖边数量,记作 \(Z(G)\),下面给出核心结论:

若存在一条边 \(e_0\) 经过所有 \(Z(G) = 1\) 的奇环,且不经过任意一个 \(Z(G) = 1\) 的偶环,那么 \(e_0\) 经过所有奇环,且不经过任意一个偶环


下面给出简单的证明:

定义两个环的异或值 \(G = G_1 \otimes G_2\) 满足:

\[G = \{ e \, \vert \, (e \in G_1 \land e \not\in G_2) \lor (e \in G_2 \land e \not\in G_1)\} \]

该运算显然满足交换律和结合律。

引理:对于任意环 \(G\),均可以被唯一分解为 \(G = G_1 \otimes G_2 \dots \otimes G_m\),其中 \(Z(G) = m\),满足 \(\forall 1 \le i \le m\),都有 \(Z(G_i) = 1\)。

证明:考虑数学归纳法,当 \(m = 1\) 时显然成立。下面假定当 \(m = k\) 时成立,则当 \(m = k + 1\) 时,容易发现 \(G_{k + 1}\) 和 \(G \otimes G_1 \otimes G_2 \dots \otimes G_k\) 显然相等,若不等,则通过 \(G_1, G_2, \dots, G_{k + 1}\) 中的返祖边显然不能成环,与环 \(G\) 矛盾。(具体的严谨证明笔者也不太会,只能感性理解)

若对于一个环 \(G = G_1 \otimes G_2 \dots \otimes G_m\),容易发现原环上的边由所有小环的边之和减去重合的边,即 \(|G| = \sum\limits_{i = 1}^{m} |G_i| - \Delta\), 且 \(2 \mid \Delta\),那么环 \(G\) 的奇偶性只和 \(G_1, G_2 \dots G_m\) 中的奇环数目 \(x\) 有关,具体的:

  • 若 \(2 \mid x\),那么环 \(G\) 为偶环,若存在一条边 \(e_0\) 经过所有的奇环,那么 \(e_0\) 必然不经过 \(G\)。

  • 若 \(2 \nmid x\),那么环 \(G\) 为奇环,若存在一条边 \(e_0\) 经过所有的奇环,那么 \(e_0\) 必然经过 \(G\)。

综上所述,原结论显然成立。


接下来的处理就很简单,只需要用树上差分统计每条边经过几个奇环和偶环,最后寻找是否存在一条 \(e_0 = (u_0, v_0)\) 满足经过所有奇环,且不经过一个偶环的边。

输出方案的话,若原图为二分图,直接染色即可,否则删掉 \(e_0\) 后,钦定 \(col_{u_0} = 1\) 染色即可,注意多测要清空。

时间复杂度为 \(\mathcal{O}(n + m)\)

代码
#include<bits/stdc++.h>
#define MULT_TEST 1
using namespace std;
inline int read() {
    int w = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        w = (w << 1) + (w << 3) + ch - 48;
        ch = getchar();
    }
    return w * f;
}
inline void Solve() {
    int n, m, sum = 0, t = 0, opt = -1;
    n = read(); m = read();
    vector<int> dep(n + 1), ans(n + 1), vis(n + 1), tag(n + 1), h(m + 1);
    vector<vector<pair<int, int>>> e(n + 1), E(n + 1);
    for (int i = 1; i <= m; i++) {
        int u = read(), v = read();
        e[u].push_back({v, i});
        e[v].push_back({u, i});
    }
    function<void(int, int)> DFS1 = [&](int x, int fa) {
        vis[x] = 1;
        dep[x] = dep[fa] + 1;
        for (auto [u, id] : e[x]) {
            if (u == fa) continue;
            if (vis[u]) {
                if (dep[u] > dep[x]) continue;
                if ((dep[x] - dep[u]) & 1) tag[u]++, tag[x]--, h[id]--;
                else sum++, tag[u]--, tag[x]++, h[id]++;
            }
            else {
                E[x].push_back({u, id});
                DFS1(u, x);
            }
        }
    };
    function<void(int, int)> DFS2 = [&](int x, int id2) {
        for (auto [u, id] : E[x]) {
            DFS2(u, id);
            tag[x] += tag[u];
        }
        if (id2 > 0) h[id2] = tag[x];
    };
    bool flag = 1;
    function<void(int, int)> DFS3 = [&](int x, int col) {
        vis[x] = 1;
        ans[x] = col;
        for (auto [u, id] : e[x]) {
            if (vis[u]) {
                if (ans[u] == col) flag = 0;
                continue;
            }
            if (id == opt) continue;
            DFS3(u, col ^ 1);
        }
    };
    DFS3(1, 1);
    if (flag) {
        printf("YES\n");
        for (int i = 1; i <= n; i++) printf("%d", ans[i]);
        puts("");
        return ;
    }
    vis.clear(); vis.resize(n + 1);
    DFS1(1, 1); DFS2(1, -1);
    for (int i = 1; i <= n; i++)
        for (auto [u, id] : e[i]) 
            if (h[id] == sum) opt = id, t = i;
    if (opt == -1) printf("NO\n");
    else {
        vis.clear(); vis.resize(n + 1);
        DFS3(1, 1);
        printf("YES\n");
        for (int i = 1; i <= n; i++) printf("%d", ans[i] ^ (ans[t] == 0));
        puts("");
    }
}
signed main() {
    int T = 1;
#if MULT_TEST
    T = read();
#endif 
    while (T--) Solve();
    return 0;
}

标签:int,Cover,Vertex,dep,tag,奇环,otimes,Lenient,id
From: https://www.cnblogs.com/WTR2007/p/17692269.html

相关文章

  • easyrecovery 2023年最好用的数据恢复软件
    EasyRecovery是一款操作简单、功能强大数据恢复软件,通过easyrecovery可以从硬盘、光盘、U盘、数码相机、手机等各种设备中恢复被删除或丢失的文件、图片、音频、视频等数据文件。easyrecovery数据恢复软件,是国内顶尖工作室的技术杰作。它是一个硬盘数据恢复工具,能够帮你恢复丢失的......
  • Proj CDeepFuzz Paper Reading: COMET: Coverage-guided Model Generation For Deep L
    Abstract背景:已有的方法(Muffin,Lemon,Cradle)cancoveratmost34.1%layerinputs,25.9%layerparametervalues,and15.6%layersequences.本文:COMETGithub:https://github.com/maybeLee/COMETBugType:Crash,NaN,inconsistencybetweentheTensorFlowlibrar......
  • Proj CDeepFuzz Paper Reading: IvySyn: Automated Vulnerability Discovery in Deep
    Abstract本文:IvySynTask:discovermemoryerrorvulnerabilitiesinDLframeworksBugType:memorysafetyerrors,fatalruntimeerrorsMethod:利用nativeAPIs中静态写明的类型信息在low-levelkernelcode上执行type-awaremutation-basedfuzzingsynthesizeProofof......
  • Recovery下显示本地化文字分析
    在本文中我们主要分析google在recovery中根据地区信息(locale)的不同,来显示国际化文字的机制。在recovery下进行升级时,不论是ota升级还是sideload升级方式,如果当前系统区域设置为中文,在进度条上面就会看到“正在安装系统更新…”这几个字,如果改变系统的区域,在这种情况下会看到下......
  • Android Recovery UI浅析1——概览
    最近在作一个在recovery中显示文字的工作,所以对这块研究较多,现在把研究的一点新的结果分享出来,如果有什么错误也欢迎大家在下面评论。 Android的Recovery中,利用 boottable/recovery下的minui库作为基础,采用的是直接存取framebuffer的方式,来完成recovery中所需的各种UI的绘制。......
  • Could not autowire. No beans of ‘DiscoveryClient‘ type found.
    一、导错了包DiscoveryClient对应有两个包:org.springframework.cloud.client.discovery.DiscoveryClient;com.netflix.discovery.DiscoveryClient;目前导入的包是:改成第一个包,发现不再报红了。......
  • covers和contains的区别?
    covers:b上的每个点都在a上(边界和内部),且所有点都不在a外部。属于相交的一种。对应九交模型参数为:T*****FF*\*T****FF*\***T**FF*\****T*FF*注意和contains的区别。参考:https://blog.csdn.net/whl0071/article/details/127127256 参考2:https://www.cnblogs.com/oloroso/p/1429......
  • playcover for mac1.1.1最新激活下载 中文版介绍
    PlayCoverforMac是一个允许您在macOS上旁加载iOS应用程序的软件(目前是arm,将测试Intel支持)。通过鼠标、键盘和控制器支持在M1Mac上运行iOS应用和游戏。软件地址:看置顶贴应用介绍PlayCover是一个允许您在macOS上加载iOS应用程序的项目(目前只支持arm,不久将测试Int......
  • 1154 Vertex Coloring
    题目A propervertexcoloring isalabelingofthegraph'sverticeswithcolorssuchthatnotwoverticessharingthesameedgehavethesamecolor.Acoloringusingatmost k colorsiscalleda(proper) k-coloring.Nowyouaresupposedtotellifagi......
  • VisionPro 图像转换工具ImageCovertTool
    前言众所周知,VisionPro是一款功能强大的机器视觉软件,用于开发和部署机器视觉应用程序。其中ImageConvertTool是其中一个重要的工具,用于图像转换和处理。本文将介绍如何使用ImageConvertTool进行图像转换,并探讨其背后的原理。写之前先吐槽一下,引出自己的原因,哈哈哈(当然一......