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的更多相关文章

  1. IE6/7下空div占用空间的问题

    最近注意力没在前端上面,工作碰到这样一个问题,下意识的写了句 font-size:0;line-height:0;哪知道引发了更大的bug.后来插入数据进去的时候都不显示了..再后来百度一番找到,原来 ...

  2. div实现自适应高度的textarea,实现angular双向绑定

    相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...

  3. 冒泡,setinterval,背景图的div绑定事件,匿名函数问题

    1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...

  4. css居中div的几种常用方法

    在开发过程中,很多需求需要我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居中方式,接下来就分享下一下几种常用的居中方式. 1.text ...

  5. Div Vertical Menu ver5

    这个小功能,如果是算此次,已经是第5次修改了.可以从这里看到前4次:V1, http://www.cnblogs.com/insus/archive/2011/10/17/2215637.html V ...

  6. 计算Div标签内Checkbox个数或已被disabled的个数

    先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...

  7. [jquery]显示隐藏div标签的几种方法

    1.$("#demo").attr("style","display:none;");//隐藏div $("#demo" ...

  8. CSS3变形记(上):千变万化的Div

    传统上,css就是用来对网页进行布局和渲染网页样式的.然而,css3的出现彻底打破了这一格局.了解过css3的人都知道,css3不但可以对网页进行布局和渲染样式,还可以绘制一些图形.对元素进行2D和3 ...

  9. 如何隐藏DIV对象

    DIV对象在网页里面,相当于一个容器,在其内部,可以显示文字.图片.视频控件等等. 以下的教程,和大家一起来学习,如何隐藏DIV对象. 这必须使用CSS来控制,才能达到隐藏的目的,那么,就得使用CSS ...

  10. css实现div,文字水平居中的方案。

    本文的目的为记录个人开发中常用的几种居中方案,以供大家参考. //basic css html, body { height: 100%; width: 100%; margin: 0; paddin ...

随机推荐

  1. Golang的安装与环境配置(包括Go lint、Go imports、Go fmt)

    Golang安装 下载地址:https://studygolang.com/dl Go语言中文网 下载后安装,win10系统中会自动配置大部分设置,linux系统请参照网上教程 GO环境变量配置: $ ...

  2. ALV报表——基础(一)

    目录 一.ALV的简介 二.程序实现 2.1.实现步骤 2.2.代码示例 三.Layout.Fieldcat相关属性,ALV输出函数 3.1.Layout相关属性 3.2.Fieldcat相关属性 3 ...

  3. Scratch编程:绘制七色花(七)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 绘制一朵美丽的七色花. 02 — 设计思路 使用画笔功能 ...

  4. Manjaro 使用基础

    一.pacman/yay 的基础命令 Manjaro 装好后,需要运行的第一条命令: sudo pacman -Syy ## 强制更新包数据 sudo pacman-mirrors --interac ...

  5. AdventureWorks 安装和配置[转自 微软msdn]

    AdventureWorks 安装和配置 2018/06/19 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 AdventureWorks 下载链接 ...

  6. LunHui 的生命观

    LunHui 的生命观 来源 https://www.zhihu.com/question/346510295 作者:齐天大圣链接:https://www.zhihu.com/question/346 ...

  7. 基于re模块的计算器

    最终计算器需求: 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - ...

  8. 关于使用sublime的一些报错异常退出的解决方法

    1.我用Sublime Text 3,装了一些插件,现在一打开软件就提示plugin_host has exited unexpectedly,如下图 解决方案 :

  9. vue进入页面时不在顶部,检测滚动返回顶部按钮

    这里是本小白使用时遇到的问题及个人使用的方法可能并不完美. 1.监测浏览器滚动条滚动事件及滚动距离 dmounted() { window.addEventListener("scroll& ...

  10. idea+maven使用

    maven 1. 打开软件,点击configure-project default-settings.配置maven目录. 后续还需要配置一个地方是本地仓库的优先,在runner-VM Options ...