号称是noip2016最恶心的题

基本上用了一天来搞明白+给sy讲明白(可能还没讲明白

具体思路是真的不想写了(快吐了

如果要看,参见洛谷P1600 天天爱跑步——题解

虽然这样不好但我真的不想写了

直接放代码:

#include<bits/stdc++.h>
#include<vector> using namespace std; inline int read() {
int ans=0;
char last=' ',ch=getchar();
while(ch>'9'||ch<'0') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
if(last=='-') ans=-ans;
return ans;
}
const int mxn=300000; int n,m;
vector<int> lcau[mxn],len[mxn];
struct nd{
int i,v;
};
vector<nd> lcav[mxn];
int dis[mxn];
struct node {
int to,nxt;
}e[mxn<<1];
int w[mxn];
int ans[mxn];
int ecnt,head[mxn];
void add(int from,int to) {
++ecnt;
e[ecnt].to=to;
e[ecnt].nxt=head[from];
head[from]=ecnt;
} int dep[mxn],fa[mxn],siz[mxn],son[mxn],top[mxn]; void dfs1(int u,int f) {
dep[u]=dep[f]+1;
fa[u]=f;
siz[u]=1;
int maxn=-1;
for(int i=head[u],v;i;i=e[i].nxt) {
v=e[i].to;
if(v==f) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(maxn<siz[v]) {
maxn=siz[v];
son[u]=v;
}
}
} void dfs2(int u,int f) {
if(son[f]==u) top[u]=top[f];
else top[u]=u;
if(son[u]) dfs2(son[u],u);
for(int i=head[u],v;i;i=e[i].nxt) {
v=e[i].to;
if(v==f||v==son[u]) continue;
dfs2(v,u);
}
} int lca(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]>dep[top[y]]) x=fa[top[x]];
else y=fa[top[y]];
}
if(x==y) return x;
if(dep[x]>dep[y]) swap(x,y);
return x;
} int t1[mxn],t2[mxn<<1];
int st[mxn]; void dfs(int u) {
int bf=t1[dep[u]+w[u]]+t2[w[u]-dep[u]+mxn];
for(int i=head[u],v;i;i=e[i].nxt) {
v=e[i].to;
if(v==fa[u]) continue;
dfs(v);
}
if(st[u])
t1[dep[u]]+=st[u];
if(len[u].size())
for(int i=0;i<len[u].size();i++) {
int Dis=len[u][i];
t2[Dis-dep[u]+mxn]++;
}
ans[u]=t1[dep[u]+w[u]]+t2[w[u]-dep[u]+mxn]-bf;
if(lcau[u].size()) {
for(int i=0;i<lcau[u].size();i++) {
int start=lcau[u][i];
int enddd=lcav[u][i].v;
int num=lcav[u][i].i;
if(dep[u]+w[u]==dep[start]&&dis[num]-dep[enddd]+dep[u]==w[u])
ans[u]--;
t1[dep[start]]--;
t2[dis[num]-dep[enddd]+mxn]--;
}
}
} int main() {
n=read();
m=read();
for(int i=1,u,v;i<n;i++) {
u=read();
v=read();
add(u,v);
add(v,u);
}
for(int i=1;i<=n;i++)
w[i]=read();
int l;
dfs1(1,0);
dfs2(1,0);
for(int i=1,S,T;i<=m;i++) {
S=read();
T=read();
st[S]++;
int f=lca(S,T);
lcau[f].push_back(S);
lcav[f].push_back(nd{i,T});
dis[i]=dep[S]+dep[T]-(dep[f]<<1);
len[T].push_back(dis[i]);
}
dfs(1);
for(int i=1;i<=n;i++)
printf("%d ",ans[i]);
return 0;
}

