Sub-Bipartite Graph FZU - 2141 (贪心+染色)

AtCoder Beginner Contest 284解题报告(A-D) AtCoder Beginner Contest 284解题报告 阅读详情

Sub-Bipartite Graph

FZU - 2141

Given a simple undirected graph G with n vertices and m edges, your task is to select a sub-bipartite graph of G with at least m/2 edges.

In the mathematical field of graph theory, a bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint sets U and V such that every edge connects a vertex in U to one in V; that is, U and V are each independent sets. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

In the mathematical field of graph theory, a subgraph is a graph G whose graph vertices and graph edges form subsets of the graph vertices and graph edges of a given graph G..

In graph theory, a simple graph is a graph containing no self-loops or multiple edges.

from wikipedia


Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case starts of two numbers N and M, representing the number of vertices and the number of edges, then M lines follow. Each line contains two integers x and y, means that there is an edge connected x and y. The number of nodes is from 1 to N.

1 <= T <= 100, 1 <= N <= 100, 0 <= M <= 10086

Output

For each case, you should output two lines to describe your sub-graph, the first line is the set of U and the second line is the set of V.

Each line should output an integer F first, which is the total number of the vertices in this set, then F integers follow which are the number of each vertex of this part, see sample input and sample output for more details.

You can assume that the answer is always existed.

Sample Input
3
1 0
2 1
1 2
3 3
1 2
2 3
1 3
Sample Output
1 1
0
1 1
1 2
2 1 2
1 3
Hint

This problem is special judge.

题意:给定一个图,要求我们在这个图中构造一个二分图,且边数至少m/2

思路:贪心,用染色的方法,二分图嘛,就染成两种颜色,我们假设i个点都已经染好,并且是最优解那么第i+1个染色时我们遍历和i+1号点相连的所有点,让这个点染成和相连点不同的颜色看染哪种颜色的时候可以加的边最多,然后确定下这个点的颜色

最终遍历一边输出。

code:


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 111;
int n,m,cnt;
int head[maxn],color[maxn];
struct node{
    int to;
    int nex;
}edge[222*maxn];
void init(){
    cnt = 0;
    memset(head,-1,sizeof(head));
    memset(color,-1,sizeof(color));
}
void add(int u,int v){
    edge[cnt].to = v;
    edge[cnt].nex = head[u];
    head[u] = cnt++;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        for(int i = 1; i <= m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        for(int i = 1; i <= n; i++){
            int cnt0 = 0,cnt1 = 0;//cnt0表示i点染成0新加的边数,cnt1同理
            for(int j = head[i]; j != -1; j = edge[j].nex){//遍历i点连接的点
                int v = edge[j].to;
                if(color[v] != 0) cnt0++;//如果与i连接的点不是0色,说明i点可以染成0色,所以边数加一
                if(color[v] != 1) cnt1++;//同理
            }
            if(cnt0 > cnt1) color[i] = 0;//如果把i染成0色增加的边多,就染成0
            else color[i] = 1;//反之
        }
        int cnt0 = 0,cnt1 = 0;
        for(int i = 1; i <= n; i++){
            if(color[i] == 1) cnt1++;
            else cnt0++;
        }
        printf("%d",cnt0);
        for(int i = 1; i <= n; i++){
            if(color[i] == 0) printf(" %d",i);
        }
        printf("\n");
        printf("%d",cnt1);
        for(int i = 1; i <= n; i++){
            if(color[i] == 1) printf(" %d",i);
        }
        printf("\n");
    }
    return 0;
}

fzu 2150 Fire Game 【技巧BFS】 题目: fzu 2141 Sub-Bipartite Graph (贪心) 阅读详情

相关推荐

题解 | Graph Games-2019牛客暑期多校训练营第三场A题

题目来源于牛客竞赛:https://ac.nowcoder.com/acm/contest/discuss 题目描述: You are given an undirected graph with N vertices and M edges. The edges are numbered from 1 to M. Denote the set S(x) as: All the vertice...

lastbye的博客 619

Heterogeneous Flow Table Distribution in Software-Defined Networks笔记

一、几个概念 homogeneous flow table:同构流表(策略) 一个入口交换机应该执行相同的策略(ACL)——>由于TCAM容量限制,入口交换机的策略可以分配到由此导出的每条路径上——>为确保每条路径上的流都能匹配到规则,应该在每条路径上存贮相同的规则集(palette),这些被分割的相同的规则集就是同构流表。 总结:流的需求一样——>所有流经过的路径规则集都一样 homogene

Laity000的博客 873

贪心法求解图的着色问题

贪心法求解图的着色问题C++源代码,可直接编译运行。 greedy.

1.10总结

