Codeforces Round #523 (Div. 2) E. Politics(最小费+思维建图)
https://codeforces.com/contest/1061/problem/E
题意
有n个点(<=500),标记第i个点的代价a[i],然后分别在这n个点建两棵树,对于每颗树的每个点来说,都有一个p[i],代表他的子树中被标记的点的个数需要等于p[i],请你选择需要标记的点,使得可以满足上述两棵树的要求,并且使得代价最大,输出最大的代价或者-1(不存在)
思路
- 这道题难在建图
- 首先考虑子树问题,是否选择每个节点对他的子孙节点没有影响,但是对他自身及其祖先节点有影响,注意p[i]=0代表i的子树没有限制
假设v为u的子孙节点且p[u]>0(有限制),sum=p[v1]+p[v2]+..+p[vn],
sum>p[u],则对于本棵树来说不存在满足条件的选点情况
sum<=p[u],则剩下p[u]-sum的空间给本棵树和另外一颗树选择 - 设col[u]为最靠近u的有限制的祖先节点(包括u),那么选择u就等于填充了两棵树的col[u]节点
- 所以可以这样建图,建超源超汇S,T,将第一颗树的点和源点相连,cap=p[u]-sum,cost=0,将第二颗树的点和汇点相连,cap=p[u]-sum,cost=0,将两棵树之间的点col1[u]和col2[u]相连,cap=1,cost=-a[u],跑一个mcmf
- 还需要判一下左边容量等于右边容量并且最大流等于容量,才能存在满足条件的选点情况
#include<bits/stdc++.h>
#define N 2005
#define M 4000000
#define inf 0x3f3f3f3f
using namespace std;
int n,rt1,rt2,ent,S,T,i,u,v,q,x,hd[N],p[N],col[N],f[N],d[N],inq[N],pre[N],a[N];
int flow,cost,ans,in,out;
struct edge{
int u,v,nt,c,w;
edge(int u=0,int v=0,int nt=0,int c=0,int w=0):u(u),v(v),nt(nt),c(c),w(w){}
}E[M];
void fail(){
cout<<-1;exit(0);
}
void add(int u,int v,int c,int w){
E[++ent]=edge(u,v,hd[u],c,w);hd[u]=ent;
E[++ent]=edge(v,u,hd[v],0,-w);hd[v]=ent;
}
int dfs(int u,int fa,int tar){
if(p[u])tar=u;
col[u]=tar;
int cnt=0,tp=p[u];
for(int i=hd[u];i;i=E[i].nt){
int v=E[i].v;
if(v==fa)continue;
cnt+=dfs(v,u,tar);
}
if(p[u]){if(p[u]<cnt)fail();p[u]-=cnt;cnt=tp;}
return cnt;
}
int bf(){
queue<int>Q;
for(i=0;i<=T;i++)d[i]=inf;
memset(inq,0,sizeof(inq));
d[S]=0;inq[S]=1;pre[S]=0;f[S]=inf;Q.push(S);
while(!Q.empty()){
int u=Q.front();Q.pop();
inq[u]=0;
for(int i=hd[u];i;i=E[i].nt){
edge &e=E[i];
if(e.c&&d[e.v]>d[u]+e.w){
d[e.v]=d[u]+e.w;
pre[e.v]=i;
f[e.v]=min(f[u],e.c);
if(!inq[e.v]){inq[e.v]=1;Q.push(e.v);}
}
}
}
return d[T]<inf;
}
void mcmf(){
if(in!=out)fail();
flow=0;cost=0;
while(bf()){
flow+=f[T];cost-=d[T]*f[T];
for(int i=T;i!=S;i=E[pre[i]].u){
E[pre[i]].c-=f[T];
E[pre[i]^1].c+=f[T];
}
}
if(flow!=in)fail();
cout<<cost<<endl;
}
int main(){
cin>>n>>rt1>>rt2;rt2+=n;
ent=1;S=2*n+1;T=2*n+2;
for(i=1;i<=n;i++)scanf("%d",&a[i]);
for(i=0;i<n-1;i++){scanf("%d%d",&u,&v);add(u,v,0,0);}
for(i=0;i<n-1;i++){scanf("%d%d",&u,&v);u+=n;v+=n;add(u,v,0,0);}
cin>>q;
for(i=0;i<q;i++){scanf("%d%d",&u,&x);p[u]=x;}
cin>>q;
for(i=0;i<q;i++){scanf("%d%d",&u,&x);p[u+n]=x;}
dfs(rt1,0,rt1);dfs(rt2,0,rt2);
memset(hd,0,sizeof(hd));ent=1;
in=out=0;
for(i=1;i<=n;i++){in+=p[i];if(p[i])add(S,i,p[i],0);}
for(i=1;i<=n;i++){out+=p[i+n];if(p[i+n])add(i+n,T,p[i+n],0);}
for(i=1;i<=n;i++)add(col[i],col[i+n],1,-a[i]);
mcmf();
}
Codeforces Round #523 (Div. 2) E. Politics(最小费+思维建图)的更多相关文章
- Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)
You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #523 (Div. 2)
Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
- Codeforces Round #523 (Div. 2) Cdp
题:https://codeforces.com/contest/1061/problem/C 题意:给你一个序列,我们求他们子序列的个数,这个子序列有个限制就是每一个子序列上的值都必须是能整除他的下 ...
- Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分
题意:给出n个电视节目的起始和结束时间 并且租一台电视需要x +y*(b-a) [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...
- Codeforces Round #523 (Div. 2) D. TV Shows
传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 题意: 有n个节目,每个节目都有个开始时间和结束时间. 定义x,y分别为租电视需要的花 ...
随机推荐
- TZOJ 3659 神奇的探险之旅(有向无环每个点只能经过一次最长路dij)
描述 我们正在设计这样的一款儿童探险游戏:游戏由很多故事场景组成,每个场景中都有一个问题,游戏根据玩家的回答将进入下一场景,经过巧妙的设计,我们保证在一次“探险旅行”中,不会重复的进入任何相同的场景, ...
- TZOJ 1221 Tempter of the Bone(回溯+剪枝)
描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...
- python 中 类型转换 bytes
https://www.cnblogs.com/sesshoumaru/p/5980090.html
- C++ 中的RTTI机制详解
前言 RTTI是”Runtime Type Information”的缩写,意思是运行时类型信息,它提供了运行时确定对象类型的方法.RTTI并不是什么新的东西,很早就有了这个技术,但是,在实际应用中使 ...
- swift - 3D 视图,截图,关键字搜索
1.xib 上的 3D效果 按钮 2. import UIKit //1.导入框架 import MapKit class ViewController: UIViewController { @IB ...
- Android camera2 回调imagereader 从Image拿到YUV数据转化成RGB,生成bitmap并保存
ImageUtil.java import android.graphics.ImageFormat; import android.media.Image; import android.os.Bu ...
- js学习(初)
一种弱数据类型语言 var 基础: 处理字符串的函数 数组基础操作 流程控制语句 选择,分支 循环for for in for(索引变量 in 对象){ 语句块 } 面向对象: js语言的对象就是 ...
- js iterable类型
遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...
- ofo退押金脚本
同事钉钉给的 因为押金一直没退,电话很难打进去,咨询客服排队要等好久,一直几千位. 长时间挂机就自动退出客服了,所以自动写了一个脚本,目前已经成功退押金了.所以共享出来 1.关注ofo小黄车订阅号,注 ...
- snort学习笔记
Snort有三种工作模式:嗅探器.数据包记录器.网络入侵检测系统(ids). 嗅探器模式仅仅是从网络上读取数据包并作为连续不断的流显示在终端上. 数据包记录器模式把数据包记录到硬盘上. 网络入侵检测模 ...