[AHOI2005]航线规划

Posted mrclr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AHOI2005]航线规划相关的知识,希望对你有一定的参考价值。

嘟嘟嘟


好久不写树剖,细节有点小问题。


这题比较好想。看到删边,一般就能想到离线加边。
然后考虑如果一条边是关键边,那么他一定是一个桥。因此首先要做的是边双缩点。
缩完点后图就变成了树。至于加边,显然就是把这条边所在环上的点缩成了一个点。但如果再暴力缩点的话会超时。
实际上相当于把树上在环中的边的边权改成了0.然后询问的时候就是树上两点间距离了。
于是上树剖。


细节就是树剖的时候不要改lca。这点我注意到了,关键是每一次我都没有改链的顶端结点……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
#include<map>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e4 + 5;
const int maxm = 1e5 + 5;
In ll read()

  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;

In void write(ll x)

  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');

In void MYFILE()

#ifndef mrclr
  freopen("ha.in", "r", stdin);
  freopen("ha.out", "w", stdout);
#endif


int n, m, qcnt = 0;
struct edges int x, y;E[maxm];
struct Node

  bool flg; int x, y;
q[maxm];
map<int, int> mp[maxn];
int ans[maxm], acnt = 0;

struct Edge

  int nxt, to;
e[maxm << 1];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y)

  e[++ecnt] = (Edge)head[x], y;
  head[x] = ecnt;


bool in[maxn];
int st[maxn], Top = 0;
int dfn[maxn], low[maxn], cnt = 0;
int col[maxn], ccol = 0;
In void tarjan(int now, int _e)

  st[++Top] = now; in[now] = 1;
  dfn[now] = low[now] = ++cnt;
  for(int i = head[now], v; ~i; i = e[i].nxt)
    
      if(!dfn[v = e[i].to])
        
          tarjan(v, i);
          low[now] = min(low[now], low[v]);
        
      else if((i ^ 1) ^ _e) low[now] = min(low[now], dfn[v]);
    
  if(dfn[now] == low[now])
    
      int x; ++ccol;
      do
        
          x = st[Top--], in[x] = 0;
          col[x] = ccol;
        while(x ^ now);
    


Edge e2[maxm << 1];
int head2[maxn], ecnt2 = -1;
In void addEdge2(int x, int y)

  e2[++ecnt2] = (Edge)head2[x], y;
  head2[x] = ecnt2;

In void buildGraph(int now)

  int u = col[now];
  for(int i = head[now], v; ~i; i = e[i].nxt)
    if((v = col[e[i].to]) ^ u) addEdge2(u, v);


int a[maxn], siz[maxn], dep[maxn], fa[maxn], son[maxn];
In void dfs1(int now, int _f)

  siz[now] = 1;
  for(int i = head2[now], v; ~i; i = e2[i].nxt)
    
      if((v = e2[i].to) == _f) continue;
      a[v] = 1;
      fa[v] = now, dep[v] = dep[now] + 1;
      dfs1(v, now);
      siz[now] += siz[v];
      if(!son[now] || siz[son[now]] < siz[v]) son[now] = v;
    

int top[maxn], dfsx[maxn], pos[maxn];
In void dfs2(int now, int _f)

  dfsx[now] = ++cnt, pos[cnt] = now;
  if(son[now]) top[son[now]] = top[now], dfs2(son[now], now);
  for(int i = head2[now], v; ~i; i = e2[i].nxt)
    
      if((v = e2[i].to) == _f || v == son[now]) continue;
      top[v] = v, dfs2(v, now);
    


int l[maxn << 2], r[maxn << 2], sum[maxn << 2], lzy[maxn << 2];
In void build(int L, int R, int now)

  l[now] = L, r[now] = R;
  lzy[now] = -1;
  if(L == R) sum[now] = a[pos[L]]; return;
  int mid = (L + R) >> 1;
  build(L, mid, now << 1);
  build(mid + 1, R, now << 1 | 1);
  sum[now] = sum[now << 1] + sum[now << 1 | 1];

In void change(int now)

  sum[now] = lzy[now] = 0;

In void pushdown(int now)

  if(~lzy[now])
    
      change(now << 1), change(now << 1 | 1);
      lzy[now] = -1;
    

In void update(int L, int R, int now)

  if(L > R) return;
  if(l[now] == L && r[now] == R) change(now); return;
  pushdown(now);
  int mid = (l[now] + r[now]) >> 1;
  if(R <= mid) update(L, R, now << 1);
  else if(L > mid) update(L, R, now << 1 | 1);
  else update(L, mid, now << 1), update(mid + 1, R, now << 1 | 1);
  sum[now] = sum[now << 1] + sum[now << 1 | 1];

In int query(int L, int R, int now)

  if(L > R) return 0;
  if(l[now] == L && r[now] == R) return sum[now];
  pushdown(now);
  int mid = (l[now] + r[now]) >> 1;
  if(R <= mid) return query(L, R, now << 1);
  else if(L > mid) return query(L, R, now << 1 | 1);
  else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);


In void update_path(int x, int y)

    if(x == y) return;
  while(top[x] ^ top[y])
    
      if(dep[top[x]] < dep[top[y]]) swap(x, y);
      update(dfsx[top[x]], dfsx[x], 1);
      x = fa[top[x]];
    
  if(dep[x] < dep[y]) swap(x, y);
  update(dfsx[y] + 1, dfsx[x], 1);

In int query_path(int x, int y)

    if(x == y) return 0;
  int ret = 0;
  while(top[x] ^ top[y])
    
      if(dep[top[x]] < dep[top[y]]) swap(x, y);
      ret += query(dfsx[top[x]], dfsx[x], 1);
      x = fa[top[x]];
    
  if(dep[x] < dep[y]) swap(x, y);
  ret += query(dfsx[y] + 1, dfsx[x], 1);
  return ret;


int main()

  MYFILE();
  Mem(head, -1), Mem(head2, -1);
  n = read(), m = read();
  for(int i = 1; i <= m; ++i)
    
      int x = read(), y = read();
      if(x > y) swap(x, y);
      E[i] = (edges)x, y;
      ++mp[x][y];
    
  int c;
  while(scanf("%d", &c) && ~c)
    
      int x = read(), y = read();
      if(x > y) swap(x, y);
      q[++qcnt] = (Node)c, x, y;
      if(!c && mp[x][y]) --mp[x][y];
    
  for(int i = 1; i <= m; ++i)
    
      int x = E[i].x, y = E[i].y;
      if(mp[x][y]) addEdge(x, y), addEdge(y, x);
    
  for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i, 0);
  for(int i = 1; i <= n; ++i) buildGraph(i);
  dfs1(1, 0), cnt = 0, top[1] = 1, dfs2(1, 0);
  build(1, cnt, 1);
  for(int i = qcnt; i; --i)
    
      int x = q[i].x, y = q[i].y;
      if(q[i].flg) ans[++acnt] = query_path(col[x], col[y]);
      else update_path(col[x], col[y]);
    
  for(int i = acnt; i; --i) write(ans[i]), enter;
  return 0;

以上是关于[AHOI2005]航线规划的主要内容,如果未能解决你的问题,请参考以下文章

[AHOI2005]航线规划

解题:AHOI 2005 航线规划

刷题BZOJ 1969 [Ahoi2005]LANE 航线规划

BZOJ1969[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树

BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)

[AHOI2005]航线规划