由于做的时候看的是中文题面,第一遍写就被卡题意了:还以为每一条都要过x,那么就是一道动态树根选择2y个叶子的奇怪题目

交完0分gg,才发现题目看错了╮(╯▽╰)╭

the node containing the candy is adjacent to at least one rope covered with a web

完全就是两道题啊。。。。。


首先考虑没有x的做法

贪心显然是对的

1.直径一定要取,否则一定可以通过把与直径最接近(以直径一段为根的lca深度最大)的一条路径改为直径来改善答案

2.剩下的一定从大取到小贪心取(在每次取完后更新每个的数据情况下),同理可证明

然后就可以通过“长链剖分”(把重链剖分里的子树大小改为最深的带权深度)解决没有x的问题了

(目测蛮好写的)

然后考虑一定要把x包含进去

然后是不会证但是感觉很对还能A的想法(⊙﹏⊙)b:

1.先把直径和前2(y-1)【因为每条路径都可以跨两条支链,但是选直径还需要一条路径】大的支链拉进答案(形成一棵树)

2.把x加入答案有两种方案:

  把离x最近的一条边切掉一半和x所在链连成一条边

  删掉最短的一条边把x所在链加入答案

——上图中黑色和灰色表示考虑x前做出的答案,红色表示考虑x后加上的边,灰色表示考虑x后去掉的边,左右图分别表示了两种考虑的姿势

两种方案去个比较好的,O(∩_∩)O搞定啦!


实现什么的。。。我是这么写的:

先找直径,拉出来存起来,然后把剩下的(应该是一个大森林)每个节点深度算出来(把直径上的点深度看做0)

瞎搞一波即可,没什么数据结构

 #include <bits/stdc++.h>
using namespace std;
int n,tot,lend,post,lengthd,m,N,p,q,o,x,y,lord;
int fir[],nex[],dis[],to[],len[],d[],sum_d[],rank[];
int fa[],best[],dep[],top[],ans[],sum[];
void add(int p,int q,int o)
{
nex[++N]=fir[p];len[N]=o;to[N]=q;fir[p]=N;
}
void Dfs(int now)
{
for(int i=fir[now];i;i=nex[i])
if(!dis[to[i]])
{
dis[to[i]]=dis[now]+len[i];
Dfs(to[i]);
}
}
void dfs(int now)
{
for(int i=;i<=n;i++)
dis[i]=;
dis[now]=;
Dfs(now);
}
void build(int now,int fat,int deep)
{
fa[now]=fat;best[now]=deep;dep[now]=deep;rank[now]=lord;
for(int i=fir[now];i;i=nex[i])
if(to[i]!=fat)
{
build(to[i],now,deep+len[i]);
best[now]=max(best[now],best[to[i]]);
}
}
void pou(int now,int Top)
{
bool sad=;top[now]=Top;
for(int i=fir[now];i;i=nex[i])
if(to[i]!=fa[now])
if(sad && best[now]==best[to[i]])
sad=,pou(to[i],Top);
else
pou(to[i],to[i]);
if(sad)
ans[++tot]=dep[now]-dep[fa[top[now]]];
}
void find_d()
{
dfs();int start=,bes=,end=;
for(int i=;i<=n;i++)
if(dis[i]>bes) bes=dis[i],start=i;
dfs(start);bes=;
for(int i=;i<=n;i++)
if(dis[i]>bes) bes=dis[i],end=i;
for(d[lend=]=post=end;post!=start;d[++lend]=post,rank[post]=lend)
for(int i=fir[post];i;i=nex[i])
if(dis[to[i]]<dis[post])
{
post=to[i];
break;
}
lengthd=dis[end]-;
for(int i=;i<=lend;i++)
sum_d[i]=dis[d[i]]-;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d%d%d",&p,&q,&o),
add(p,q,o),add(q,p,o);
find_d();
for(int i=;i<=lend;i++)
for(int j=fir[d[i]];j;j=nex[j])
{
if(i> && to[j]==d[i-]) continue;
if(i<lend && to[j]==d[i+]) continue;
lord=i;
build(to[j],d[i],len[j]);
pou(to[j],to[j]);
}
sort(ans+,ans+tot+,greater<int>());
for(int i=;i<=tot;i++)
sum[i]=sum[i-]+ans[i];
int lastans=;
if()
{
for(int i=;i<=lend;i++)
printf("%d ",d[i]);
puts("");
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x== && y==)
int e=;
x=(x+lastans-)%n+;y=(y+lastans-)%n+;
y=y*-;
if(y>=tot)
{
lastans=sum[tot]+lengthd;
printf("%d\n",lastans);
}
else
if(!y)
if(!dep[x])
{
lastans=lengthd;
printf("%d\n",lastans);
}
else
{
lastans=best[x]+max(sum_d[rank[x]],lengthd-sum_d[rank[x]]);
printf("%d\n",lastans);
}
else
if(dep[x] && best[x]-dep[fa[top[x]]]<ans[y])
{
lastans=lengthd+sum[y-];
int enter=x;
while(dep[enter] && best[enter]-dep[fa[top[enter]]]<ans[y])
enter=fa[top[enter]];
if(dep[enter])
lastans+=max(best[x]-dep[enter],best[x]-best[enter]+ans[y]);
else
{
lastans+=best[x];
if(ans[y]>min(sum_d[rank[x]],lengthd-sum_d[rank[x]]))
lastans+=ans[y]-min(sum_d[rank[x]],lengthd-sum_d[rank[x]]);
}
printf("%d\n",lastans);
}
else
{
lastans=sum[y]+lengthd;
printf("%d\n",sum[y]+lengthd);
}
}
return ;
}