不是因为数字太大没有用对应的转换规范,就是由于没有及时break,还有就是忘记初始化s,还有就是开数的时候对应的类型开小了TAT!嗯这个题刚开始想的就是偶数可以奇数不可以,但是后来一直出不来答案,从学长那里才了解到其实就是三不行,然后可以找规律,你会发现,我甘愿做残冬的槁木,只要曾经是早春如诗如画的花火,我立誓要在成土成烟之余都要洒然一笑,因为活过了就是一场胜利,就有资格欢呼;,n个数,再m行表示关系,这个边其实就是用来计数的表示关系,没什么用。这题真的写了我好久QAQ!写这个题犯的错误真的多到天际,

weixin_74789176的博客 308

【仓颉 + 鸿蒙 + AI Agent】CangjieMagic框架(3):Agent请求和响应

CangjieMagic框架:使用华为仓颉编程语言编写,专门用于开发AI Agent,支持鸿蒙、Windows、macOS、Linux等系统。这篇文章剖析一下 CangjieMagic 框架中的 AgentRequest 和 AgentResponse。

九丘教育官方博客 1475

制作Ubuntu22.04启动盘

现在越来越多程序代码都需要运行在linux环境下,运行这些代码的前提是安装一个稳定的linux操作系统。本文选择Ubuntu 22.04去制作系统启动盘。本文主要对ubuntu22.04的启动盘制作过程进行了详细的说明,希望能帮助到有需要的朋友。

lyx_ok的博客 1万+

1.8学习总结

1.8学习总结

shu_yamini的博客 299

深入浅出:.sh文件在Linux系统中的实战应用与技巧

本文深入浅出地介绍了.sh文件在Linux系统中的实战应用与技巧。从基础概念入手,详细讲解了Bash脚本的运行方法、权限管理,并通过编写自动化备份脚本等实例,展示了如何利用Shell脚本提升工作效率。文章还涵盖了脚本调试、错误处理以及函数、参数解析等进阶技巧,帮助读者从入门到精通,掌握这一强大的自动化工具。

weixin_29243279的博客 346

补题补题2.13

补题

weixin_74789176的博客 389

SAP开发实战:用FI_DOCUMENT_CHANGE BAPI批量修改FB03凭证抬头和行项目文本(附完整ABAP代码)

本文详细介绍了如何使用SAP的FI_DOCUMENT_CHANGE BAPI批量修改FB03凭证抬头和行项目文本,提供完整的ABAP代码实现方案。通过该BAPI,开发者可以高效完成财务凭证的批量修改,确保数据一致性和审计合规性,显著提升SAP财务模块的运维效率。

weixin_29224589的博客 380

SWMM建模与案例应用

4、排水系统控制规则通过控制功能,可以在模拟进行过程中,通过条件判断,对闸门、水泵、管道、孔口、堰进行控制,实现排水系统实时控制。5、SWMM与其他模型对比分析分析:STORM、MOUSE、Info Works、SUSTAIN、SWC。1、模型初始设置主要参数:雨量计、汇水区、节点、出水口、管渠、分流器、蓄水池等参数前缀的设置。2、气象数据设置温度、蒸发、风速、融雪、面积亏损、气候调整、雨量计。1、多软件数据交互:CAD、Arcgis、SWMM。(1)节点、管段、水泵、孔口、堰、雨水口和街道。

m0_74575633的博客 3417

FZU2141 Sub-Bipartite Graph 贪心

问题描述 Given a simple undirected graph G with n vertices and m edges, your task is to select a sub-bipartite graph of G with at least m/2 edges. In the mathematical field of graph theory, a bipart

sinat_21243261的专栏 531

让 SAP HANA 查询飞起来:彻底避开隐式类型转换的性能暗礁

SAP HANA性能优化:警惕隐式类型转换陷阱 隐式类型转换是SAP HANA项目中常见的性能隐患。当SQL表达式两侧数据类型不匹配时,系统会自动进行类型转换,这种看似便利的操作可能导致: 过滤条件无法下推,扫描范围扩大 列存引擎优势丧失,触发中间结果物化 执行计划走向低效路径 治理方案包括: 从源头统一数据类型,确保常用比较字段类型一致 将显式转换应用于常量而非整列 使用生成列预先转换存储,避免运行时计算 排查方法: 通过SQL Plan Cache定位高频查询 分析执行计划中的转换迹象 重点关注VARC

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 11万+

FZU 2141 Sub-Bipartite Graph 贪心

,,,这题组队训练的时候,没做出来,,一部分原因是题目没看懂,,,当时把注意力都集中再维基百科的那段话上,,,结果整个描述就第一句有用,,而我们都没怎么看第一句话。。。  这题的策略的是贪心,,,假设已经放好了前i个点的二分图,,把第i+1个点,如何染色,才能是最终边的收益最大那?,,这时就将第i+1个点,染成2种颜色都试一下,看看哪种收益大,,具体看代码。。。 #include #inclu

