spoj freetour II
昨天吐槽还没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的更多相关文章
- hdu5977 Garden of Eden
都不好意思写题解了 跑了4000多ms 纪念下自己A的第二题 (我还有一道freetour II wa20多发没A...呜呜呜 #include<bits/stdc++.h> using ...
- 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 ...
- 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 ...
- 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 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 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 ...
- 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 ...
随机推荐
- 浅析Numpy.genfromtxt及File I/O讲解
Python 并没有提供数组功能,虽然列表 (list) 可以完成基本的数组功能,但它并不是真正的数组,而且在数据量较大时,使用列表的速度就会慢的让人难受.为此,Numpy 提供了真正的数组功能,以及 ...
- mysql DML DDL DCL
DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言 D ...
- Word Press使用
邮件发送功能插件:Easy WP SMTP LNMP一键包网站环境WordPress程序无法后台切换安装主题 https://help.aliyun.com/document_detail/44619 ...
- Hive入门教程
Hive 安装 相比起很多教程先介绍概念,我喜欢先动手装上,然后用例子来介绍概念.我们先来安装一下Hive 先确认是否已经安装了对应的yum源,如果没有照这个教程里面写的安装cdh的yum源http: ...
- 基于Parallax设计HTML视差效果
年关将至,给大家拜年. 最近时间充裕了一点,给大家介绍一个比较有意思的控件:Parallax.它可以用来实现鼠标移动时,页面上的元素也做偏移的视差效果.在一些有表现层次,布局空旷的页面上,用来做Hea ...
- CentOS 挂载 cdrom, iso文件作为源
在生产系统环境中的机器都没有连接互联网,因此都是使用本地源. 首先,需要将cdrom, 或 iso文件挂载到本地目录. 1.挂载光驱: 将cdrom 放入光驱. $ mkdir /media/cdr ...
- JS原生Ajax&Jquery的Ajax技术&Json
1.介绍Ajax Ajax = 异步 JavaScript 和 XML Ajax是一种创建快速动态网页的技术 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新.这意味着可以不用整个 ...
- python多版本以及各种包管理
python多版本以及各种包管理 python 包管理 各个版本 python版本管理 由于Python有2.x和3.x两个大的版本,而且每一个工程建立所用的各种包的版本也不尽相同(如flask1.x ...
- 机器学习之支持向量机(二):SMO算法
注:关于支持向量机系列文章是借鉴大神的神作,加以自己的理解写成的:若对原作者有损请告知,我会及时处理.转载请标明来源. 序: 我在支持向量机系列中主要讲支持向量机的公式推导,第一部分讲到推出拉格朗日对 ...
- 用原生js实现一个new方法
首先写一个父类方法(包含参数name,age): function Person(name,age){ this.name = name; this.age = age; } new一个Person的 ...