我是靠谱客的博主 畅快小馒头,最近开发中收集的这篇文章主要介绍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 <iostream>
#include <cstdio>
#include <algorithm>
#include<queue>
#include <cstring>
using namespace std;
char mapp[105][105];
int n,m;
vector<int> V;
vector<int> U;
struct node
{
int deg;
int v;
}p[105];
bool cmp(node x1,node x2)
{
return x1.deg>x2.deg;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
int x,y;
memset(mapp,0,sizeof(mapp));
memset(p,0,sizeof(p));
U.clear();
V.clear();
for(int i=1;i<=n;i++)
p[i].v=i;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
mapp[x][y]=mapp[y][x]=1;
p[x].deg++;
p[y].deg++;
}
if(m==0)
{
printf("1 1n0n");
continue;
}
sort(p+1,p+1+n,cmp);
V.push_back(p[1].v);
int i=2;
while(i<=n)
{
int now=p[i].v;
int countt1=0;
int countt2=0;
for(int i=0;i<V.size();i++)
if(mapp[now][V[i]])
countt1++;
for(int i=0;i<U.size();i++)
if(mapp[now][U[i]])
countt2++;
if(countt1>countt2)
U.push_back(now);
else
V.push_back(now);
i++;
}
cout<<V.size();
for(int i=0;i<V.size();i++)
cout<<" "<<V[i];
cout<<endl;
cout<<U.size();
for(int i=0;i<U.size();i++)
cout<<" "<<U[i];
cout<<endl;
}
}

最后

以上就是畅快小馒头为你收集整理的Sub-Bipartite Graph FZU - 2141 (贪心)的全部内容,希望文章能够帮你解决Sub-Bipartite Graph FZU - 2141 (贪心)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部