FZU2141 Sub-Bipartite Graph 贪心

问题描述

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


输入

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.


样例输入

31 02 11 23 31 22 31 3

样例输出

1 101 11 22 1 21 3

提示

This problem is special judge.

 题目大意:把图的一些边去掉变成二分图。剩下的边要大于等于原来的一半数量。
思路:贪心。把点染色,一些点染成1一些染成2,这样分成2部分。每次染一个点的时候判断他之前的点染1的多还是染2的多,染什么多就染什么颜色,这样可以减少删除的边的个数。
 AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=110;
int g[maxn][maxn],colo[maxn],A,B;

void f(int p)
{
    int a=0,b=0;
    colo[p]=1;
    for(int i=1;i<p;i++)
        if(g[i][p]&&colo[i]!=colo[p]) a++;
    colo[p]=2;
    for(int i=1;i<p;i++)
        if(g[i][p]&&colo[i]!=colo[p]) b++;
    if(a>b) colo[p]=1,A++;
    else colo[p]=2,B++;
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(colo,0,sizeof(colo));
        memset(g,0,sizeof(g));
        int u,v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&v,&u);
            g[u][v]=g[v][u]=1;
        }
        B=A=0;
        for(int i=1;i<=n;i++)
            f(i);
        printf("%d",A);
        for(int i=1;i<=n;i++)
        {
             if(colo[i]==1) printf(" %d",i);
        }

        printf("\n");
        printf("%d",B);
        for(int i=1;i<=n;i++)
        {
            if(colo[i]==2) printf(" %d",i);
        }

        printf("\n");

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值