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

别再浪费NFC了!手把手教你用Android手机模拟门禁卡(Mifare Classic破解教程)

本文深入探讨Android NFC技术的核心原理与创新应用,教你如何利用手机模拟门禁卡(Mifare Classic破解教程),解锁智能生活的无限可能。涵盖NFC标签类型解析、HCE开发实战及安全实践,让你的Android设备成为数字生活的万能钥匙。

weixin_29239661的博客 230

你的Wi-Fi总断网?可能是DHCP租约到期惹的祸!手把手教你用Windows/路由器设置永久IP或延长租期

本文深入解析家庭Wi-Fi频繁断网的常见原因——DHCP租约到期问题,并提供Windows和路由器设置永久IP或延长租期的详细教程。通过对比静态IP配置与延长租期两种方案,帮助用户有效解决网络不稳定问题,特别适合游戏玩家和远程工作者优化家庭网络环境。

weixin_29305313的博客 1409

1.8学习总结

1.8学习总结

shu_yamini的博客 299

基于Landsat-8数据的山西省吕梁市地表温度反演研究过程(大气校正法)

1.数据下载:本实验采用的数据是 ①从地理空间数据云下载的来自landsat8 卫星,云量<=1%,时间为2017年5月12日的山西省吕梁市影像数据。为全面覆盖吕梁市,选用了两片影像。 ②吕梁市边界矢量图 2.数据预处理 ...

weixin_45747376的博客 6600

补题补题2.13

补题

weixin_74789176的博客 389

Coze工作流实战:Coze一键生成沉浸式历史故事视频

【coze工作流实战】一键生成火爆的沉浸式历史故事视频!短短3分钟,顶替你3天的工作量!

bickys的博客 1004

「深度学习之优化算法」笔记(二)优化算法的分类

但是由于生物的局限性,人们所创造出的算法也会有局限性:我们所熟知的生物都生存在三维空间,在这些环境中,影响生物生存的条件比较有限,反应到算法中就是这些算法在解决较低维度的问题时效果很好,当遇到超高维(维度>500)问题时,结果可能不容乐观,没做过实验,我也不敢乱说。当然,他们的目的不是在最佳狩猎位置集合,他们的目的是在过去的途中看是否能发现更加好的狩猎位置,去往已经到过的狩猎地点再次狩猎是没有意义的,因为每个位置获取食物的难易程度是固定的。如果他们的策略是“不跟随最优解”,那么他们的策略是什么呢?

quanzhan_King的博客 1371

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

飞牛NAS1.1.19更新后SSH连不上?两步快速恢复连接

飞牛NAS1.1.19版本更新后SSH无法连接😟,实则是官方安全策略升级默认关闭了该服务,本文分享两步实操配置恢复SSH连接💻,还同步了官方的设备安全配置建议,帮助用户兼顾使用与设备防护🔒。

XiaoqiangClub的博客 2092

FZU 2141 Sub-Bipartite Graph 贪心

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

我们做的队 464

【数据集】1980-2020年(5年)土地利用分类数据-中国科学院

简介1980-2020年每五年土地利用数据-中国科学院

WW、forever的博客 3931

《点云进阶》专栏文章目录

点云进阶》专栏文章目录

点云侠的博客 4903

fzu 2141 Sub-Bipartite Graph (贪心)

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

绝望的乐园 1535

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

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

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

AtCoder Beginner Contest 284 (E - Count Simple Paths)

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

Edward Shi的博客 931

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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、付费专栏及课程。

余额充值