昨天吐槽还没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. 洛谷 [P1265] 公路修建

    本题的描述:城市联盟,最短距离.. 使人想到了prim求MST,再一看数据范围:完全图!,那么一定得用prim,因为只有5000个点,所以不加优化的prim就能过. #include <iost ...

  2. UOJ 241. 【UR #16】破坏发射台 [矩阵乘法]

    UOJ 241. [UR #16]破坏发射台 题意:长度为 n 的环,每个点染色,有 m 种颜色,要求相邻相对不能同色,求方案数.(定义两个点相对为去掉这两个点后环能被分成相同大小的两段) 只想到一个 ...

  3. Lua利用cjson读写json

    前言 本文结合本人的实际使用经验和代码示例,介绍如何在Lua中对json进行encode和decode.我这里采用的是Lua CJson库,是一个高性能的JSON解析器和编码器,其性能比纯Lua库要高 ...

  4. 计算机基础之Windows10操作系统安装U盘制作

    1.第一步,下载Windows10--ISO镜像(Windows7类似),下载站点: https://msdn.itellyou.cn/(百度搜索msdn即可),个人认为这是最干净的操作系统镜像站点, ...

  5. 阶段小项目2:显示bin格式图片

    #include<stdlib.h>#include<stdio.h>#include<string.h>#include<error.h>#inclu ...

  6. [Python Study Notes] Python的安装

    Windows: 1.下载安装包: 转到Python官网https://www.python.org/downloads/  ,下载最新版本的Python. 2.安装 安装到自定义的安装路径下. 3. ...

  7. laravel中实现短信发送验证码

    前段时间想实现一个短信验证码的功能,但是卡了很长时间. 首先我用的是阿里云的短信服务业务,其首次接入流程如下: 在阿里云上开通短信服务后需要做的: 1,申请签名  2,申请模板   3,创建Acces ...

  8. Go语言极速入门手册

    Github: https://github.com/coderzh/CodeTips /* gotips_test.go: Golang速学速查速用代码手册 Source: github.com/c ...

  9. CEF小白人系列2-DEV环境配置-Windows10+SDK+VS2015

    以下资料整理来自CEF官方教学.作者做了翻译和整理.并会以一种重事件操作的方式来一点点入门. 3个问题: 在上一篇内容中我们了解了什么是CEF,能干些什么.必要的物理学习环境. 下面我们也是以带着问题 ...

  10. Java经典编程题50道之二十九

    求一个3*3矩阵对角线元素之和. public class Example29 {    public static void main(String[] args) {        int[][] ...