Hard Life

Time Limit: 8000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3155
64-bit integer IO format: %lld      Java class name: Main

Special Judge
 

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 ≤ aibi ≤ nai ≠ 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

Source

 
解题:最大密度子图。。。学习ing。。。。
 
第一种建模方案,转化成最大权闭合图
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn<<];
int head[maxn],d[maxn],cur[maxn],x[maxn],y[maxn];
int tot,S,T,n,m;
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
queue<int>q;
q.push(S);
d[S] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow>&&d[e[i].to] == d[u]+ && (a=dfs(e[i].to,min(e[i].flow,low))) > ){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double flow = m;
while(bfs()){
memcpy(cur,head,sizeof(head));
double tmp = dfs(S,INF);
if(tmp > ) flow -= tmp;
}
return flow <= ;
}
void build(double delta){
memset(head,-,sizeof(head));
tot = ;
for(int i = ; i <= m; ++i){
add(S,i+n,1.0);
add(i+n,x[i],INF);
add(i+n,y[i],INF);
}
for(int i = ; i <= n; ++i) add(i,T,delta);
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + m + ;
for(int i = ; i <= m; ++i)
scanf("%d %d",x+i,y+i);
if(m == ) printf("1\n1\n");
else{
double low = ,high = 1.0*m,mid;
const double exps = 1.0/(n*n);
while(high - low >= exps){
mid = (low + high)/2.0;
build(mid);
if(dinic()) high = mid;
else low = mid;
}
build(low);
dinic();
int cnt = ,ans[maxn];
for(int i = ; i <= n; ++i)
if(d[i] > ) ans[cnt++] = i;
printf("%d\n",cnt);
for(int i = ; i < cnt; ++i)
printf("%d%c",ans[i],'\n');
}
}
return ;
}

第二种建模方案

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
double flow;
arc(int x = ,double y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],cur[maxn],d[maxn],du[maxn];
int tot,S,T,n,m,cnt;
pii p[maxn*maxn];
void add(int u,int v,double flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S] = ;
queue<int>q;
q.push(S);
cnt = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
cnt++;
}
}
}
return d[T] > -;
}
double dfs(int u,double low){
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > && d[u] + == d[e[i].to]&&(a=dfs(e[i].to,min(e[i].flow,low)))>){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic(){
double ans = n*m;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans -= dfs(S,INF);
}
return ans/2.0 > ;
}
void build(double g){
memset(head,-,sizeof(head));
for(int i = tot = ; i < m; ++i){
add(p[i].first,p[i].second,);
add(p[i].second,p[i].first,);
}
for(int i = ; i <= n; ++i){
add(S,i,m);
add(i,T,m+g*2.0-du[i]);
}
}
int main() {
while(~scanf("%d %d",&n,&m)){
S = ;
T = n + ;
memset(du,,sizeof(du));
for(int i = ; i < m; ++i){
scanf("%d %d",&p[i].first,&p[i].second);
++du[p[i].first];
++du[p[i].second];
}
if(!m) printf("1\n1\n");
else{
const double exps = 1.0/(n*n);
double low = ,high = m;
while(high - low >= exps){
double mid = (low + high)/2.0;
build(mid);
if(dinic()) low = mid;
else high = mid;
}
build(low);
dinic();
printf("%d\n",cnt);
for(int i = ; i <= n; ++i)
if(d[i] > -) printf("%d\n",i);
}
}
return ;
}

POJ 3155 Hard Life的更多相关文章

  1. POJ 3155 Hard Life(最大密度子图+改进算法)

    Hard Life Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9012   Accepted: 2614 Case Ti ...

  2. POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分

    http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...

  3. POJ 3155 Hard Life(最大密度子图)

    裸题.输入一个无向图,输出最大密度子图(输出子图结点数和升序编号). 看了<最小割模型在信息学竞赛中的应用——胡伯涛>的一部分,感觉01分数规划问题又是个大坑.暂时还看不懂. 参考http ...

  4. poj 3155 最大密度子图

    思路: 这个还是看的胡伯涛的论文<最小割在信息学竞赛中的应用>.是将最大密度子图问题转化为了01分数规划和最小割问题. 直接上代码: #include <iostream> # ...

  5. poj 3155 二分+最小割求实型最小割(最大密集子图)

    /* 最大密集子图子图裸题 解法:设源点s和汇点t 根据胡波涛的<最小割模型在信息学中的应用> s-每个点,权值为原边权和m, 每个点-t,权值为m+2*g-degree[i], 原来的边 ...

  6. POJ 3155:Hard Life(最大密度子图)

    题目链接 题意 给出n个人,和m对有冲突的人.要裁掉一些人,使得冲突率最高,冲突率为存在的冲突数/人数. 思路 题意可以转化为,求出一些边,使得|E|/|V|最大,这种分数规划叫做最大密度子图. 学习 ...

  7. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  8. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  9. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

随机推荐

  1. Python发行版本Anaconda的安装说明:基于Anaconda2-4.3.1-Windows-x86_64

    Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.因为包含了大量的科学包,Anaconda 的下载文件比较大(约 531 MB),如果 ...

  2. DML语句(添加、更新和删除记录)

       a.添加记录(一次插入一行记录)     insert into 表名(字段名,字段名...)     values (字段值,字段值...)       insert into person ...

  3. Sybase数据库工具DbVisualizer乱码问题

    使用DbVisualizer来操作sybase数据库的时候,会出现乱码问题,中文变成  '口口'. 解决的方法例如以下: 将这三个字体都改成 "宋体"  或者改成 "PM ...

  4. linux文件与用户和群组

    文件基本属性 在图片中alogrithm的文件属性为drwxrwxr-x,其中d代表此文件为目录. 后面rwx,rwx,r-x分别代表文件所属者(ower),组(group),其他用户(other)的 ...

  5. 字符串的HashCode可能相同

    字符串的HashCode可能相同 学习了:http://blog.csdn.net/hl_java/article/details/71511815

  6. Leetcode--easy系列4

    #58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...

  7. android全磁盘加密

    android 全磁盘加密 什么是全磁盘加密? 全磁盘加密是使用一个密钥来为android设备上全部的用户数据加密的过程.一旦设备被加密,全部的用户创建的数据都将会在提交的磁盘之前自己主动加密,在读取 ...

  8. MySQL之----在java编程加强知识点

    在数据中,建表处理是非经常见且非常有用的方法. 表和表之间的关系有 1:1  1:N         N:N 三种方式. 1对1的方式 <span style="font-size:1 ...

  9. bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)

    3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...

  10. Elasticsearch yellow 意味着主分片可用,副本不可用

    摘自:http://unasm.com/2016/11/644/ 在通过 /_cluster/state 命令查看es 状态的时候,发现es 处于一个yellow的状态, 这个很奇怪,按照官方的解释, ...