又积累了一个网络流模型:最大权闭合子图,相关证明去看论文,感觉自己不是很懂证明,但现在还是先把建模记下来再说吧

枚举一个点,硬点它一定要被选中,那么以它为根,如果选了$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的更多相关文章

  1. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  2. 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)

    Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...

  3. Understanding theory (1)

    Source: verysmartbrothas.com It has been confusing since my first day as a PhD student about theory ...

  4. Machine Learning Algorithms Study Notes(3)--Learning Theory

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...

  5. Java theory and practice

    This content is part of the series: Java theory and practice A brief history of garbage collection A ...

  6. CCJ PRML Study Note - Chapter 1.6 : Information Theory

    Chapter 1.6 : Information Theory     Chapter 1.6 : Information Theory Christopher M. Bishop, PRML, C ...

  7. 信息熵 Information Theory

    信息论(Information Theory)是概率论与数理统计的一个分枝.用于信息处理.信息熵.通信系统.数据传输.率失真理论.密码学.信噪比.数据压缩和相关课题.本文主要罗列一些基于熵的概念及其意 ...

  8. Computer Science Theory for the Information Age-4: 一些机器学习算法的简介

    一些机器学习算法的简介 本节开始,介绍<Computer Science Theory for the Information Age>一书中第六章(这里先暂时跳过第三章),主要涉及学习以 ...

  9. Computer Science Theory for the Information Age-1: 高维空间中的球体

    高维空间中的球体 注:此系列随笔是我在阅读图灵奖获得者John Hopcroft的最新书籍<Computer Science Theory for the Information Age> ...

随机推荐

  1. Mockito中@Mock与@InjectMock

    Mockito是java单元测试中,最常用的mck工具之一,提供了诸多打桩方法和注解.其中有两个比较常用的注解,@Mock和@InjectMock,名字和在代码中使用 的位置都很像,对于初学者,很容易 ...

  2. javaScript中的this关键字解析

    this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.接下来,笔者就从作用域的角度粗谈下自己对this关键字的理解,希望能给到大家一些启示, ...

  3. 关于Javascript 闭包的理解

    一.什么是闭包? 官方”的解释是:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.相信很少有人能直接看懂这句话,因为他描述的太学术.其实这句话 ...

  4. 在生成的Debug中test.exe的同级目录下创建一个文件,如TestLog.log

    在上次编写一个日志类库时,想在.exe的同级目录下创建.log文件,对于这个路径的获得很简单,调用GetModuleFileName()函数即可.但是要去掉.exe而换成.log时,由于对字符串处理不 ...

  5. 逐步实现python版wc命令

    Python 如何处理管道输入输出 sys.stdin 等于打开了一个文件对象,所有输入的文件都会写入到标准输入文件中(键盘) sys.stdout 等于打来了一个文件对象,使用.write()把信息 ...

  6. django返回二进制图片

    @login_required def down_img(request, path): content = Storage().download(path) from django.http imp ...

  7. 一道面试题:C++相比C#或者java的优势到底在哪里

    被问到了这样一道面试题,当时就懵了,内心一直觉得C++肯定在很多方面要比C#或者java要牛b的. 但是真的不知道怎么回答. 问题是:你以前一直做得是.NET相关项目,现在为什么找C++开发相关工作呢 ...

  8. JAVA -- JDK JRE JAR

    转载:http://blog.csdn.net/wym19830218/article/details/5399401 JDK里面的工具也是用JAVA编写的,它们本身运行的时候也需要一套JRE,如C: ...

  9. android 调试 native 程序的方法

    一.背景 首先说需求,这个需求非常常见,就是android上需要的一个功能,linux已经有开源代码而且非常稳定,希望能直接porting过去使用,这个程序是pure c 的代码,也就是说,跟andr ...

  10. Linux指令详解useradd groupadd passwd chpasswd chage 密码修改

    Linux指令详解useradd groupadd passwd chpasswd chage 密码修改 http://speediness.blog.51cto.com/760841/1783661 ...