POJ3155--Hard Life

Description

John is a Chief Executive Officer at a privately owned medium size company. The owner of the company has decided to make his son Scott a manager in the company. John fears that the owner will ultimately give CEO position to Scott if he does well on his new manager position, so he decided to make Scott’s life as hard as possible by carefully selecting the team he is going to manage in the company.

John knows which pairs of his people work poorly in the same team. John introduced a hardness factor of a team — it is a number of pairs of people from this team who work poorly in the same team divided by the total number of people in the team. The larger is the hardness factor, the harder is this team to manage. John wants to find a group of people in the company that are hardest to manage and make it Scott’s team. Please, help him.

In the example on the picture the hardest team consists of people 1, 2, 4, and 5. Among 4 of them 5 pairs work poorly in the same team, thus hardness factor is equal to 54. If we add person number 3 to the team then hardness factor decreases to 65.

Input

The first line of the input file contains two integer numbers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 1000). Here n is a total number of people in the company (people are numbered from 1 to n), and m is the number of pairs of people who work poorly in the same team. Next m lines describe those pairs with two integer numbers ai and bi (1 ≤ aibi ≤ nai ≠ bi) on a line. The order of people in a pair is arbitrary and no pair is listed twice.

Output

Write to the output file an integer number k (1 ≤ k ≤ n) — the number of people in the hardest team, followed by k lines listing people from this team in ascending order. If there are multiple teams with the same hardness factor then write any one.

Sample Input

sample input #1
5 6
1 5
5 4
4 2
2 5
1 2
3 1

sample input #2
4 0

Sample Output

sample output #1
4
1
2
4
5

sample output #2
1
1
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 308
#define edge 20800
#define inf 0x3f3f3f3f
#define eps 1e-5
int first[maxn],dis[maxn],num[maxn],du[maxn];
int vv[edge],nxt[edge];
double ww[edge];
bool vis[maxn];
int e,NN,n,m;
int sum = 0;
struct Edge
{
	int u,v;
	Edge(){}
	Edge(int uu,int vv)
	{
		u = uu;v = vv;
	}
}cur[1080];
void addedge(int u,int v,double w)//添加单向边
{
	vv[e] = v;	ww[e] = w;		nxt[e] = first[u];	first[u] = e++;
	vv[e] = u;	ww[e] = 0;		nxt[e] = first[v];		first[v] = e++;
}
void addEdge(int u,int v,double w)//添加双向边
{
	vv[e] = v;	ww[e] = w;		nxt[e] = first[u];	first[u] = e++;
	vv[e] = u;	ww[e] = w;		nxt[e] = first[v];		first[v] = e++;
}
inline double min(double a,double b)
{
	return a>b?b:a;
}
double dfs(int u,int s,int d,double cost)
{
	if(u == d)	return cost;
	double ans = 0;
	int _min = NN;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(ww[i] > eps)
		{
			if(dis[v] + 1 == dis[u])
			{
				double t = dfs(v,s,d,min(ww[i],cost));
				ww[i] -= t;
				ww[i^1] += t;
				ans += t;
				cost -= t;
				if(dis[s] == NN)		return ans;
				if(cost <= eps)		break;
			}
			if(_min > dis[v])		_min = dis[v];
		}
	}
	if(ans <= eps)
	{
		if(--num[dis[u]] == 0)		dis[s] = NN;
		dis[u] = _min + 1;
		++num[dis[u]];
	}
	return ans;
}
double isap(int s,int d)
{
	memset(dis,0,sizeof(dis));                                                                                                                                                                                                                                                                                                                                                                                                                                                         
	memset(num,0,sizeof(num));
	num[0] = NN;
	double ans = 0;
	while(dis[s] < NN)	ans += dfs(s,s,d,inf);
	return ans;
}
void build(double g)
{
	e = 0;NN = n + 2;
	memset(first,-1,sizeof(first));
	for(int i = 1;i <= n;i++)
	{
		addedge(0,i,m);
	}
	for(int i = 1;i <= m;i++)
	{
		addEdge(cur[i].u,cur[i].v,1);
	}
	for(int i = 1;i <= n;i++)
	{
		addedge(i,n+1,m+2*g-du[i]);
	}
}
void dfs(int u)
{
	vis[u] = 1;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(ww[i] > 0 && !vis[v])
		{
			vis[v] = 1;
			sum++;
			dfs(v);
		}
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d%d",&n,&m)==2)
	{
		if(m == 0)
		{
			printf("%d\n%d\n",1,1);
			continue;
		}
		memset(du,0,sizeof(du));
		for(int i = 1;i <= m;i++)
		{
			scanf("%d%d",&cur[i].u,&cur[i].v);
			du[cur[i].u]++;du[cur[i].v]++;
		}
		double l = 1./n,r = m;
		while(r - l >= 1./n/n)
		{
			double mid = (l + r)/2;
			build(mid);
			if((m*n - isap(0,n+1))/2 > eps)
			{
				l = mid;
			}
			else r = mid;
		}
		build(l);
		memset(vis,0,sizeof(vis));
		isap(0,n+1);
		sum = 0;
		dfs(0);
		int ok = 0;
		printf("%d\n",sum);
		for(int i = 1;i <= n;i++)
		{
			if(vis[i])	
			{
				printf("%d\n",i);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值