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
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.
3 1 0 2 1 1 2 3 3 1 2 2 3 1 3Sample Output
1 1 0 1 1 1 2 2 1 2 1 3Hint
This problem is special judge.
题意:求所给给图的一个子图,使得这个子图能够构成二分图,前边大于m/2条#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
vector<int>G[105];
int col[105];
void init(){
memset(col,-1,sizeof(col));
for(int i=0;i<=100;i++)
G[i].clear();
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
int cnt0,cnt1;
for(int u=1;u<=n;u++){
cnt0=cnt1=0;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(col[v]!=0) cnt0++;
if(col[v]!=1) cnt1++;
}
if(cnt0>cnt1) col[u]=0;
else col[u]=1;
}
int a=0,b=0;
for(int i=1;i<=n;i++)
if(col[i]==0) a++;
else if(col[i]==1) b++;
printf("%d",a);
for(int i=1;i<=n;i++)
if(col[i]==0) printf(" %d",i);
puts("");
printf ("%d",b);
for(int i=1;i<=n;i++)
if(col[i]==1) printf(" %d",i);
puts("");
}
return 0;
}