昨天吐槽还没A,今天就A了
有个变量开成了全局变量,应该携程局部变量

对于中间的solve我也不懂为什么是nlog2n,我不看题解也不会做

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 2000000000;
const int MAXN = 4e5+5; int ans;
int co[MAXN];
int vis[MAXN];
struct Pode{
int to,nx, di;
Pode(int a=0,int b=0,int c=0):to(a), nx(b), di(c){}
}E[MAXN];
int head[MAXN], cot;
void add(int u, int v, int w) {
E[cot] = Pode(v,head[u],w); head[u] = cot++;
}
int N,K,M;
/***************WeightRoot************/
int all, num, center;
int dp[MAXN], nodes[MAXN];
void findRoot(int x,int pre) {
nodes[x] = 1; dp[x] = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
findRoot(y,x);
nodes[x] += nodes[y];
dp[x] = max(dp[x], nodes[y]);
}
dp[x] = max(dp[x], all-nodes[x]);
if(dp[x] < num) {
num = dp[x]; center = x;
}
}
int getRoot(int root,int sn) {
num = INF; all = sn; center = root;
findRoot(root, -1);
return center;
}
/*************treecdq***************/
struct Node{
int dep, v, di;
}so[MAXN];
int cmp(Node a,Node b) {
return a.dep < b.dep;
}
int dep[MAXN]; // max dep (black)
int g[MAXN];
int mg[MAXN];
void getdep(int x,int pre) {
dep[x] = co[x]; int res = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
getdep(y,x);
res = max(res, dep[y]);
}
dep[x] += res;
}
void getg(int x,int pre,int d,int c) {
g[c] = max(g[c], d);
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
getg(y,x,d+E[i].di, c+co[y]);
}
}
void work(int x) {
vis[x] = 1; int tot = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(vis[y]) continue;
work(getRoot(y,nodes[y]));
}
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(vis[y]) continue;
getdep(y, x);
so[++tot].dep = dep[y]; so[tot].v = y; so[tot].di = E[i].di;
}
sort(so+1,so+tot+1,cmp);
// printf("%d:",x); for(int i = 1; i <= tot; ++i) printf("%d ",so[i].dep); printf("\n");
for(int i = 0; i <= so[tot].dep; ++i) mg[i] = -INF;
for(int i = 1; i <= tot; ++i) {
int t1 = so[i].dep; int t2 = so[i].v; int t3 = so[i].di;
for(int j = 0; j <= t1; ++j) g[j] = -INF;
getg(t2,x,t3,co[t2]);
if(i != 1) {
for(int j = 0; j <= K-co[x] && j <= t1; ++j) {
int tt = min(so[i-1].dep, K-co[x]-j);
if(mg[tt] == -INF) break;
if(g[j] != -INF) ans = max(ans, mg[tt]+g[j]);
}
}
for(int j = 0; j <= t1; ++j) {
mg[j] = max(g[j], mg[j]);
if(j) mg[j] = max(mg[j], mg[j-1]);
if(j+co[x] <= K) ans=max(ans, mg[j]);
}
}
vis[x] = 0;
} int main(){
while(~scanf("%d %d %d",&N,&K,&M)) {
ans = 0;
memset(head,-1,sizeof(head)); cot = 0;
memset(co,0,sizeof(co));
memset(vis,0,sizeof(vis));
for(int i = 1; i <= M; ++i) {
int a; scanf("%d",&a);
co[a] ++;
}
for(int i = 1; i < N; ++i) {
int a,b,c; scanf("%d %d %d",&a,&b,&c);
add(a,b,c); add(b,a,c);
}
work(getRoot(1,N));
printf("%d\n",ans);
}
return 0;
}

spoj freetour II的更多相关文章

  1. hdu5977 Garden of Eden

    都不好意思写题解了 跑了4000多ms 纪念下自己A的第二题 (我还有一道freetour II wa20多发没A...呜呜呜 #include<bits/stdc++.h> using ...

  2. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  3. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  4. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  5. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  6. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  7. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  8. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  9. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

随机推荐

  1. elk5.4小白踩坑记录

    搭建请参考:http://www.ywnds.com/?p=9776 搭建步骤:http://www.ywnds.com/?p=9776 初步解方案:先不用管这个提示,当你创建完索引后,接受数据后.一 ...

  2. 用js脚本一键下载网页所有图片

    年前这两天稍微闲一点了,琢磨了一点js脚本,功能是把当前网页页面上的所有图片一次性保存到本地,免得每次都要对图片右键保存. 测试环境:Chrome开发者模式下(启动Chrome,按F12即可) 测试网 ...

  3. 【ASP.NET Core】处理异常(上篇)

    依照老周的良好作风,开始之前先说点题外话. 前面的博文中,老周介绍过自定义 MVC 视图的搜索路径,即向 ViewLocationFormats 列表添加相应的内容,其实,对 Razor Page 模 ...

  4. [Python Study Notes]批量将wold转换为pdf

    本文代码,由原ppt2pdf.py进行改写 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  5. LocalMaxima_NOI导刊2009提高(1)

    先打表,发现\(ans=\sum_{i=1}^n\frac{1}{i}\) 对于小数据可以直接打表 数据很大时,精度相对就比较宽松 欧拉-马斯刻若尼常数=调和级数-自然对数 调和级数为:\(\sum_ ...

  6. 阿里云pai项目使用说明

    PAI项目创建方法 购买region 进入MaxCompute,购买相应region,目前机器学习只支持华东2(GPU公测免费)以及华北2(GPU计划收费),注意选择"按量后付费" ...

  7. 使用Nginx实现灰度发布

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

  8. 用batch调用DB2 CLPPlus执行多个SQL文

    不啰嗦直接上技能. 大概三部分组成: 1.bat文件.(run.bat) 2.辅助SQL文.(AllRun.sql) 3.要执行的SQL文.(S1.sql,S2.sql,S3.sql) +++++++ ...

  9. axios + mock.js模拟数据实现前后端分离开发的实例代码

    首先就是必须安装axios和mock.js npm install axios npm install mockjs 1. 然后在文档src中新建一个mock.js文件,如图 2. 在main.js中 ...

  10. HDU - 2160 递推

    思路:dp(i)表示第i天的猪的数量,g(i)表示第i天新出生的猪的数量,d(i) = d(i-1) * 2 - g(i-2), g(i) = d(i-1) AC代码 #include <cst ...