第一问最小割,第二问:

设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. PHP生成CSV文件

    CSV文件的定义这里就不多做介绍了,难能可贵的是用Excel可以直接打开CSV文件.用PHP输出CSV文件本身很简单,但是大家如果有业务需求,下面的代码可以作为参考. $tableheader = a ...

  2. HLG1744组合数学问题与lucas定理运用

    The figure below shows Pascal's Triangle: Baby H divides Pascal's Triangle into some Diagonals, like ...

  3. PHP网页数据正则采集

    $url ="https://********"; $contents = file_get_contents($url); //抓取页面数据 //如果出现中文乱码使用下面代码 / ...

  4. linux awk 内置函数详细介绍(实例)

    这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...

  5. sort如何按指定的列排序

    <1>[root@localhost company]# cat test 06d7            145             4192542506e1            ...

  6. 74 使用BitSet输出数组中的重复元素

    [本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...

  7. codeforces 483B Friends and Presents 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...

  8. codeforces 459C Pashmak and Buses 解题报告

    题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...

  9. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  10. java读取本地properties文件

    package cn.edu.hbcf.pojo; import java.io.FileNotFoundException; import java.io.IOException; import j ...