Codeforces 557D Vitaly and Cycle【二分图染色+思维】好题~

CodeForces - 1423B(二分答案+二分图最大匹配) 题意: 给一个二分图,以及边权,让你求在满足完美匹配的同时,使得得到完美匹配中所有边权的最大值最小。 思路: 看到最大值最小,直接想到二分,二分答案,然后跑二分图看看是否是完美匹配。 #include<bits/stdc++.h> const int maxn=2e4+5; struct edge { int u,v,w; }; vector<edge> e; vector<int> G[maxn]; int un,n,m; int mx[maxn],my[maxn]; 阅读详情

D. Vitaly and Cycle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After Vitaly was expelled from the university, he became interested in the graph theory.

Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.

Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.

Two ways to add edges to the graph are considered equal if they have the same sets of added edges.

Since Vitaly does not study at the university, he asked you to help him with this task.

Input

The first line of the input contains two integers n and m ( — the number of vertices in the graph and the number of edges in the graph.

Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.

It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.

Output

Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.

Examples
Input
4 4
1 2
1 3
4 2
4 3
Output
1 2
Input
3 3
1 2
2 3
3 1
Output
0 1
Input
3 0
Output
3 1
Note

The simple cycle is a cycle that doesn't contain any vertex twice.


题目大意:

给你一个无向图,其中有n个点,m条边,然你加最少的边使得图中存在奇数点个数的环,并且输出方案数。


思路:


1、奇数点个数的环,直接考虑二分图,如果原图是一个二分图,那么肯定要加边,如果原图不是一个二分图,那么就无需加边,因为原图中一定存在奇数点个数的环。

那么我们首先二分染色,如果可以染色,继续判断,否则输出0 1.


2、如果可以染色,我们分成几种情况讨论:

①max(degree【i】)==0.那么我们任意选三个点,加三条边,形成一个三个点的环即可。ans= 3 C(n,3)

②max(degree【i】)==1.那么我们明显需要加两条边,对应我们每一条边原图中的边,对应可以随意找除了这两个点以外的任意一个点进行环即可(依然是组成三个点的环) ans=2 m*(n-2);

③max(degree【i】)==2.那么我们明显只要加一条边就行,对应我们每一个连通块中的同颜色点之间可以相互连接,就是在同颜色边任取两个点即可。

那么过程中我们维护一个数组root【i】,表示节点i属于的连通块编号是多少。

那么对应同一颜色同一root【i】的点之间可以任意取两个点进行连接,增加的方案数就是C(这个连通块中同颜色的点的个数,2);

过程维护一下即可,对应两种颜色的点都要处理。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define ll __int64
vector<int >mp[100050];
int vis[100050];
int color[100050];
int degree[100050];
int sum[100050];
int root[100050];
int flag,rot;
void Dfs(int u)
{
    vis[u]=1;
    root[u]=rot;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            color[v]=3-color[u];
            Dfs(v);
        }
        else if(color[v]==color[u])flag=0;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        flag=1;
        memset(color,0,sizeof(color));
        memset(degree,0,sizeof(degree));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            degree[x]++,degree[y]++;
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        rot=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                rot++;color[i]=1;root[i]=rot;
                Dfs(i);
            }
        }
        if(flag==0)
        {
            printf("0 1\n");
        }
        else
        {
            int maxn=0;
            for(int i=1;i<=n;i++)
            {
                maxn=max(maxn,degree[i]);
            }
            if(maxn==0)
            {
                ll output=(ll)n*(ll)(n-1)*(ll)(n-2);
                output/=3;
                output/=2;
                printf("3 %I64d\n",output);
            }
            if(maxn==1)
            {
                ll one=0;
                ll two=0;
                for(int i=1;i<=n;i++)
                {
                    if(degree[i]==1)one++;
                    else two++;
                }
                ll output=m*(one-1+two-1);
                printf("2 %I64d\n",output);
            }
            if(maxn>=2)
            {
                ll output=0;
                memset(sum,0,sizeof(sum));
                for(int i=1;i<=n;i++)
                {
                    if(color[i]==1)sum[root[i]]++;
                }
                for(int i=1;i<=rot;i++)output+=(ll)sum[i]*(ll)(sum[i]-1)/2;
                memset(sum,0,sizeof(sum));
                for(int i=1;i<=n;i++)
                {
                    if(color[i]!=1)sum[root[i]]++;
                }
                for(int i=1;i<=rot;i++)output+=(ll)sum[i]*(ll)(sum[i]-1)/2;
                printf("1 %I64d\n",output);
            }
        }
    }
}










