Hard Life(POJ-3155)(最大权闭合图解法)

Problem 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

题意:多组数据,每组给出 n、m 两个数,代表一个公司有 n 个人,有 m 对冲突关系,现在公司决定裁员,要裁掉冲突率最高的人,其中冲突率=这些人中存在的冲突数/人数,输出要裁掉的人的总数与编号

思路:

简单来说,就是给出 n 个点 m 条边,然后求出一些点,使得这些点之间的边数/点数最大,也即求最大密度子图

本题可以说是最大密度子图的模版题,利用二分+最大权闭合图模型即可解决,具体思路:点击这里

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL ans=1; while(b){if(b&1)ans*=a; a*=a; b>>=1;} return ans; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL ans=0; while(b){if(b&1)ans=(ans+a)%mod; a=(a<<=1)%mod; b>>=1; } return ans%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL ans=1,k=a; while(b){if((b&1))ans=multMod(ans,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return ans%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL ans=1; while(b){if(b&1)ans=(a*ans)%mod; a=(a*a)%mod; b>>=1; } return ans; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1500+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;
 
struct Edge {
    int to, next;
    double cap;
} edge[80 * N]; 
Pair pastEdge[N];//原图边集
int head[N],tot;
int n,m,S,T;
int ans;//层数
int level[N];//记录层次
int gap[N];//记录每组层次标号有几个
int numNode;//最大密度子图中点的个数
bool vis[N];//标记最大密度子图中的点
void addedge(int x, int y, double cap) {
    edge[tot].to = y;
    edge[tot].cap = cap;
    edge[tot].next = head[x];
    head[x] = tot++;
 
    edge[tot].to = x;
    edge[tot].cap = 0;
    edge[tot].next = head[y];
    head[y] = tot++;
}
void makeMap(double g) {
    memset(head,-1,sizeof(head));  
    tot=0;
 
    for(int i=1;i<=n;i++)//原图中的点到汇点 
        addedge(i,T,g); 
    for(int i=0;i<m;i++) {  
        //源点到原图中的每条边
        addedge(S,n+i+1,1.0);
        //原图中的每条边到之间建边
        addedge(n+i+1,pastEdge[i].first,INF);  
        addedge(n+i+1,pastEdge[i].second,INF);  
    }  
}
double dfs(int x, double minflow) {
    if (x == T)
        return minflow;
 
    double flow = 0.0;
    for (int i = head[x]; i != -1; i = edge[i].next) {
        int y = edge[i].to;
        if (edge[i].cap > 0) {
            if (level[y] + 1 == level[x]) {
                double newFlow =
                    edge[i].cap > minflow - flow ? minflow - flow : edge[i].cap;
                newFlow = dfs(y, newFlow);
 
                flow += newFlow;
                edge[i].cap -= newFlow;
                edge[i ^ 1].cap += newFlow;
 
                if (minflow - flow <= EPS)
                    return flow;
                if (level[S] >= ans)
                    return flow;
            }
        }
    }
 
    if (--gap[level[x]] == 0)
        level[S] = ans;
    level[x]++;
    gap[level[x]]++;
    return flow;
}
double ISAP() {
    double maxflow=0.0;    
    memset(gap,0,sizeof(gap));    
    memset(level,0,sizeof(level));  
    gap[0]=ans;    
 
    while(level[S]<ans)
        maxflow+=dfs(S,INF);
 
    return 1.0*m-maxflow;     
}
void findNode(int x) {//寻找残量网络中可到达的点
    vis[x] = true;
    if (x >= 1 && x <= n)
        numNode++;
    for (int i = head[x]; i != -1; i = edge[i].next) {
        int y = edge[i].to;
        if (vis[y] == false && edge[i].cap > 0)
            findNode(y);
    }
}
int main() {
    while (scanf("%d%d", &n, &m) != -1) {
        if (m == 0) {
            printf("1\n1\n");
            continue;
        }
 
        S=0,T=n+m+1;
        ans=T+1;  
        
        for(int i=0;i<m;i++)  
            scanf("%d%d",&pastEdge[i].first,&pastEdge[i].second);
 
        double left = 0, right = m;
        while (right - left >= 1.0 / (n*n)) { //论文给出了证明,不同解之间误差的精度不超过1/(n*n)
            double mid = (left + right) / 2;
            makeMap(mid); //根据mid值重新建图
 
            if (ISAP() < EPS) //如果小于0,g值太大,调整上界
                right = mid;
            else
                left = mid;
        }
        // printf("%lf",left);//最大密度
 
        makeMap(left); //根据最大密度建图
        ISAP();        //求最大权闭合图
 
        //寻找最大密度子图中的点
        numNode = 0;
        memset(vis, false, sizeof(vis));
        findNode(S);
        printf("%d\n", numNode);
        for (int i = 1; i <= n; i++)
            if (vis[i])
                printf("%d ", i);
        printf("\n");
    }  
    return 0;  
} 
发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/102683853
今日推荐