我们做的队 464

超详细!Linux 服务器 6 大核心性能测试(附自动脚本 + 结果分析秘籍)---Linux 服务器性能测试文档

本文档旨在提供 Linux 服务器的 CPU、内存和磁盘性能测试方案,包括详细的测试大纲、操作步骤以及一键化测试脚本,帮助管理员快速评估服务器性能状况。通过以上测试方案,可以全面评估 Linux 服务器的 CPU、内存和磁盘性能,为系统优化、容量规划和问题排查提供重要参考依据。使用 sysbench 进行 CPU 基准测试。使用 sysbench 测试内存读写性能。使用 fio 工具进行更全面的磁盘测试。使用 dd 命令测试磁盘顺序读写性能。使用 dd 命令测试内存读写速度。记录磁盘 IO 性能。

jundao1997的专栏 380

【FDTD】仿真目录(共计 16 篇)

对于完美吸收型边界条件,有时候我们需要设置将仿真结构穿过边界条件区域,具体原因可以阅读下文。在进行 S 参数扫描时,当考虑到材料的色散特性时,我们可以如下文一样选择计算群延迟。仿真,我们需要构建仿真结构与仿真区域,对于它们的尺寸,应该遵循下文进行设定。中存在多种不同的网格类型,具体可以参考下文,这部分内容仅作了解即可。尺寸的设置问题,对于 mesh 的最小尺寸,可以参考下文。的边界条件设置也十分重要,下文中我们对边界条件进行总结。仿真中我们可以设定参与仿真的模式数量,可以参考下文。尺寸的实例,详见下文。

u011699626的博客 502

fzu 2141 Sub-Bipartite Graph (贪心)

题意:给出N个点M条边的图,现在要

绝望的乐园 1535

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

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

FZU 2141 Sub-Bipartite Graph

Sub-Bipartite Graph Time Limit:1000MSMemory Limit:32768KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeFZU 2141 Description Given a simple undirected graph G with n ...

weixin_33897722的博客 224

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

AtCoder Beginner Contest 284 (E - Count Simple Paths)

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!Hello, 大家好哇!本今天来讲一下AtCoder Beginner Contest 284的E题——Count Simple Paths!

Edward Shi的博客 931

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Sub-Bipartite Graph FZU - 2141 (贪心)

Given a simple undirected graph G with n vertices and m edges, your task is to select a sub-bipartite graph of G with at least m/2 edges. In the mathematical field of graph theory, a bipartite graph ...

Coldfresh的博客 353

数字染色(贪心)

给出一个长度为n的序列a1,a2,a3,……,an,要求你使用最少的颜色对每个数染色。对于任何颜色都需要满足:染成该颜色的数都能被染成该颜色的最小数整除。每种颜色可以使用一次或多次。染成同一种颜色的所有元素不需要是连续的。请求出最少需要的颜色数量。比如[40,60,10]可以被染成同一种颜色,因为它们都可以被10整除。第二行n个正整数,表示序列的每个元素。第一行一个正整数n,表示序列长度。一个整数,表示至少需要的颜色数量。......

luoyixin2009的博客 1173

B - Sub-Bipartite Graph FZU - 2141

Given a simple undirected graph G with n vertices and m edges, your task is to select a sub-bipartite graph of G with at least m/2 edges. In the mathematical field of graph theory, a bipartite graph...

海绵的博客 311

upc-2021个人训练赛第27场 D: Values(思维+并查集)

问题 D: Values 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Given is a simple undirected graph with N vertices and M edges. The i-th edge connects Vertex ci and Vertex di. Initially, Vertex i has the value ai written on it. You want to change the values on Vertex 1, …, V

我们都有光明的未来 472

#2020.02.05训练题解#最小生成树入门(F题)

题源CF-1108 CF-1108-MST Unification Description You are given an undirected weighted connected graph with n vertices and m edges without loops and multiple edges. The i-th edge is ei=(ui,vi,wi); the di...

薄荷糖·琳的博客 1788

AtCoder:Hamiltonish Path(思维 & dfs)

B - Hamiltonish Path Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement You are given a connected undirected simple graph, which has N vertices and M edges

junior19的博客 759

AtcoderABC262 E Red and Blue Graph

### 题目描述 You are given a simple undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1,2,\dots,N$, and the $i$-th $(1\leq i\leq M)$ edge connects Vertices $U_i$ and $V_i$. There are $2^N$ ways to paint each vertex red or blue. Fin

tanjunming2020的博客 1244
上一篇: Forever 0.5 FZU - 2140 (思维+计算几何+构造)
下一篇: A-B Game FZU - 2147
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值