Hard Life (POJ-3155) (minimum cut Method)

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

Meaning of the questions: multiple sets of data, each given n, m two numbers, n represents an individual company, there are m conflict relationship, and now the company decided to lay off, to cut the highest rate of conflict, including conflict of these = the number of people there is a conflict / number, to cut output and the total number of people

Ideas:

Simply put, given n dots and m edges, and obtains at some point, so that the number of edges between these points / maximum points, i.e., seek the maximum density subgraph

This problem can be said to be the largest density sub-title template map, use half the minimum cut + model can solve specific ideas: Click here

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 res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
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]; 
int head[N],tot;
int n,m,S,T;
Pair pastEdge[N];//原图边集
int degree[N];//度数
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(S, i, m * 1.0);
        addedge(i, T, m + 2 * g - degree[i]);
    }
    for (int i = 0; i < m; i++) {//原图中的边
        addedge(pastEdge[i].first, pastEdge[i].second, 1.0);
        addedge(pastEdge[i].second, pastEdge[i].first, 1.0);
    } 
}
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 maxflow;     
}

void findNode(int x) {//寻找残量网络中可到达的点
    vis[x] = true;
    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;
        }

        memset(degree,0,sizeof(degree));
        S=0,T=n+1;
        ans=T+1;

        for (int i = 0; i < m; i++) {
            int x,y;
            scanf("%d%d", &x, &y);
            pastEdge[i].first = x;
            pastEdge[i].second = y;
            degree[x]++;
            degree[y]++;
        }

        double left = 0, right = m;
        while (right - left >= 1.0 / (n*n)) { //论文给出了证明,不同解之间误差的精度不超过1/(n*n)
            double mid = (left + right) / 2;
            makeMap(mid); //根据mid值重新建图
            double hg = (1.0 * n * m - ISAP()) / 2;
            if (hg < 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 - 1);
        for (int i = 1; i <= n; i++)
            if (vis[i])
                printf("%d ", i);
        printf("\n");
    }  
    return 0;  
} 

 

Released 1871 original articles · won praise 702 · Views 1.94 million +

Guess you like

Origin blog.csdn.net/u011815404/article/details/102685838