《玩儿起来吧》MATLAB 实时图像处理系列(一二三四) 大家学了图像处理之后,是不是就想学以致用。而要用于生活中,最好的方式就是和摄像头搭配,实现实时图像处理。今天就给大家分享MATLAB实时图像处理的基础方法,希望能帮你快速入门。 阅读详情

相关推荐

交通小白首投TRB就中Oral?我的完整时间线与避坑指南(附Editorial Manager实操截图)

本文详细解析了交通领域新手如何高效完成TRB国际会议投稿的全流程,从选题策略到投稿系统实操,再到评审过程解析和常见误区避坑指南。特别针对初次投稿者提供了实用建议,包括时间规划、选题考量、Editorial Manager系统操作技巧,以及如何应对评审意见。文章还分享了从录用到会议参与的关键步骤,帮助研究者提升投稿成功率。

weixin_27303545的博客 394

CodeForces 624C【二分图染色】

题意: 本来有一个由’a’,’b’,’c’ 组成的字符串,然后,在这个串里,相同字符 或者 相邻字符能构成边,然后就构成了一副图。 现在抹掉了这个字符串,给你这副图,问你能不能组成这个字符串。 思路: 其实重要的就是:如果存在两个点没有边,这两个点只能是 a,c; 那么问题变成,给我这副图的补图是不是能由 a, c 组成,边的相邻两点只能是 a-c; 那么就用染色可以简单地解决这个问题。

ONE MORE TRY 743

别再死记硬背了!用Amesim HCD库亲手搭建单向阀、锥阀、球阀模型(附详细参数设置)

本文详细介绍了如何使用Amesim HCD库从零构建单向阀、锥阀和球阀的仿真模型,帮助工程师摆脱死记硬背,掌握物理建模的核心技巧。通过实战案例和参数设置指南,读者将学会如何优化阀门动态响应、验证流量特性以及处理旋转运动等独特挑战,提升液压系统设计效率。

weixin_29248611的博客 520

Codeforces 557D - Vitaly and Cycle [二分图染色]【图论】

题目链接:http://codeforces.com/problemset/problem/557/D —————————————————————————————————————— D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inputstandard input o

Tabris的博客 524

嵌入式Linux Qt应用程序卡死问题分析记录

根据应用和系统层面的分析,软件功能异常和卡死现象的原因是系统控制台/dev/console阻塞导致依赖其的相关应用和命令异常。系统控制台/dev/console阻塞的原因还未确定,最近的软件版本和其他测试同事未出现该问题,所以很大可能是串口线的问题,待后续有进展了继续更新。

V10_x的博客 1655

招标文件智能解析系统开发文档

本系统是一个基于OCR与大语言模型的招标文件智能解析平台,专门用于处理和分析招标文件,自动提取关键信息并进行结构化展示。

警世龙的技术博客 1948

Codeforces 557D Vitaly and Cycle 【染色判二分图 + 组合数学】

题目链接:Codeforces 557D Vitaly and Cycle D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output After Vitaly was expelled from

世界很大 878

Android Audio最全面试题及参考答案(7万字长文)

在 Android 音频系统中,应用通过框架层的 API 发出音频请求,这些请求经过音频服务层的处理,传递到 HAL 层,再由 HAL 层调用音频驱动层与硬件进行交互,最终实现音频的播放、录制等功能。在 Android 音频系统中,应用通过框架层的 API 发出音频请求,这些请求经过音频服务层的处理,传递到 HAL 层,再由 HAL 层调用音频驱动层与硬件进行交互,最终实现音频的播放、录制等功能。音频播放完成事件:当使用 MediaPlayer 等音频播放类播放音频时,音频播放完成后会触发这个事件。

大模型大数据攻城狮的专栏 2617

E. Vladik and cards Codeforces Round #384 (Div. 2) 好题 二分+(贪心+状态压缩DP)判断

题意:给一串取值为1-8的序列,找出满足下列要求的最长子序列(不要求连续): 1.每个值出现的次数相差不超过1; 2.子序列中相同的值得出现在一起。 设满足要求的子序列数字出现次数至少为len。 性质: 1.子序列中出现次数至少为len,且有t个值出现len+1次,则长度为len*8+t; 2.若存在满足最少出现次数为len的子序列,则一定存在len-1的子序列(所有取值均丢掉一个即可

亂丟程式碼的天空 620

YOLO11涨点优化:卷积魔改 | 可变形条带卷积(DSCN),魔改轻量DCNv3二次创新

如何跟YOLO11结合:1)和C3k2创新性结合

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 996

基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示

基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示

