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 (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.


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector> 
using namespace std;
typedef long long ll;

const int maxn=110;
vector<int> vec[maxn];
int color[maxn];

void init(int n){
	memset(color,0,sizeof(color));
	for(int i=0;i<n;i++){
		vec[i].clear();
	}
}
/*
这道题感觉还是蛮难的
要求边的数量大于m/2,这就很难受
他看的是哪边的边多,就放到对面,这样每个点
都会留下较多的边 ,所以就会大于m/2
也没说单独的点不可以 

bipartite graph
指顶点可以分成两个不相交的集使得在同一个集内的顶点不相邻(没有共同边)的图。
*/
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d %d",&n,&m);
		init(n);
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d %d",&u,&v);
			if(v>u) swap(u,v);
			vec[u].push_back(v); 
		}
		for(int i=1;i<=n;i++){
			int sz=vec[i].size();
			int cnt0=0,cnt1=0;
			for(int j=0;j<sz;j++){
				int v=vec[i][j];
				if(color[v]==0) cnt1++;
				if(color[v]==1) cnt0++;
			}
			color[i]=0;
			if(cnt1>cnt0)
				color[i]=1;
		}
		int cnt0=0,cnt1=0;
		for(int i=1;i<=n;i++){
			if(color[i]==0) cnt0++;
			else  cnt1++;
		} 
		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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值