fzu2141(二分图着色)
来源:互联网 发布:网络广告代理公司多吗 编辑:程序博客网 时间:2024/05/29 17:39
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
1 0
2 1
1 2
3 3
1 2
2 3
1 3
Sample Output
0
1 1
1 2
2 1 2
1 3
Hint
This problem is special judge.
题意:给你一个图,现在让你找出一个边数不小于m/2的二分图,然后分别输出二分图的两部分。
思路:很明显的二分图着色,只不过要有个优先着色的策略,尽量让二分图的点最多。
#include<stdio.h>#include<algorithm>#include<queue>#include<vector>#include<iostream>#include<string.h>using namespace std;int tu[110][110],color[110];void judge(int x,int n){ for(int i=1; i<=n; i++) if(tu[x][i]&&color[i]==-1) { int ling=0,yi=0; for(int j=1; j<=n; j++) if(tu[i][j]) { if(color[j]==0)ling++; if(color[j]==1)yi++; } if(ling>=yi) color[i]=1; else color[i]=0; judge(i,n); }}int main(){ int t; cin>>t; while(t--) { vector<int>a1,a2; memset(color,-1,sizeof(color)); memset(tu,0,sizeof(tu)); int n,m; scanf("%d%d",&n,&m); for(int i=0; i<m; i++) { int a,b; scanf("%d%d",&a,&b); if(!tu[a][b]) tu[a][b]=tu[b][a]=1; } for(int i=1; i<=n; i++) if(color[i]==-1) { color[i]=0; judge(i,n); } for(int i=1; i<=n; i++) { if(color[i]==0) a1.push_back(i); else if(color[i]==1) a2.push_back(i); } int l1=a1.size(),l2=a2.size(); printf("%d",l1); for(int i=0; i<l1; i++) printf(" %d",a1[i]); puts(""); printf("%d",l2); for(int i=0; i<l2; i++) printf(" %d",a2[i]); puts(""); } return 0;}
- fzu2141(二分图着色)
- 二分图的判定 -- 着色
- (intermediate) 二分图(边着色) UVA 10615 Rooks
- 二分图判定 (黑白二着色法)
- HDU 5971 Wrestling Match(二分图着色)
- 点双联通和二分图着色(二分图判定)模板
- 图着色问题 (转)
- 图着色
- 图着色
- 图着色
- 图着色
- FZU2141 Sub-Bipartite Graph 贪心
- UVA - 1364(无向图的bcc以及对bcc二分着色)
- 图的m着色(回溯法)
- poj 1129(图的着色问题)
- m-图着色问题(贪心解法)
- 图的点着色、区间着色问题及其应用(基于贪心思想的DFS回溯法求点着色问题和区间着色算法求解任务调度问题)
- 二分图趣写(二分图)
- redis列表操作
- ZXing的使用
- 无责任共享 Coursera、Udacity 等课程视频【百度云】
- Windows Server使用python实现自动化运维——开启系统更新服务
- 解决ubuntu系统中文拼音输入法输入错误的问题
- fzu2141(二分图着色)
- 快速排序
- C++封装向量-线性表
- HTML5+规范:Geolocation(管理设备位置信息)
- 安卓中测试用例
- 浅谈Android多屏幕的事
- CODE[VS] 1670 无穷的序列 二分答案水法
- CrackingtheCodeInterview之树与图
- Jsp ssh