FZU 2141 Sub-Bipartite Graph【贪心】
来源:互联网 发布:社交软件 编辑:程序博客网 时间:2024/06/06 20:17
Description
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
Sample Output
Hint
This problem is special judge.
第一场组队赛啊,妹的QAQ
题意:给定n个点,m个边,要求删去最多一半的边使得原有点集形成二分图
做法:贪心的思路,既然我们想要保留尽量多的边,那么每次有新点选择加入的就是跟自己连边少的集合……没了T^T
#include<cstdio>#include<cstring>using namespace std;int n,m,t;int num1[100],num2[100];bool fl[120][120];int main(){ // freopen("cin.txt","r",stdin); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(fl,0,sizeof(fl)); while(m--) { int a,b; scanf("%d%d",&a,&b); fl[a][b]=fl[b][a]=1; } int pos1=0,pos2=0; for(int i=1;i<=n;i++) { int tmp1=0,tmp2=0; for(int j=1;j<=pos1;j++) { if(fl[i][num1[j]])tmp1++; } for(int j=1;j<=pos2;j++) { if(fl[i][num2[j]])tmp2++; } if(tmp1<tmp2)num1[++pos1]=i; else num2[++pos2]=i; } printf("%d",pos1); for(int i=1;i<=pos1;i++)printf(" %d",num1[i]); puts(""); printf("%d",pos2); for(int i=1;i<=pos2;i++)printf(" %d",num2[i]); puts(""); } return 0;}
- fzu 2141 Sub-Bipartite Graph (贪心)
- FZU 2141 Sub-Bipartite Graph【贪心】
- FZU 2141 Sub-Bipartite Graph 贪心
- FZU 2141Sub-Bipartite Graph
- FZU 2141 Sub-Bipartite Graph
- fzu 2141 Sub-Bipartite Graph 贪心 二分图构建
- 【随机】 FZU 2141 Sub-Bipartite Graph
- 【贪心】 FOJ 2141 Sub-Bipartite Graph
- FZU2141 Sub-Bipartite Graph 贪心
- hdu 5313 Bipartite Graph 贪心+bfs染色
- Bipartite Graph
- Bipartite graph/network学习
- HDU5313.Bipartite Graph
- hdu5313--Bipartite Graph
- hdu 5313 Bipartite Graph
- hdu-5313 Bipartite Graph
- hdu 5313 Bipartite Graph
- hdu5354 Bipartite Graph
- 代理模式
- ATOM基础教程一安装ATOM(2)
- BRP 模型
- Struts 2学习(一)Struts 2环境搭建及示例程序编写
- 基于.NET考勤系统(附源码)
- FZU 2141 Sub-Bipartite Graph【贪心】
- Java类型信息与用反射机制编写通用的Excel导入导出
- 【三星官方教程】如何为Gear VR 开发应用(一):开发环境搭建(转)
- 《VR入门系列教程》之21---使用Unity开发GearVR应用(转)
- MyBatis的ResultMaps之一对一关系
- Service的生命周期
- Spinner 之 在strings.xml中提供数据源
- MongoDB c++ driver(六)
- AbstractRoutingDataSource实现多数据源