[other] Div
https://www.luogu.org/problemnew/show/U16765
解法一
随机输出一组合法解。
复杂度 O(1)
预计得分 10~???
解法二
看完题目基本能想到大力贪心,通过树上差分统计经过每一条边的查询次数,贪心取访问次数最多的到儿子的边作为重链。
复杂度 O(nlogn)
预计得分 60~70
解法三
为什么解法二不能拿到满分呢?
注意到以下数据:
5 3 1 2 2 4 2 5 2 3 1 5 3 4 4 3 标准答案为
2 5 0 0 0 次数为5。
而解法二的答案为
2 3(或4)
0 0 0 次数为6
它是一个十字架型,2为中心,3,4,5均为2的儿子,注意到3~4的查询如果使用重链对答案是没有贡献的,或者说如果一个查询中,两点的LCA如果为某个点的父亲,那么它到该儿子之间的边是不是重链对答案没有影响,在差分的时候这样实现:
if(g[u][0]!=r)
{
tree[u].cnt++;
tree[r].cnt--;
}
if(g[v][0]!=r)
{
tree[v].cnt++;
tree[r].cnt--;
}
其中g[u][0]为倍增数组,即u的父亲节点,r是u和v的LCA,tree[].cnt是差分的记录变量。 复杂度 O(nlogn)
预计得分 100
解法四
模拟退火等随机化算法乱搞,我没试过,但是似乎可能可以得到很好的结果。
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ; #define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1
#define important T[jd].w = T[lson].w + T[rson].w struct Node_1{
int siz, son, fa, deep, tree, toop;
}P[N];
struct Node_2{
int v, nxt;
}G[N << ];
struct Node_3{
int l, r, f, w;
}T[N << ];
int n, Ti, now = , head[N], tim, bef[N], data[N]; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v){
G[now].v = v;
G[now].nxt = head[u];
head[u] = now ++;
} void dfs_find_son(int u, int fa, int dep){
P[u].fa = fa;
P[u].deep = dep;
P[u].siz = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa) {
dfs_find_son(v, u, dep + );
P[u].siz += P[v].siz;
if(P[v].siz > P[P[u].son].siz) P[u].son = v;
}
}
} void dfs_to_un(int u, int tp){
P[u].toop = tp;
P[u].tree = ++ tim;
bef[tim] = u;
if(!P[u].son) return ;
dfs_to_un(P[u].son, tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != P[u].son && v != P[u].fa) dfs_to_un(v, v);
}
} void down(int jd){
int F = T[jd].f;
T[lson].w += (T[lson].r - T[lson].l + ) * F;
T[rson].w += (T[rson].r - T[rson].l + ) * F;
T[lson].f += F;
T[rson].f += F;
T[jd].f = ;
} void Sec_G(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
int yj = (r - l) + ;
T[jd].w += yj;
T[jd].f ++;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y);
if(y > mid) Sec_G(mid + , r, rson, x, y);
important;
} inline void Sec_G_imp(int x, int y){
int tp1 = P[x].toop, tp2 = P[y].toop;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , P[tp1].tree, P[x].tree);
x = P[tp1].fa;
tp1 = P[x].toop;
}
if(P[x].deep < P[y].deep) Sec_G(, n, , P[x].tree, P[y].tree);
else Sec_G(, n, , P[y].tree, P[x].tree);
return ;
} void Ask_ans(int l, int r, int jd){
if(l == r) {
data[bef[l]] = T[jd].w;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
Ask_ans(l, mid, lson);
Ask_ans(mid + , r, rson);
} void debug(){
for(int i = ; i <= n; i ++) cout << data[i] << endl;
exit();
} void build_tree(int l, int r, int jd){
T[jd].l = l; T[jd].r = r;
if(l == r) return ;
int mid = (l + r) >> ;
build_tree(l, mid, lson);
build_tree(mid + , r, rson);
} int main()
{
n = read(); Ti = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
dfs_find_son(, , );
dfs_to_un(, );
build_tree(, n, );
while(Ti --){
int x = read(), y = read();
Sec_G_imp(x, y);
}
Ask_ans(, n, );
//debug();
for(int i = ; i <= n; i ++){
int answer = -, whoo = ;
for(int j = head[i]; ~ j; j = G[j].nxt){
int v = G[j].v;
if(v == P[v].fa || P[v].tree < P[i].tree) continue ;
if(data[v] > answer){
answer = data[v];
whoo = v;
}
}
printf("%d\n", whoo);
}
return ;
}
/*
14 7
1 4
4 10
4 9
4 8
9 13
13 14
3 1
7 3
2 1
2 6
6 12
11 6
5 2
11 3
7 8
2 8
11 1
8 14
5 7
9 14 */
[other] Div的更多相关文章
- IE6/7下空div占用空间的问题
最近注意力没在前端上面,工作碰到这样一个问题,下意识的写了句 font-size:0;line-height:0;哪知道引发了更大的bug.后来插入数据进去的时候都不显示了..再后来百度一番找到,原来 ...
- div实现自适应高度的textarea,实现angular双向绑定
相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...
- 冒泡,setinterval,背景图的div绑定事件,匿名函数问题
1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...
- css居中div的几种常用方法
在开发过程中,很多需求需要我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居中方式,接下来就分享下一下几种常用的居中方式. 1.text ...
- Div Vertical Menu ver5
这个小功能,如果是算此次,已经是第5次修改了.可以从这里看到前4次:V1, http://www.cnblogs.com/insus/archive/2011/10/17/2215637.html V ...
- 计算Div标签内Checkbox个数或已被disabled的个数
先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...
- [jquery]显示隐藏div标签的几种方法
1.$("#demo").attr("style","display:none;");//隐藏div $("#demo" ...
- CSS3变形记(上):千变万化的Div
传统上,css就是用来对网页进行布局和渲染网页样式的.然而,css3的出现彻底打破了这一格局.了解过css3的人都知道,css3不但可以对网页进行布局和渲染样式,还可以绘制一些图形.对元素进行2D和3 ...
- 如何隐藏DIV对象
DIV对象在网页里面,相当于一个容器,在其内部,可以显示文字.图片.视频控件等等. 以下的教程,和大家一起来学习,如何隐藏DIV对象. 这必须使用CSS来控制,才能达到隐藏的目的,那么,就得使用CSS ...
- css实现div,文字水平居中的方案。
本文的目的为记录个人开发中常用的几种居中方案,以供大家参考. //basic css html, body { height: 100%; width: 100%; margin: 0; paddin ...
随机推荐
- Java基础IO类之数据流
DataInputStream: 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本java数据类型.应用程序可以使用数据输出流 写入稍后由数据输入流读取的数据.DataInputStrea ...
- Spring的事件监听ApplicationListener
ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...
- Linux磁盘管理系列 — 磁盘配额管理
一.磁盘管理的概念 Linux系统是多用户任务操作系统,在使用系统时,会出现多用户共同使用一个磁盘的情况,如果其中少数几个用户占用了大量的磁盘空间,势必压缩其他用户的磁盘的空间和使用权限.因此,系统管 ...
- BZOJ4400 TJOI2012桥(最短路+线段树)
首先找出任意一条1-n的最短路径.显然删除的边只有在该最短路上才会对最短路长度产生影响. 不会证明地给出一个找不到反例的结论:删除一条边后,新图中一定有一条1-n的最短路径上存在一条边x->y, ...
- vue npm run build 失败
之前删除过 node-moudel 文件夹,然后 npm install 重新安装,一切OK.打包的时候,报错,找不到caniuse什么的.再删除node-moudel,重新cnpm install ...
- Python之原始数据-1
一.数据对于模型来说是基础,是数据成就了模型,而现在的又是一个数据时代,比如:淘宝等.通过对用户数据的分析挖掘,预测用户的消费习惯等,再比如:人工智能.通过提取摄像头的图片帧数,通过分析图片,得出具体 ...
- 快速提交一个项目到github或gitee上
以下步骤假设你已经安装好git 一.Git 全局设置: git config --global user.name "用户名" git config --global user.e ...
- echarts使用结合时间轴timeline动态刷新案例
1.echarts简介 ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Fire ...
- Apache日志轮询Cronolog安装及简单用法
安装日志轮询工具cronolog: [root@bqh- tools]# tar xf cronolog-.tar.gz [root@bqh- tools]# cd cronolog- [root@b ...
- Linux学习笔记之二
vim编辑器 :三种工作模式 vim /tmp/xueying.txt 命令模式 a.i.o/esc \ :wq 保存并退出 / \ 输入模式 ...