写的又臭又长╮(╯▽╰)╭

Codeforces 526G Spiders Evil Plan的更多相关文章

  1. Codeforces 526G - Spiders Evil Plan(长链剖分+直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 %%%%% 这题也太神了吧 storz 57072 %%%%% 首先容易注意到我们选择的这 \(y\) 条路径的端点一定是叶子节点,否则我 ...

  2. 【CF526G】Spiders Evil Plan(贪心)

    [CF526G]Spiders Evil Plan(贪心) 题面 洛谷 CodeForces 给定一棵树,要求选择\(y\)条链,满足被链覆盖的所有点在树上联通,且\(x\)必定在联通块中. 对于每次 ...

  3. CF Contest 526 G. Spiders Evil Plan 长链剖分维护贪心

    LINK:Spiders Evil Plan 非常巧妙的题目. 选出k条边使得这k条边的路径覆盖x且覆盖的边的边权和最大. 类似于桥那道题还是选择2k个点 覆盖x那么以x为根做长链剖分即可. 不过这样 ...

  4. [CF526G]Spiders Evil Plan

    题目大意: 给出一个$n(n\leq 10^5)$个结点的带边权的树,$q(q\leq 10^5)$个询问,每次询问用$y$条路径覆盖整棵树且覆盖$x$至少一次,最多能覆盖的道路长度是多少? 强制在线 ...

  5. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  6. 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  8. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  9. Arpa's loud Owf and Mehrdad's evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

随机推荐

  1. uboot显示logo的时候发现颜色偏黄【学习笔记】

    平台信息:内核:linux3.0.68 系统:android6.0平台:rk3288 将一张图片烧录进logo分区,发现在uboot读取这张图片并显示的时候发现颜色偏黄,解决办法,在烧录bmp图片的时 ...

  2. BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...

  3. python学习笔记:第二天(基本数据类型)

    Python3 基本数据类型 1.标准数据类型 Python3中有六个标准的数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictio ...

  4. BZOJ1566 【NOI2009】管道取珠

    题面 这是一道DP神题,直到我写下这句题解时也没有想明白…… 首先,这道题要我们求所有(不同输出序列的方案数)的平方和,于是我们当然就想到求所有不同输出序列的方案数……(大雾) .这道题一个巧妙的地方 ...

  5. 【转载】Java的接口和抽象类

    尊重作者劳动成果,转载请标明原文链接: http://www.cnblogs.com/dolphin0520/p/3811437.html 也不知道,面试为什么经常用到这个知识点—— 一.抽象类 在了 ...

  6. LC_MESSAGES、语言文件po和mo互相转换

    LANGUAGE, LC_ALL, LC_MESSAGES, LANG zz- -   关于这几个环境变量一般认为它们的作用是用来指定程序用户界面语言而且这几个环境变量的优先级是从左到右依次降低的大概 ...

  7. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  8. Spring 事务管理高级应用难点剖析: 第 1 部分

    Spring 的事务管理是被使用得最多的功能之一,虽然 Spring 事务管理已经帮助程序员将要做的事情减到了最小.但在实际开发中,如果使用不当,依然会造成数据连接泄漏等问题.本系列以实际应用中所碰到 ...

  9. 不能访问tomcat中的项目

    tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomcat,在 ...

  10. hadoop各组件安装(非专业人士,不定期更新)

    压缩包下载http://www.cnblogs.com/bfmq/p/6027202.html 1.zookeepermkdir /usr/local/hadooptar zxf /root/zook ...