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 ...
随机推荐
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- [Sdoi2017]序列计数 [矩阵快速幂]
[Sdoi2017]序列计数 题意:长为\(n \le 10^9\)由不超过\(m \le 2 \cdot 10^7\)的正整数构成的和为\(t\le 100\)的倍数且至少有一个质数的序列个数 总- ...
- BZOJ 3781: 小B的询问 [莫队]
求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏 [Nim游戏 SG函数]
小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作,他想问你他是否有必胜策略,如 ...
- Nginx设置身份验证
在某些情况下,需要对某些内容的访问进行限制,在Nginx中也提供了这样的限制措施,以下是几种常见的限制措施: 1.访问身份验证 在Nginx的插件模块中有一个模块ngx_http_auth_basic ...
- Ansible进阶--playbook的使用
一.什么是playbooksplaybooks是ansible的脚本.如同shell脚本一样,它是控制远程主机的一系列命令的集合,通过YAML语言编写.执行一些简单的任务,我们可以使用ad-hoc命令 ...
- WinSock IOCP 模型总结(附一个带缓存池的IOCP类)
前言 本文配套代码:https://github.com/TTGuoying/IOCPServer 由于篇幅原因,本文假设你已经熟悉了利用Socket进行TCP/IP编程的基本原理,并且也熟练的掌握了 ...
- Asp.Net Core 2.0 之旅---在Ubuntu上部署WEB应用程序
1.Ubuntu 上 安装NET Core 2.0 SDK 第一步的安装,微软大佬已经写的非常详细了=>直达链接,按照教程来即可. 2.将我们的WEB 发布到一个文件夹,将这个文件夹打包成 压缩 ...
- python中常见的三种句型if,while,for
1.if语句: 特别说明:条件后面的冒号不能少,同样必须是英文字符. 特别特别说明:if内部的语句需要有一个统一的缩进,一般用4个空格.python用这种方法替代了其他很多编程语言中的{}. num= ...
- Leetcode刷题C#版之 Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...