Luogu P1600[NOIP2016]day1 T2天天爱跑步的更多相关文章

  1. [NOIP2016 DAY1 T2]天天爱跑步-[差分+线段树合并][解题报告]

    [NOIP2016 DAY1 T2]天天爱跑步 题面: B[NOIP2016 DAY1]天天爱跑步 时间限制 : - MS 空间限制 : 565536 KB 评测说明 : 2s Description ...

  2. NOIP2016 DAY1 T2天天爱跑步

    传送门 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 ...

  3. NOIP2016 Day1 T2 天天爱跑步(树上差分,LCA)

    原文链接 原题链接 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏 ...

  4. 【NOIP2016】DAY1 T2 天天爱跑步

    [NOIP2016]DAY1 T2 天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要玩家每天按时 ...

  5. 【NOIP 2016】Day1 T2 天天爱跑步

    Problem Description 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...

  6. P1600 [NOIP2016 提高组] 天天爱跑步 (树上差分)

    对于一条路径,s-t,位于该路径上的观察员能观察到运动员当且仅当以下两种情况成立:(d[ ]表示节点深度) 1.观察员x在s-lca(s,t)上时,满足d[s]=d[x]+w[x]就能观察到,所以我们 ...

  7. 【NOIP2016 Day1 T2】天天爱跑步

    题目传送门:https://www.luogu.org/problemnew/show/P1600 感觉这两天在处理边界问题上有点神志不清......为了从80的暴力变成100,花了整整一个下午+一个 ...

  8. [NOIp2016提高组]天天爱跑步

    题目大意: 有一棵n个点的树,每个点上有一个摄像头会在第w[i]秒拍照. 有m个人再树上跑,第i个人沿着s[i]到t[i]的路径跑,每秒钟跑一条边. 跑到t[i]的下一秒,人就会消失. 问每个摄像头会 ...

  9. [luogu]P1600 天天爱跑步[LCA]

    [luogu]P1600 [NOIP 2016]天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上 ...

随机推荐

  1. 记使用vim常用的快捷键

    显示行数  :set nu 跳转到最后一行 G 跳转到指定行数 nG  n为行数 跳转到行首 0 跳转到行尾 $ (shift+4)

  2. bootstrap单选框复选框的使用

    <form role="form"> <div class="form-group"> <label class="ch ...

  3. codeforces402B

    Trees in a Row CodeForces - 402B The Queen of England has n trees growing in a row in her garden. At ...

  4. DOM对象属性

    事件  onmouseover 鼠标以上事件  onmouseout    鼠标离开事件  onclock 鼠标点击事件  onfocus 获取焦点 onblur 失去焦点 oninput  输入事件 ...

  5. $\LaTeX$数学公式大全6

    $6\ Binary\ Operation/Relation\ Symbols$$\ast$ \ast$\star$ \star$\cdot$ \cdot$\circ$ \circ$\bullet$ ...

  6. Leetcode题目21.合并两个有序链表(简单)

    题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1->1-& ...

  7. 学习UEFI 之你把C语言学好了码?学习UEFI 之你把C语言学好了吗?

    很多人在问我说: 怎样子把UEFI 学好?! 其实写BIOS 的人答案应该只有一个,把SPCE看懂看完然后融会贯通!这样子的答案好像跟没有是一样的! 小弟就以我的学习经验来分享给大家吧!(虽然我也没学 ...

  8. TCP之Nagle算法与TCP_NODELAY

    1. Nagle 算法 在一个 Rlogin 连接上客户一般每次发送一个字节到服务器,这就产生了一些 41 字节长的分组:20 字节的 IP 首部.20 字节的 TCP 首部和 1 个字节的数据.在局 ...

  9. pycharm2019连接mysql错误:08801 ------Connection to django1@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

  10. wait/notify模拟连接池

    连接池中的连接可重复使用,减少每次新建和烧毁连接对资源的消耗,但连接池的容量大小也要设置合理,否则也会占用多余的资源.连接池的基本功能是获取连接和释放连接 连接在java中也是一个类,连接对象是一个普 ...