[SP1825] Free tour II
Posted lizehon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SP1825] Free tour II相关的知识,希望对你有一定的参考价值。
/* ----------------------- [题解] https://www.luogu.org/blog/IRving1-1/solution-sp1825 ----------------------- O(Nlog^2)做法,vjudge上写的是时限100ms,过2e5数据 ----------------------- 统计tmp[i]为有i个黑点的最长路径,进行转移 合并的顺序很巧妙,也很重要,这里倒序枚举当前子树的j(tmp[j]),则可以做到控制维护之前子树cur(maxn[cur])单调递增 用maxn[i]记录小于等于i个黑点的最长路径,更新答案完了以后用当前的tmp[]更新maxn[] 记得清空tmp[]和maxn[] -----------------------2019.2.12 */ #pragma GCC optimize(2) #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<set> #include<vector> #include<map> #include<queue> #define rep(i,a,n) for(int i = a;i <= n;i++) #define per(i,n,a) for(int i = n;i >= a;i--) #define enter putchar(‘ ‘) #define fr friend inline #define y1 poj #define mp make_pair #define pr pair<int,int> #define fi first #define sc second #define pb push_back #define lowbit(x) x & (-x) #define B printf("Bug "); using namespace std; typedef long long ll; const int M = 200005; const int N = 2000005; const int INF = 1e9; const double eps = 1e-7; int read() { int x = 0,op = 1;char ch = getchar(); while(ch < ‘0‘ || ch > ‘9‘) {if(ch == ‘-‘) op = -1;ch = getchar();} while(ch >= ‘0‘ && ch <= ‘9‘) x = x * 10 + ch - ‘0‘,ch = getchar(); return x * op; } struct edge { int next,to,from,v; }e[M<<1]; int n,k,m,maxn[M],G,size[M],dis[M],dep[M],sum,x,y,z,head[M],ecnt,hson[M],ans,tmp[M],mdep; bool black[M],vis[M]; vector <pr> v; void add(int x,int y,int z) { e[++ecnt].to = y; e[ecnt].next = head[x]; e[ecnt].from = x; e[ecnt].v = z; head[x] = ecnt; } void getG(int x,int fa) { size[x] = 1,hson[x] = 0; for(int i = head[x];i;i = e[i].next) { if(e[i].to == fa || vis[e[i].to]) continue; getG(e[i].to,x); size[x] += size[e[i].to],hson[x] = max(hson[x],size[e[i].to]); } hson[x] = max(hson[x],sum - size[x]); if(hson[x] < hson[G]) G = x; } void getdis(int x,int fa,int d,int depth) { dis[x] = d,dep[x] = depth,mdep = max(mdep,dep[x]); for(int i = head[x];i;i = e[i].next) { if(e[i].to == fa || vis[e[i].to]) continue; getdis(e[i].to,x,d + e[i].v,depth + black[e[i].to]); } } void getmaxn(int x,int fa) { tmp[dep[x]] = max(tmp[dep[x]],dis[x]); for(int i = head[x];i;i = e[i].next) { if(vis[e[i].to] || e[i].to == fa) continue; getmaxn(e[i].to,x); } } void solve(int x) { vis[x] = 1,v.clear(); if(black[x]) k--; for(int i = head[x];i;i = e[i].next) { if(vis[e[i].to]) continue; mdep = 0,getdis(e[i].to,x,e[i].v,black[e[i].to]);//算出最长路径有几个黑点mdep v.pb(mp(mdep,e[i].to)); } sort(v.begin(),v.end()); rep(i,0,(int)(v.size()-1)) { getmaxn(v[i].sc,x);//算出有i个黑点的最长路径长度tmp[i] int cur = 0; if(i != 0) per(j,v[i].fi,0)//启发式合并:[倒序]枚举j并控制cur+j<k,这个思想很巧妙,很重要 O(N) { //一直没看到这个-1 while(cur + j < k && cur < v[i-1].fi) cur++,maxn[cur] = max(maxn[cur],maxn[cur-1]);//小于等于i个黑点的最长路径maxn[i] if(cur + j <= k) ans = max(ans,maxn[cur] + tmp[j]); } if(i != (int)(v.size() - 1)) rep(j,0,v[i].fi) maxn[j] = max(maxn[j],tmp[j]),tmp[j] = 0;//小于等于i个黑点的最长路径maxn[i],并准备转移 else rep(j,0,v[i].fi){if(j <= k) ans = max(ans,max(tmp[j],maxn[j]));tmp[j] = maxn[j] = 0;}//最后一个子树,直接统计 } if(black[x]) k++; for(int i = head[x];i;i = e[i].next) { if(vis[e[i].to]) continue; sum = size[e[i].to],G = 0; getG(e[i].to,x),solve(G); } } int main() { n = read(),k = read(),m = read(); rep(i,1,m) x = read(),black[x] = 1; rep(i,1,n-1) x = read(),y = read(),z = read(),add(x,y,z),add(y,x,z); sum = n,hson[G] = INF,getG(1,0); solve(G); printf("%d ",ans); return 0; }
以上是关于[SP1825] Free tour II的主要内容,如果未能解决你的问题,请参考以下文章
SPOJ1825 FTOUR2 - Free tour II