Robot_PLC_HMI_Automation 3171

codeforces #547D Mike and Fish (二分图 欧拉回路)

Mike and Fish CodeForces - 547DAs everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish.He

w4149 612

PCIe 全解析笔记:从协议本质到工程实现

学习 PCIe,最容易陷入的误区是直接跳进协议手册(Base Spec 1300 多页),然后在 TLP 字段、LTSSM 状态、Ordering Rules 中迷失方向。正确的思维路径历史维度:从 PCI 到 PCIe,解决了什么核心矛盾?抽象维度:协议栈分层的真正意图是什么?工程维度:高速串行链路面临哪些物理挑战?协议如何应对?系统维度:作为 CPU 与外设的"动脉",PCIe 如何融入整个计算机体系?带着这四个视角去看每一个细节,知识才能内化为认知。

m0_46644103的博客 269

Codeforces Round #311 (Div. 2) C(技巧) *D(二分图染色)

C. Arthur and Table http://codeforces.com/contest/557/problem/C 题意:一张桌子有n条桌腿,每条桌腿有相应的长度和移动花费值,要使桌子平稳,最长桌腿的数量M 与桌腿的数量关系为 M*2>N,求出使桌子平稳的最小花费值。 思路:将桌腿按照长度由大到小排序,依次以不同长度的桌腿cnt 作为桌子的平稳支撑,保留另外cnt-1条花费值

Halo 810

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

CodeForces Gym 100735G 二分图匹配

就是给你一个字符串,和一些正方体,每个正方体的六个面上都标有字母或者数字,然后问你能否选一些正方体,通过某种摆放,使得他们摆成一排后组成的那个字符串 据说是裸的二分图匹配,但是我这种智障就是没看出裸来啊,果然图论题做的太少太少,然后看别人代码又短又快,总以为是暴力,然后题意也没说这些正方体可以怎么操作,迷的一笔 还是说下方法吧,字符串的每个字符为左边的点,每个 正方体为右边的 点,然后对于坐标

一蓑烟雨任平生 743

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Codeforces Round #383 (Div. 2) E(贪心,二分图染色,好题)

题目链接 E. Arpa’s overnight party and Mehrdad’s silent entering time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

fouzhe的博客 624

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Codeforces Round #311 (Div. 2) D DFS+染色

链接:戳这里 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output After Vitaly was expelled from the university, he beca

Cross 445

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图

D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output After Vitaly was expelled from the university, he became interested in

习惯与规则决定命运 1430

【CF547D】Mike and Fish(二分图染色)

传送门 题解: 标算的欧拉回路其实也挺好懂的。 这里来一个二分图染色的做法。 直接对于横坐标或纵坐标相同的点,如果在该方向上还没有连边就连起来。 然后跑二分图染色,显然如果染色成功方案一定合法,因为每行每列最多剩一个。 那么需要证明不可能连出来奇环。 考虑每个点最多连出来两条边,并且一定一条横着一条竖着。 把所有横着的边染成黑色,所有竖着的边染成白色即可证明所有环均为偶环。 代码: #incl...

zxyoi_dreamer的博客(不定期诈尸) 368

Codeforces Round #311 (Div. 2) D - Vitaly and Cycle

D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output After Vitaly was expelled from the university, he be...

weixin_30511107的博客 207

杭电OJ2063 (基础二分图匹配)(月老降临)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15700    Accepted Submission(s): 6886 Problem Description RPG girls今天和大家一起去游乐场玩

mengxiang000000 2470

hdu 3478 Catch【并查集+二分图染色】

Catch Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1607    Accepted Submission(s): 771 Problem Description A thief is running away!

mengxiang000000 1910

hdu 5874 Friends and Enemies【题意不明系列+二分图】

Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 121 Problem Description On an is

mengxiang000000 1492

hdu 5556 Land of Farms【思维+暴力枚举+最大独立集】好题!

Land of Farms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 526    Accepted Submission(s): 174 Problem Description Farmer John and hi

mengxiang000000 1260

Poj 3189 Steady Cow Assignment【二分+多重匹配】

Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6589   Accepted: 2260 Description Farmer John's N (1 FJ would lik

mengxiang000000 1179

Codeforces Round #360 (Div. 2) 前三题题解【简单模拟+思维+二分图判定二分染色】

A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Arya has n opponents in the school. Each day he

mengxiang000000 1149
上一篇: Codeforces 557C Arthur and Table【思维】
下一篇: 玲珑学院OJ 1096 萌萌哒的第一题【暴力】
mengxiang000000
博客等级 码龄11年 627粉丝 2116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值