[51nod1325]两棵树的问题
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]两棵树的问题的更多相关文章
- LeetCode——Same Tree(判断两棵树是否相同)
问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- WPF的两棵树与绑定
原文:WPF的两棵树与绑定 先建立测试基类 public class VisualPanel : FrameworkElement { protected VisualCollection Chi ...
- element ui改写实现两棵树
使用element ui组件库实现一个table的两棵树的效果 效果如下,左边树自动展开一级,右边树默认显示楼层,然后可以一个个展开 代码如下 <el-table :data="rel ...
- 51 NOD 1325 两棵树的问题
Discription 对于 100% 的数据, N<=50. solution: 发现N比较小,所以我们可以花O(N^2)的代价枚举两颗树的联通块的LCA分别是哪个点,然后现在问题就变成了:选 ...
- 51nod 1325 两棵树的问题(最大权闭合子图)
首先如果点权全都为正,就可以直接选所有的点. 活在梦里.. 考虑枚举一个点\(i\),作为我们选择的集合中的一个点. 然后我们把另一个点\(j\)选入集合的时候必须把两棵树中\(i\)和\(j\)路径 ...
- HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)
6315.Naive Operations 题意很好理解,但是因为区间求和求的是向下取整的a[i]/b[i],所以直接分数更新区间是不对的,所以反过来直接当a[i]==b[i]的时候,线段树对应的位置 ...
- 判断两棵树是否相等 leecode
很简单 提交代码 https://oj.leetcode.com/problems/same-tree/ iven two binary trees, write a function to chec ...
- hdu-3015 Disharmony Trees---离散化+两个树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...
- LeetCode——1305. 两棵二叉搜索树中的所有元素
给你 root1 和 root2 这两棵二叉搜索树. 请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序.. 示例 1: 输入:root1 = [2,1,4], root2 = [1,0 ...
随机推荐
- CLR via C#读书笔记二:类型基础
1.CLR允许将对象转换为它的(实际)类型或者它的任何基类型. 2.is操作符检测对象是否兼容于指定类型,is操作符永远不抛出异常. 3.as操作符返回对同一个对象的非null引用.如果对象不兼容,a ...
- 网易云易盾与A10 Networks达成战略合作 携手打造抗DDoS攻击的解决方案
欢迎访问网易云社区,了解更多网易技术产品运营经验. 2018年9月,网易云易盾宣布,与智能和自动化网络安全解决方案提供商A10 Networks结成战略合作伙伴关系.双方将在抗DDoS攻击领域展开深入 ...
- 理解Python的装饰器
看Flask文档时候看到关于cache的装饰器,有这么一段代码: def cached(timeout=5 * 60, key=’view/%s’): def decorator(f): @wraps ...
- 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)
题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...
- hdu1175连连看(dfs+细节)
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Appium(Python)驱动手机淘宝App
请注意操作步骤: 1. 用数据线连接手机, 打开开发者模式, 并赋予相关权限, 并保持不锁屏状态: 2. 启动Appium桌面服务端: 3. 运行程序: 首次运行, Appium会在手机上安装3个Ap ...
- Python3安装pywin32模块
假如你安装的是Python3.6, 那么可以直接用PyCharm或者pip安装pywin32模块: 但是, 由于我安装的是Python3.7, 所以PyCharm或者pip都无法成功安装pywin32 ...
- 第三模块:面向对象&网络编程基础 第3章 选课系统作业讲解
01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课系统作业讲解4 01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课 ...
- Python输入数据类型判断正确与否的函数大全(非常全)
对于python输入数据类型判断正确与否的函数大致有三类: (1)type(),它的作用直接可以判断出数据的类型 (2)isinstance(),它可以判断任何一个数据与相应的数据类型是否一致,比 ...
- 【转】网游服务器中的GUID(唯一标识码)实现-基于snowflake算法
本文中的算法采用twitter的snowflake算法,具体请搜索介绍,原来是用Scala写的,因我项目需要,改写成C++语言,主要用于高效的生成唯一的ID, 核心算法就是毫秒级时间(41位)+机器I ...