[xsy2164]theory

又积累了一个网络流模型:最大权闭合子图,相关证明去看论文,感觉自己不是很懂证明,但现在还是先把建模记下来再说吧
枚举一个点,硬点它一定要被选中,那么以它为根,如果选了$x$就必须要选$fa_x$,这就是闭合图的定义了,再加上权值最大,所以直接上最大权闭合子图即可
最大权闭合子图的建模方法:把原图中的每一条边容量设为$+\infty$,对每个$v_x\gt0$的点,连边$(s,x,v_x)$,对每个$v_x\lt0$的点,连边$(x,t,|v_x|)$,所有正权点的权值之和减去$s\rightarrow t$的最小割就是答案
#include<stdio.h>
#include<string.h>
const int inf=1000000000;
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
int h[110],cur[110],nex[810],to[810],cap[810],dis[110],q[110],M,S,T;
void add(int a,int b,int c){
M++;
to[M]=b;
cap[M]=c;
nex[M]=h[a];
h[a]=M;
M++;
to[M]=a;
cap[M]=0;
nex[M]=h[b];
h[b]=M;
}
bool bfs(){
int head,tail,i,x;
head=tail=1;
q[1]=S;
memset(dis,-1,sizeof(dis));
dis[S]=0;
while(head<=tail){
x=q[head];
head++;
for(i=h[x];i;i=nex[i]){
if(cap[i]>0&&dis[to[i]]==-1){
dis[to[i]]=dis[x]+1;
if(to[i]==T)return 1;
tail++;
q[tail]=to[i];
}
}
}
return dis[T]>0;
}
int dfs(int x,int flow){
if(x==T)return flow;
int i,f;
for(i=cur[x];i;i=nex[i]){
if(cap[i]>0&&dis[to[i]]==dis[x]+1){
f=dfs(to[i],min(flow,cap[i]));
if(f){
cap[i]-=f;
cap[i^1]+=f;
if(cap[i])cur[x]=i;
return f;
}
}
}
dis[x]=-1;
return 0;
}
int dicnic(){
int ans=0,tmp;
while(bfs()){
memcpy(cur,h,sizeof(h));
while(tmp=dfs(S,inf))ans+=tmp;
}
return ans;
}
struct tree{
int h[110],nex[210],to[210],M;
void reset(){
M=0;
memset(h,0,sizeof(h));
}
void add(int a,int b){
M++;
to[M]=b;
nex[M]=h[a];
h[a]=M;
}
void dfs(int f,int x){
for(int i=h[x];i;i=nex[i]){
if(to[i]!=f){
::add(to[i],x,inf);
dfs(x,to[i]);
}
}
}
}a,b;
int v[110],n;
int get(int x){
int i,sum;
M=1;
memset(h,0,sizeof(h));
a.dfs(0,x);
b.dfs(0,x);
sum=0;
for(i=1;i<=n;i++){
if(v[i]>0){
sum+=v[i];
add(S,i,v[i]);
}
if(v[i]<0)add(i,T,-v[i]);
}
return sum-dicnic();
}
int main(){
int T,i,x,y,ans;
scanf("%d",&T);
while(T--){
a.reset();
b.reset();
scanf("%d",&n);
S=n+1;
::T=n+2;
for(i=1;i<=n;i++)scanf("%d",v+i);
for(i=1;i<n;i++){
scanf("%d%d",&x,&y);
a.add(x,y);
a.add(y,x);
}
for(i=1;i<n;i++){
scanf("%d%d",&x,&y);
b.add(x,y);
b.add(y,x);
}
ans=-inf;
for(i=1;i<=n;i++)ans=max(ans,get(i));
printf("%d\n",ans);
}
}
[xsy2164]theory的更多相关文章
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)
Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...
- Understanding theory (1)
Source: verysmartbrothas.com It has been confusing since my first day as a PhD student about theory ...
- Machine Learning Algorithms Study Notes(3)--Learning Theory
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...
- Java theory and practice
This content is part of the series: Java theory and practice A brief history of garbage collection A ...
- CCJ PRML Study Note - Chapter 1.6 : Information Theory
Chapter 1.6 : Information Theory Chapter 1.6 : Information Theory Christopher M. Bishop, PRML, C ...
- 信息熵 Information Theory
信息论(Information Theory)是概率论与数理统计的一个分枝.用于信息处理.信息熵.通信系统.数据传输.率失真理论.密码学.信噪比.数据压缩和相关课题.本文主要罗列一些基于熵的概念及其意 ...
- Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
一些机器学习算法的简介 本节开始,介绍<Computer Science Theory for the Information Age>一书中第六章(这里先暂时跳过第三章),主要涉及学习以 ...
- Computer Science Theory for the Information Age-1: 高维空间中的球体
高维空间中的球体 注:此系列随笔是我在阅读图灵奖获得者John Hopcroft的最新书籍<Computer Science Theory for the Information Age> ...
随机推荐
- 如何用JavaScript做一个可拖动的div层
可拖动的层在Web设计中用处很多,比如在某些需要自定义风格布局的应用中,控件就需要拖动操作,下面介绍一个,希望可以满足你的需求,顺便学习一下可拖动的层是如何实现的. 下面是效果演示: 这个DIV可以移 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Sqlserver面试题
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 ...
- 51nod1031(简单斐波拉契数列)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1031 题意:中文题诶- 思路:对于第x块骨牌的情况,我们用a ...
- 【BZOJ】1592: [Usaco2008 Feb]Making the Grade 路面修整
[算法]动态规划DP [题解] 题目要求不严格递增或不严格递减. 首先修改后的数字一定是原来出现过的数字,这样就可以离散化. f[i][j]表示前i个,第i个修改为第j个数字的最小代价,a表示排序后数 ...
- swift方法 的写法,ui上拖拽的控件到controller里面的方法
直接点xcode右上角三个按键中间一下,左右拆分为storyboard和controller, 点击button,按ctrl,然后拖拽到controller里面即可生成对应的点击事件在controll ...
- 哈希Hash在字符串中的应用_C++
本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 哈希大家都会用撒,字符串显然都会写撒,那么哈希离散化字符串不就懂了?!(XXX的神逻辑,其实原文是:树都晓得吧,数组显然都会开呀,那么恭喜 ...
- USACO月赛2005 january volume
2013-09-18 08:12 由题可知,ans=∑i ∑j(x[i]-x[j]) 最后整理完之后应该是不同系数的X[i]相加,所以这道题就成了求不同x[i]的系数 对于X[i],它需要减前面(i ...
- DWM.EXE进程(Desktop Window Manager)不能删除
英文全拼:Desktop Window Manager(DWM) 进程描述:桌面窗口管理器文件位置:C:\Windows\System32进程简介:桌面窗口管理器是windows Vista及wind ...
- 移动开发之css3实现背景渐变效果
前段时间由于手机项目需要实现一个背景渐变功能, 开始是想切个小背景图平铺下, 后来想到css3可以实现,如是用下面的代码就实现了. .sec_case_list li span{ backgro ...