description

题面

solution

点分治+最小割。

点分必选的重心,再在树上dfs判交,转化为最大权闭合子图。

可以做\(k\)棵树的情况。

code

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define RG register
#define il inline
using namespace std;
typedef long long ll;
typedef double dd;
const int N=205;
const int M=20;
const int mod=1e9+7;
const int inf=2147483647;
il ll read(){
RG ll d=0,w=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch<='9'&&ch>='0')d=d*10+ch-48,ch=getchar();
return d*w;
} int n,sum,rt,ans=-inf,val[N],ret;
int head[N],nxt[N<<1],to[N<<1],cnt;
il void add(int u,int v){
to[++cnt]=v;
nxt[cnt]=head[u];
head[u]=cnt;
} int S,T,dhead[N],dnxt[N<<1],dto[N<<1],dval[N<<1],dcnt;
il void addedge(int u,int v,int w){
dto[++dcnt]=v;
dnxt[dcnt]=dhead[u];
dval[dcnt]=w;
dhead[u]=dcnt; dto[++dcnt]=u;
dnxt[dcnt]=dhead[v];
dval[dcnt]=0;
dhead[v]=dcnt;
} queue<int>Q;int dep[N],cur[N];
il bool bfs(){
for(RG int i=1;i<=T;i++)dep[i]=0;
while(!Q.empty())Q.pop();
dep[S]=1;Q.push(S);
while(!Q.empty()){
RG int u=Q.front();Q.pop();
for(RG int i=dhead[u];i;i=dnxt[i]){
RG int v=dto[i];
if(!dep[v]&&dval[i]){
dep[v]=dep[u]+1;
Q.push(v);
}
}
}
return dep[T];
} int dfs(int u,int t,int power){
if(u==t)return power;
for(RG int &i=cur[u];i;i=dnxt[i]){
RG int v=dto[i];
if(dep[v]==dep[u]+1&&dval[i]){
RG int d=0;
if(d=dfs(v,t,min(power,dval[i]))){
dval[i]-=d;
dval[i^1]+=d;
return d;
}
}
}
return 0;
} il int Dinic(){
RG int ret=0,d;
while(bfs()){
for(RG int i=1;i<=T;i++)cur[i]=dhead[i];
while(d=dfs(S,T,inf))ret+=d;
}
return ret;
} int sz[N],w[N],cover[N],tot,pd[N];bool vis[N];
void dfscover(int u,int fa){
cover[u]=tot;pd[u]=dhead[u]=0;
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==fa||vis[v])continue;
dfscover(v,u);
}
}
void dfspd(int u,int fa){
RG int x=u>n?u-n:u;pd[x]++;
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==fa)continue;
RG int y=v>n?v-n:v;
if(cover[y]==tot)dfspd(v,u);
}
} void dfsadd(int u,int fa){
RG int x=u>n?u-n:u;if(u<=n&&val[u]>0)ret+=val[u];
if(u<=n&&val[x])
val[x]>0?addedge(S,x,val[x]):addedge(x,T,-val[x]);
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==fa)continue;
RG int y=v>n?v-n:v;
if(cover[y]==tot&&pd[y]==2){
addedge(y,x,inf);
dfsadd(v,u);
}
}
}
il void calc(int u){
tot++;dcnt=1;ret=0;
S=sum+1;T=sum+2;dhead[S]=dhead[T]=0;
dfscover(u,0);
dfspd(u,0);dfspd(u+n,0);
//addedge(S,u,inf);??????
dfsadd(u,0);dfsadd(u+n,0);
ans=max(ans,ret-Dinic());
} void getrt(int u,int fa){
sz[u]=1;w[u]=0;
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==fa||vis[v])continue;
getrt(v,u);sz[u]+=sz[v];
w[u]=max(w[u],sz[v]);
}
w[u]=max(w[u],sum-sz[u]);
if(w[rt]>w[u])rt=u;
}
void solve(int u){
calc(u);vis[u]=1;
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(vis[v])continue;
sum=sz[v];rt=0;
getrt(v,0);
solve(rt);
}
} int main()
{
n=read();
for(RG int i=1;i<=n;i++)val[i]=read();
for(RG int i=1,u,v;i<n;i++){
u=read()+1;v=read()+1;add(u,v);add(v,u);
}
for(RG int i=1,u,v;i<n;i++){
u=read()+1;v=read()+1;add(u+n,v+n);add(v+n,u+n);
}
w[0]=sum=n;rt=0;
getrt(1,0);
solve(rt);
printf("%d\n",ans);
return 0;
}

