POI2014
...一个shabi和一堆神题的故事
今天只写了两道
之后随缘更吧
啊 顺便 snake我是不会更的
bzoj3829 POI2014 Farmcraft
当先走$u$再走$v$的时候时间是$f[u] + 1 + 2 \times size[u] + f[v] + 1$
当先走$u$再走$v$的时候时间是$f[v] + 1 + 2 \times size[v] + f[u] + 1$
#include<bits/stdc++.h>
using namespace std; inline int read()
{
int x = ,f = ;char ch = getchar();
for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
for(;isdigit(ch);ch = getchar())x = * x + ch - '';
return x * f;
}
int n;
const int maxn = ;
int first[maxn],to[maxn << ],nx[maxn << ],cnt;
int val[maxn],size[maxn],fa[maxn];
int f[maxn];
int sons[maxn];
inline void add(int u,int v){to[++cnt] = v;nx[cnt] = first[u];first[u] = cnt;}
inline bool cmp(int u,int v){return max(f[u], * size[u] + f[v]) < max(f[v], * size[v] + f[u]);}
inline void dfs(int x)
{
size[x] = ;
for(int i=first[x];i;i=nx[i])
{
if(to[i] == fa[x])continue;
fa[to[i]] = x;dfs(to[i]);size[x] += size[to[i]];
}
int nt = ,tmp = ;
f[x] = val[x];
for(int i=first[x];i;i=nx[i]){if(to[i] == fa[x])continue;sons[++nt] = to[i];}
sort(sons + ,sons + nt + ,cmp);
for(int i=;i<=nt;++i)f[x]=max(f[x],tmp*+f[sons[i]]+),tmp+=size[sons[i]];
}
int main()
{
n = read();
for(int i=;i<=n;i++)val[i] = read();
for(int i=;i<n;i++)
{
int u = read(),v = read();
add(u,v);add(v,u);
}
dfs();
cout<<max(f[],(n-)*+val[]);
}
bzoj3832 Rally
一个DAG,每条边长度都是$1$,求删掉一个点之后最长路的最小值
输出最小值和那个点
$n \leq 500000$
$m \leq 2 \times n$
sol:神题
首先我们建超级源$S$,超级汇$T$,每个点向源汇连边$(S -> X -> T)$
这样图上最长链就变成了$S$到$T$最长链
我们可以拓扑序$dp$出每个点到$S$到$T$的最长链$f[x]$和$g[x]$
这样就是对一个割集内的每条边$(x,y)$求$f[x] + g[y]$的最大值
我们另所有点都在$T$集,$S$在$S$集
每次从$T$删一个点加入$S$
删除的时候删掉它的入边 记录答案 然后加入它的出边
具体 我们要加入一个元素 删除一个元素 查询最大值
这个用堆或者线段树都可以
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x = ,f = ;char ch = getchar();
for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
for(;isdigit(ch);ch = getchar())x = * x + ch - '';
return x * f;
}
int n,m;
const int maxn = ;
struct Graph
{
int first[maxn],to[maxn << ],nx[maxn << ],cnt;
int ind[maxn],indd[maxn];
inline void add(int u,int v){to[++cnt] = v;nx[cnt] = first[u];first[u] = cnt;ind[v]++;indd[v]++;}
}G,rev;
priority_queue<int> q1,q2;
queue<int> q;
int dp[maxn],f[maxn];
inline void push(int x){q1.push(x);}
inline void del(int x){q2.push(x);while(!q1.empty() && !q2.empty() && (q1.top() == q2.top()))q1.pop(),q2.pop();}
inline int top(){if(q1.empty())return ;return q1.top();}
void toposort()
{
for(int i=;i<=n;i++)
if(G.ind[i] == )q.push(i);
while(!q.empty())
{
int now = q.front();q.pop();
for(int i=G.first[now];i;i=G.nx[i])
{
G.ind[G.to[i]]--;
dp[G.to[i]] = max(dp[G.to[i]],dp[now] + );
if(G.ind[G.to[i]] == )q.push(G.to[i]);
}
}
while(!q.empty())q.pop();
for(int i=;i<=n;i++)
if(rev.ind[i] == )q.push(i);
while(!q.empty())
{
int now = q.front();q.pop();
for(int i=rev.first[now];i;i=rev.nx[i])
{
rev.ind[rev.to[i]]--;
f[rev.to[i]] = max(f[rev.to[i]],f[now] + );
if(rev.ind[rev.to[i]] == )q.push(rev.to[i]);
}
}
}
void solve()
{
int ans = (n << ),id;
while(!q.empty())q.pop();
for(int i=;i<=n;i++)
if(G.indd[i] == )q.push(i);
while(!q.empty())
{
int now = q.front();q.pop();
del(f[now]);
for(int i=rev.first[now];i;i=rev.nx[i])del(dp[rev.to[i]] + f[now] + );
if(top() < ans)
{
ans = top();
id = now;
}
for(int i=G.first[now];i;i=G.nx[i])
{
push(f[G.to[i]] + dp[now] + );
G.indd[G.to[i]]--;
if(G.indd[G.to[i]] == )q.push(G.to[i]);
}
push(dp[now]);
}
printf("%d %d",id,ans);
}
int main()
{
n = read();m = read();
for(int i=;i<=m;i++){int u = read(),v = read();G.add(u,v);rev.add(v,u);}
toposort();
for(int i=;i<=n;i++)push(f[i]);
solve(); }
POI2014的更多相关文章
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 【BZOJ】【3522】【POI2014】Hotel
暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...
- 【BZOJ】【3831】【POI2014】Little Bird
DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...
- Bzoj 3831 [Poi2014]Little Bird
3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...
- BZOJ3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 195 Solved: 85[Submit][Status] ...
- 3522: [Poi2014]Hotel( 树形dp )
枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显 ...
- 【BZOJ3524/2223】[Poi2014]Couriers 主席树
[BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...
- 3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 253 Solved: 117[Submit][Status ...
随机推荐
- html中图片上传预览的实现
本地图片预览 第一种方法 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- RobotFramework-Selenium2Library--关键字
Selenium2Library用户关键字 *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arg ...
- bzoj4010【HNOI2015】菜肴制作
4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB Submit: 981 Solved: 480 [Submit][Statu ...
- EMI-CLK信号串电阻并电容
一般DMIC的CLK都会EMI超标,所以看到的案子这个DMIC CLK信号都会源端串接电阻和并电容 1,串电阻是为了信号的完整性,考虑到匹配的,一般说来这个电阻不是固定的,要随实际的PCB的走线的阻抗 ...
- Spring Security 表单登录
1. 简介 本文将重点介绍使用Spring Security登录. 本文将构建在之前简单的Spring MVC示例之上,因为这是设置Web应用程序和登录机制的必不可少的. 2. Maven 依赖 要将 ...
- JS 的引用赋值与传值赋值
这个问题说大不大说小不小,如果你有幸踩了这个坑,一定会找这篇文章,哈哈~ 现说一下JS数字的类型:基本类型和引用类型 先看下下面两个栗子: var a = 30; var b = a; a = 20; ...
- FullPage.js 活动单页 - 全屏滚动插件
插件描述:fullPage.js 是一个基于 jQuery 的插件,它能够很方便.很轻松的制作出全屏网站. https://www.uedsc.com/fullpage.html 官网 如今我们经常能 ...
- HDFS源码分析心跳汇报之整体结构
我们知道,HDFS全称是Hadoop Distribute FileSystem,即Hadoop分布式文件系统.既然它是一个分布式文件系统,那么肯定存在很多物理节点,而这其中,就会有主从节点之分.在H ...
- Java8中 Date和LocalDateTime的相互转换
一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使用以下方法: 1.从日期获取ZonedDateTime并使用其方法toL ...
- cesium学习--初识
一.Cesium 官方介绍:CesiumJS是一个开源的JavaScript库,用于世界级的3D地球仪和地图.任务是为静态和时间动态的内容创建领先的3D地球和地图,具有最好的性能.精度.视觉质量.平台 ...