FZU 2141 Sub-Bipartite Graph

本文探讨了一种从给定简单无向图中选择至少一半边长的子双部分图的方法,详细介绍了算法实现及实例解析。
Sub-Bipartite Graph
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  FZU 2141

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.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int g[110][110],color[110],B,W;
 7 
 8 int cal(int p)
 9 {
10     int b=0,w=0;
11     int i;
12     color[p]=1;
13     for(i=1;i<p;++i)
14         if(g[i][p] && color[i]!=color[p]) b++;
15     color[p]=2;
16     for(i=1;i<p;++i)
17         if(g[i][p] && color[i]!=color[p]) w++;
18     if(b>w) color[p]=1,B++;
19     else color[p]=2,W++;
20 }
21 
22 int main()
23 {
24     int T,n,m;
25     scanf("%d",&T);
26     while(T--)
27     {
28         int i,j;
29         scanf("%d%d",&n,&m);
30         memset(color,0,sizeof(color));
31         memset(g,0,sizeof(g));
32         int u,v;
33         for(i=1;i<=m;++i)
34         {
35             scanf("%d%d",&u,&v);
36             g[u][v]=1;
37             g[v][u]=1;
38         }
39         
40         B=W=0;
41         for(i=1;i<=n;++i)
42             cal(i);
43         printf("%d",B);
44         for(i=1;i<=n;++i)
45             if(color[i]==1) printf(" %d",i);
46         printf("\n");
47         printf("%d",W);
48         for(i=1;i<=n;++i)
49             if(color[i]==2) printf(" %d",i);
50         printf("\n");
51     }
52     return 0;
53 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4771425.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值