您的位置:首页 > Web前端

poj 3155 Hard Life (最大密度子图)

2017-01-16 15:07 615 查看
Hard Life

Time Limit: 8000MSMemory Limit: 65536K
Total Submissions: 8595Accepted: 2497
Case Time Limit: 2000MSSpecial Judge
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 5⁄4. If we add person number 3 to the team then hardness factor
decreases to 6⁄5.

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 ≤ ai, bi ≤ n, ai ≠ 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

Hint

Note, that in the last example any team has hardness factor of zero, and any non-empty list of people is a valid answer.

Source

Northeastern Europe 2006
[Submit] [Go Back] [Status]
[Discuss]

题解:最大密度子图

需要用到01分数规划+最小割

胡伯涛的论文中有详细的讲解和证明







#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 30003
#define eps 1e-6
#define inf 1e9
using namespace std;
int point
,v
,next
,x
,y
,mark
,tot;
int last
,num
,deep
,cur
,n,m;
double remain
;
void add(int x,int y,double z)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
double addflow(int s,int t)
{
int now=t; double ans=1e10;
while (now!=s) {
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s) {
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=1;i<=t;i++) deep[i]=t;
queue<int> p; p.push(t); deep[t]=0;
while (!p.empty()){
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=next[i])
if (deep[v[i]]==t&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
double isap(int s,int t)
{
int now=s; double ans=0;
bfs(s,t);
for (int i=1;i<=t;i++) num[deep[i]]++;
for (int i=1;i<=t;i++) cur[i]=point[i];
while (deep[s]<t) {
if (now==t) {
ans+=addflow(s,t);
now=s;
}
bool pd=false;
for (int i=cur[now];i!=-1;i=next[i])
if(deep[now]==deep[v[i]]+1&&remain[i]>eps){
pd=true;
cur[now]=i;
last[v[i]]=i;
now=v[i];
break;
}
if (!pd) {
int minn=t;
for (int i=point[now];i!=-1;i=next[i])
if (remain[i]>eps) minn=min(minn,deep[v[i]]);
if (!--num[deep[now]]) break;
num[deep[now]=minn+1]++;
cur[now]=point[now];
if (now!=s) now=v[last[now]^1];
}
}
return ans;
}
bool check(double g)
{
tot=-1;
memset(point,-1,sizeof(point));
memset(num,0,sizeof(num));
int s=1; int t=m+n+2;
for (int i=1;i<=n;i++) add(i+1,t,g);
for (int i=1;i<=m;i++) {
add(s,i+n+1,1);
add(i+n+1,x[i]+1,inf);
add(i+n+1,y[i]+1,inf);
}
return m-isap(s,t)>=eps;
}
void dfs(int x)
{
mark[x]=1;
for (int i=point[x];i!=-1;i=next[i])
if (remain[i]&&!mark[v[i]]) dfs(v[i]);
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d",&n,&m);
if (!m) {
printf("1\n1\n");
return 0;
}
for (int i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]);
double l=1.0/n; double r=m; double ans=0;
while (r-l>=1.0/n/n) {
double mid=(l+r)/2;
if (check(mid)) ans=max(ans,mid),l=mid+eps;
else r=mid-eps;
}
check(ans);
dfs(1);
int size=0;
for (int i=1;i<=n;i++)
if (mark[i+1]) size++;
printf("%d\n",size);
for (int i=1;i<=n;i++)
if (mark[i+1]) printf("%d\n",i);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: