FZU 2141 Sub-Bipartite Graph

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

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-1个点,那么现在有左边和右边两个点列,对于第i个点,

因为第i个点只于之前的点有连边,那么只要把第i个点放在连的边较少的点列里,那么较多的边就会出现在二分图里。

对于每个点都这样操作,就可以保证最后在二分图中的边会大于一半了。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 1e2 + 10;
int n, m, T, x, y;
vector<int> L, R;
bitset<101> g[maxn], l, r;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		l = r = 0;	L.clear();	R.clear();
		for (int i = 0; i < n; i++) g[i] = 0;
		while (m--)
		{
			scanf("%d%d", &x, &y);
			if (x < y) swap(x, y);
			g[x - 1][y - 1] = 1;
		}
		for (int i = 0; i < n; i++)
		{
			if ((g[i] & l).count() > (g[i] & r).count())
			{
				R.push_back(i + 1);	r[i] = 1;
			}
			else
			{
				L.push_back(i + 1);	l[i] = 1;
			}
		}
		printf("%d", L.size());
		for (int i = 0; i < L.size(); i++) printf(" %d", L[i]);
		printf("\n%d", R.size());
		for (int i = 0; i < R.size(); i++) printf(" %d", R[i]);
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(FZU)