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

枚举一个点,硬点它一定要被选中,那么以它为根,如果选了$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. java注解(Annotation)的简单介绍

    注解你可以理解为一个特殊的类,或者接口其自定义个格式形如 public @interface 注解名(){ //注解的属性,特别提醒当注解的属性为value时,在对其赋值时,可以不写value,而直接 ...

  2. vue.单选和多选,纯css自定义单选框样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. PHP 抽象类,接口,抽象方法,静态方法

    1.Abstract class(抽象类) 抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 抽象类 ...

  4. 使用Idea远程部署调试tomcat

    转自:http://blog.csdn.net/jane1229/article/details/52402119 远程服务器的配置: 1.在远程服务器安装jdk和tomcat 2.配置环境变量 PA ...

  5. L2-001. 紧急救援---(Dijkstra,记录路径)

    https://www.patest.cn/contests/gplt/L2-001 L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...

  6. [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树

    题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...

  7. bzoj 1303 杂题

    首先如果一个数是中位数,在这段区间中比他大的数量=比他小的数量,那么如果一个数比他大设为1,比他小设为-1,设要求的数在数组中的位置是mid,那么我们可以用num[i] 表示1-mid这一段中,j-m ...

  8. bzoj 2064 DP

    这道题可以抽象成两个数列,将一个数列变换为另一个 数列的代价最小 首先我们可以处理出所有的状态代表,对于每个状态 用二进制来表示,代表的是两个数列中的每一项选还是不选 那么答案最多为n1+n2-2,也 ...

  9. 遍历文档内容,得到HTML层级结构

    嗯..没发现有写好的,那就自己写一个,刚好自己今天看了DOM操作的知识点,巩固一下. HTML可以表示为一个层次结构,生成的DOM Tree 就是类似与数据结构中的树一样,每个DOM节点都有它的chi ...

  10. laravel 获得各个根文件夹路径的方法及路由的一些使用

    各个根文件夹路径的方法 APP目录: app_path(); config目录: config_path(); public目录: public_path(); storage目录: storage_ ...