Question

写最小割的时候,如果使用

addedge(S,u,inf);

来强制重心必选

就会\(WA\)在最后一个数据点

如果不写这句话就\(A\)掉了

如果有\(dalao\)知道是为什么的话欢迎在下方的评论给出建议

[51nod1325]两棵树的问题的更多相关文章

  1. LeetCode——Same Tree(判断两棵树是否相同)

    问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  2. WPF的两棵树与绑定

    原文:WPF的两棵树与绑定   先建立测试基类 public class VisualPanel : FrameworkElement { protected VisualCollection Chi ...

  3. element ui改写实现两棵树

    使用element ui组件库实现一个table的两棵树的效果 效果如下,左边树自动展开一级,右边树默认显示楼层,然后可以一个个展开 代码如下 <el-table :data="rel ...

  4. 51 NOD 1325 两棵树的问题

    Discription 对于 100% 的数据, N<=50. solution: 发现N比较小,所以我们可以花O(N^2)的代价枚举两颗树的联通块的LCA分别是哪个点,然后现在问题就变成了:选 ...

  5. 51nod 1325 两棵树的问题(最大权闭合子图)

    首先如果点权全都为正,就可以直接选所有的点. 活在梦里.. 考虑枚举一个点\(i\),作为我们选择的集合中的一个点. 然后我们把另一个点\(j\)选入集合的时候必须把两棵树中\(i\)和\(j\)路径 ...

  6. HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)

    6315.Naive Operations 题意很好理解,但是因为区间求和求的是向下取整的a[i]/b[i],所以直接分数更新区间是不对的,所以反过来直接当a[i]==b[i]的时候,线段树对应的位置 ...

  7. 判断两棵树是否相等 leecode

    很简单 提交代码 https://oj.leetcode.com/problems/same-tree/ iven two binary trees, write a function to chec ...

  8. hdu-3015 Disharmony Trees---离散化+两个树状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...

  9. LeetCode——1305. 两棵二叉搜索树中的所有元素

    给你 root1 和 root2 这两棵二叉搜索树. 请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序.. 示例 1: 输入:root1 = [2,1,4], root2 = [1,0 ...

随机推荐

  1. 一、Lambda表达式

    一.Lambda是什么? Lambda是一个匿名函数,我们可以把Lambda理解为是一段可以传递的代码.可以写出简洁.灵活的代码.作为一种更紧凑的代码风格,使java的语言表达能力得到提升. 二.La ...

  2. Visual Studio 智能提示功能消失解决办法

    步骤如下: 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visual Studio Tools --> Developer Command ...

  3. uvaoj 101 - The Blocks Problem(vector应用+技巧)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...

  4. Windows运行机理——消息与消息队列

    Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 Windows程序设计时一种基于消息的时机驱动方式的设计模式,完全不同 ...

  5. JMeter 录制APP脚本

    具体步骤: 1.启动JMeter: 2.在“测试计划”中添加“线程组”: 3.“工作台”中添加“HTTP代理服务器”: 4.配置代理服务器:端口:8888(默认值),HTTPS Domains:192 ...

  6. 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  7. Linux命令应用大词典-第14章 显示登录用户

    14.1 w:详细查询已登录当前计算机的用户 14.2 who:显示已登录当前计算机用户的简单信息 14.3 whoami:显示与当前的有效ID相关联的用户名 14.4 logname:显示当前用户的 ...

  8. zookeeper应用:屏障、队列、分布式锁

    zookeeper工具类: 获取连接实例:创建节点:获取子节点:设置节点数据:获取节点数据:访问控制等. package org.windwant.zookeeper; import org.apac ...

  9. 【Python 开发】第一篇:计算机基础

    一.计算机基础 首先Python是一门编程语言 语言: 那什么是语言? 语言就是一种事物与另一种事物沟通的介质.所以说编程语言是程序员跟计算机沟通的介质. 什么是编程: 准确来说就是程序员用计算机所能 ...

  10. 论文笔记:Attentional Correlation Filter Network for Adaptive Visual Tracking

    Attentional Correlation Filter Network for Adaptive Visual Tracking CVPR2017 摘要:本文提出一种新的带有注意机制的跟踪框架, ...