昨天吐槽还没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. dubbo调用负载均衡

    dubbo负载均衡的地址:http://dubbo.io/books/dubbo-user-book/demos/loadbalance.html 随机策略: public class RandomL ...

  2. 洛谷 [P1129] [ZJOI2007] 矩阵游戏

    这竟然是一道二分图 乍一看,可能是用搜索做,但是这个数据范围,一定会T. 我们观察发现,无论怎样变换,同一行的一定在同一行,同一列的一定还在同一列.所以说,一行只能配一列.这样,我们的目标就是寻找是否 ...

  3. BZOJ 4195: [Noi2015]程序自动分析 [并查集 离散化 | 种类并查集WA]

    题意: 给出若干相等和不等关系,判断是否可行 woc NOI考这么傻逼的题飞快打了一个种类并查集交上了然后爆零... 发现相等和不等看错了异或一下再叫woc90分 然后发现md$a \neq b, a ...

  4. R语言-选择样本数量

    功效分析:可以帮助在给定置信度的情况下,判断检测到给定效应值时所需的样本量,也可以在给定置信水平的情况下,计算某样本量内可以检测到的给定效应值的概率 1.t检验 案例:使用手机和司机反应时间的实验 l ...

  5. ionic2+Angular 使用HttpInterceptorService拦截器 统一处理数据请求

    sstep1:新建http-Interceptor.ts文件 import { Injectable } from '@angular/core'; import { HttpInterceptorS ...

  6. PHP生成图片验证码、点击切换实例

    http://www.jb51.net/article/51506.htm 现在让我们来看下 PHP 代码 复制代码代码如下: <?php session_start();function ra ...

  7. PHP 批量获取指定目录下的文件列表(递归,穿透所有子目录)

    //调用 $dir = '/Users/xxx/www'; $exceptFolders = array('view','test'); $exceptFiles = array('BaseContr ...

  8. zabbix如何监控进程

    zabbix中item的配置如下: zabbix中trigger的配置如下:

  9. 奥酷HTML5视频直播系统AMS6.0

    今日,北极星通自主研发的流媒体服务系统Aoku Media Server6.0发布了,将正式支持HTML5直播,这使得网页中无需有flash播放插件或者其他插件,可直接观看直播,HTML5直播也会使得 ...

  10. js分页功能实现

    实现一个js的分页并在弹出框中显示 1.分页插件使用:bootstarp-paginator.js,需要先引入bootstarp.js和jquery.js等: !function($){"u ...