BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)相关的知识,希望对你有一定的参考价值。

题意:中文题。

析:首先二分是很容易想出来的,然后主要是判断这个解合不合法,先二分答案 mid,因为有 m 个计划,所以只要添加虫洞的肯定是所有的时间长于 mid 的计划 中,也就是是那些的共同边,这个就可以用树上差分来做了,假设 s 到 t,那么让in[s]++,in[t]++,in[lca(s, t)] -= 2,其中in 表示的是 该结点与其父结点的边的计数,最后再跑一次dfs,把所有的权值都累加上去,这样就能知道哪些是共同的边了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 3e5 + 20;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Edge{
  int to, val, next;
};
Edge edge[maxn<<1];
int head[maxn], cnt;

inline void addEdge(int u, int v, int val){
  edge[cnt].to = v;
  edge[cnt].val = val;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

int p[21][maxn];
int dep[maxn], dp[maxn], dist[maxn];
struct Road{
  int u, v, lca, dist;
};
Road road[maxn];

inline void dfs(int u, int fa, int d){
  p[0][u] = fa;
  dep[u] = d;
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == fa)  continue;
    dp[v] = dp[u] + edge[i].val;
    dist[v] = edge[i].val;
    dfs(v, u, d + 1);
  }
}

void init(){
  ms(p, -1);  dfs(1, -1, 0);
  for(int k = 0; k < 20; ++k)
    for(int u = 1; u <= n; ++u)
      if(p[k][u] > 0)  p[k+1][u] = p[k][p[k][u]];
}

inline int LCA(int u, int v){
  if(dep[u] > dep[v])  swap(u, v);
  for(int k = 0; k < 20; ++k)
    if(dep[v] - dep[u] >> k & 1)  v = p[k][v];
  if(u == v)  return u;
  for(int k = 19; k >= 0; --k)
    if(p[k][u] != p[k][v]){
      u = p[k][u];
      v = p[k][v];
    }
  return p[0][u];
}

int in[maxn];

inline void dfs1(int u, int fa){
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == fa)  continue;
    dfs1(v, u);  in[u] += in[v];
  }
}

inline bool judge(int mid){
  int ans = 0, cnt = 0;
  for(int i = 1; i <= n; ++i)  in[i] = 0;
  for(int i = 0; i < m; ++i)  if(road[i].dist > mid){
    ans = max(ans, road[i].dist - mid);
    ++cnt;
    ++in[road[i].u];
    ++in[road[i].v];
    in[road[i].lca] -= 2;
  }
  if(cnt == 0)  return true;
  dfs1(1, -1);
  for(int i = 1; i <= n; ++i)  if(in[i] == cnt && dist[i] >= ans)  return true;
  return false;
}

int main(){
  scanf("%d %d", &n, &m);
  ms(head, -1);  cnt = 0;
  for(int i = 1; i < n; ++i){
    int u, v, c;
    scanf("%d %d %d", &u, &v, &c);
    addEdge(u, v, c);
    addEdge(v, u, c);
  }
  init();
  int l = 0, r = 0;
  for(int i = 0; i < m; ++i){
    scanf("%d %d", &road[i].u, &road[i].v);
    road[i].lca = LCA(road[i].u, road[i].v);
    road[i].dist = dp[road[i].u] + dp[road[i].v] - (dp[road[i].lca]<<1);
    r = max(r, road[i].dist);
  }
  while(l <= r){
    int m = l + r >> 1;
    if(judge(m))  r = m - 1;
    else l = m + 1;
  }
  printf("%d\n", l);
  return 0;
}

  

以上是关于BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 4326 NOIP2015 运输计划

[BZOJ4326][codevs4632][codevs5440][UOJ#150][NOIP2015]运输计划

BZOJ4326: NOIP2015 运输计划

BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)

bzoj4326 运输计划

BZOJ-4326运输计划 树链剖分 + 树上差分 + 二分