POJ3155--Hard Life

来源:互联网 发布:mac pdg转pdf 编辑:程序博客网 时间:2024/06/03 18:38

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 #15 61 55 44 22 51 23 1sample input #24 0

Sample Output

sample output #141245sample output #211
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 308#define edge 20800#define inf 0x3f3f3f3f#define eps 1e-5int 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;}