poj3155(最小密度子图)

这个题思路是一样的。。直接分数规划跑最大权封闭子图,然后麻烦的是输出方案。。

当求出答案之后我们是不能在原图上跑bfs求方案的,因为原图的最小割跑出来就是0,也就是说我们在这个图上的最大权封闭子图可以是空集。然后就会出现一些时候方案是空的情况。。

这个时候把得到的最优分数减去一个eps,那么封闭子图的最大权就大于0了,排除掉空集的情况跑bfs就是最优方案了。。

好坑。。。

 

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2) 
#define NM 2005
#define nm 10005
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const int inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
  


struct edge{int t;double v;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM],*tmp[NM];
void _add(int x,int y,double v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
void add(int x,int y,double v){_add(x,y,v);_add(y,x,0);h[x]->rev=h[y];h[y]->rev=h[x];}


int n,m,k,a[NM],b[NM],c[NM];
int d[NM],cnt[NM],tot;
double ans;


double maxflow(){
    double flow=0,s=inf;edge*j;
    mem(cnt);mem(d);mem(p);mem(tmp);
    inc(i,0,n)tmp[i]=h[i];
    cnt[0]=tot;
    for(int x=0;d[x]<tot;){
	for(j=tmp[x];j;j=j->next)if(j->v>eps&&d[j->t]+1==d[x])break;
	if(j){
	    s=min(s,j->v);p[j->t]=tmp[x]=j;
	    if((x=j->t)==n){
		for(;x;x=p[x]->rev->t)p[x]->v-=s,p[x]->rev->v+=s;
		flow+=s;s=inf;
	    }
	}else{
	    if(!--cnt[d[x]])break;d[x]=tot;
	    link(x)if(j->v>eps&&d[x]>d[j->t]+1)tmp[x]=j,d[x]=d[j->t]+1;
	    cnt[d[x]]++;
	    if(p[x])x=p[x]->rev->t;
	}
    }
    return flow;
}


bool check(double t){
    mem(e);mem(h);o=e;
    inc(i,1,k)add(i+m,n,t);
    inc(i,1,m)add(0,i,1),add(i,a[i]+m,inf),add(i,b[i]+m,inf);
    return m-maxflow()>eps;
}

bool v[NM];
queue<int>q;
void bfs(){
    q.push(0);v[0]++;
    while(!q.empty()){
	int t=q.front();q.pop();
	link(t)if(!v[j->t]&&j->v>eps)v[j->t]++,q.push(j->t);
    }
}

int main(){
    //freopen("data.in","r",stdin);
    while(~scanf("%d%d",&k,&m)){
    n=m+k+1;tot=1+n;ans=0;mem(v);
    inc(i,1,m)a[i]=read(),b[i]=read();
    for(double x=0,y=n;x+eps<y;)
	if(check(mid))ans=mid,x=mid;else y=mid;
    check(ans-eps);tot=0;
    bfs();tot=0;
    inc(i,1,k)if(v[i+m])c[++tot]=i;
    if(!tot)c[tot=1]=1;
    printf("%d\n",tot);
    inc(i,1,tot)printf("%d\n",c[i]);
    }
    return 0;
}

 

 

 

 

Hard Life

Time Limit: 8000MS Memory Limit: 65536K
Total Submissions: 9994 Accepted: 2905
Case Time Limit: 2000MS Special 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, bin, aibi) 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 ≤ kn) — 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
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
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]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值