第一问最小割,第二问:

设du[i]表示i点的度数,则要最小化$\frac{|1集合的du[i]之和-2集合的du[i]之和|}{2}$,

压位01背包即可。

#include<cstdio>
#include<bitset>
using namespace std;
const int N=40010,inf=~0U>>2;
struct edge{int t,f;edge*nxt,*pair;}*g[N],*d[N],pool[240000],*cur=pool;
int Case,n,m,cnt,i,x,y,S,T,h[N],gap[N],maxflow,du[N],ans;bitset<19910>f;
inline int abs(int x){return x>0?x:-x;}
inline int min(int x,int y){return x<y?x:y;}
inline void add(int s,int t,int f){
edge*p=cur++;p->t=t;p->f=f;p->nxt=g[s];g[s]=p;
p=cur++;p->t=s;p->f=0;p->nxt=g[t];g[t]=p;
g[s]->pair=g[t];g[t]->pair=g[s];
}
int sap(int v,int flow){
if(v==T)return flow;
int rec=0;
for(edge*p=d[v];p;p=p->nxt)if(h[v]==h[p->t]+1&&p->f){
int ret=sap(p->t,min(flow-rec,p->f));
p->f-=ret;p->pair->f+=ret;d[v]=p;
if((rec+=ret)==flow)return flow;
}
if(!(--gap[h[v]]))h[S]=T;
gap[++h[v]]++;d[v]=g[v];
return rec;
}
int main(){
for(scanf("%d",&Case);Case--;printf("%d\n",ans)){
scanf("%d%d",&n,&m),S=n+m+m+1,T=S+1,cnt=n;
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
du[x]++,du[y]++;
add(S,++cnt,1),add(++cnt,T,1);
add(cnt-1,x,inf),add(x,cnt,inf);
add(cnt-1,y,inf),add(y,cnt,inf);
}
add(S,1,inf),add(2,T,inf);
for(gap[maxflow=0]=T,i=1;i<=T;i++)d[i]=g[i];
while(h[S]<T)maxflow+=sap(S,inf);
printf("%d ",m*2-maxflow);
for(cur=pool,i=0;i<=T;i++)g[i]=d[i]=NULL,h[i]=gap[i]=0;
for(f.reset(),f[0]=1,i=3;i<=n;i++)f|=f<<du[i];
for(ans=inf,i=0;i<=m;i++)if(f[i])ans=min(ans,abs(du[1]+i-m));
for(i=1;i<=n;i++)du[i]=0;
}
return 0;
}

  

BZOJ4155 : [Ipsc2015]Humble Captains的更多相关文章

  1. 【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP

    [BZOJ4155][Ipsc2015]Humble Captains Description 每天下午放学时都有n个zky冲出教室去搞基.搞基的zky们分成两队,编号为1的zky是1号队的首领,编号 ...

  2. 【BZOJ4155】[Ipsc2015]Humble Captains

    题解: 第一问裸的最小割 第二问考虑贪心 我们把边权平均分配给两个点 然后就变成了给n个数分两组差最小 np-hard问题 暴力背包,操作存在区间左移,右移,or bieset优化

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. 【USACO 3.1】Humble Numbers(给定质因子组成的第n大的数)

    题意:给你k(≤100)个质数,求质因子只包含它们的第n大的数. 题解: 方法一:维护一个数组,一开始只有给出的质数在里面,用每个质数去乘以数组中每个数,然后归并排序,长度保留到n,一轮接一轮,直到乘 ...

  5. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  6. HDU 1058 Humble Numbers(离线打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...

  7. HDU - The number of divisors(约数) about Humble Numbers

    Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence ...

  8. A - Humble Numbers

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  9. The number of divisors(约数) about Humble Numbers[HDU1492]

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. Unity3D使用小技巧

    原地址:http://unity3d.9tech.cn/news/2014/0411/40178.html 1.Crtl+f摄像机自动适配场景. 2.可以用一个立方体作为底盘. 3.人物角色可以直接引 ...

  2. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  3. 找不到提交和更新按钮,subversion不见了,无法更新和上传代码

    1.查看settings/plugins/下有没有subversion 插件,如果有,确保勾上. 2.VCS->Enable Version Control Integration...

  4. php友好格式化时间

    php格式化时间显示 function toTime($time) {//$time必须为时间戳 $rtime = date("Y-m-d H:i",$time); $htime ...

  5. PHP中magic_quotes_gpc动态关闭无效的问题

    昨天浏览线上项目,发现了一个问题:部分文本输出中的引号前多了一道反斜杠,比如: 引号内容多了\"反斜杠\" 单从页面展现的结果来看,猜测应该是PHP中的magic_quotes_g ...

  6. php 二分查找

    <?php /**二分查找:查找一个值在数组中的位置 * @$arr:操作的数组,前提是按顺序排列 * @$val:查找的值 * @$start:查找的起始位置,默认从数组的第一个数找起 * @ ...

  7. 经典的SQL面试题

    SQL中 inner join. left join .right join. outer join之间的区别 A表(a1,b1,c1) B表(a2,b2) a1 b1 c1 a2 b2 01 数学 ...

  8. linux下emacs配置文件

    1:安装.在ubuntu下使用命令 sudo apt-get install emacs,即可,我使用的是ubuntu的10.04的版本,在里面使用了据说是163的2个源. 1.1:如何更新快速的源, ...

  9. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  10. ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(二) 实现聊天室连接

    上一篇已经简单介绍了layim WebUI即时通讯组件和获取数据的后台方法.现在要讨论的是SingalR的内容,之前都是直接贴代码.那么在贴代码之前先分析一下业务模型,顺便简单讲一下SingalR里的 ...