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 1sample input #2
4 0Sample Output
sample output #1
4
1
2
4
5sample 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 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;
}