1. 程式人生 > >刷題總結——尋寶遊戲(bzoj3991 dfs序)

刷題總結——尋寶遊戲(bzoj3991 dfs序)

first 最短 更新數據 iostream begin 題解 長度 ras getchar

題目:

Description

B最近正在玩一個尋寶遊戲,這個遊戲的地圖中有N個村莊和N-1條道路,並且任何兩個村莊之間有且僅有一條路徑可達。遊戲開始時,玩家可以任意選擇一個村莊,瞬間轉移到這個村莊,然後可以任意在地圖的道路上行走,若走到某個村莊中有寶物,則視為找到該村莊內的寶物,直到找到所有寶物並返回到最初轉移到的村莊為止。小B希望評測一下這個遊戲的難度,因此他需要知道玩家找到所有寶物需要行走的最短路程。但是這個遊戲中寶物經常變化,有時某個村莊中會突然出現寶物,有時某個村莊內的寶物會突然消失,因此小B需要不斷地更新數據,但是小B太懶了,不願意自己計算,因此他向你求助。為了簡化問題,我們認為最開始時所有村莊內均沒有寶物

Input

第一行,兩個整數N、M,其中M為寶物的變動次數。

接下來的N-1行,每行三個整數x、y、z,表示村莊x、y之間有一條長度為z的道路。 接下來的M行,每行一個整數t,表示一個寶物變動的操作。若該操作前村莊t內沒有寶物,則操作後村莊內有寶物;若該操作前村莊t內有寶物,則操作後村莊內沒有寶物。

Output

M行,每行一個整數,其中第i行的整數表示第i次操作之後玩家找到所有寶物需要行走的最短路程。若只有一個村莊內有寶物,或者所有村莊內都沒有寶物,則輸出0。

Sample Input

4 5
1 2 30
2 3 50
2 4 60
2
3
4
2
1

Sample Output

0
100
220
220
280

HINT

1<=N<=100000


1<=M<=100000
對於全部的數據,1<=z<=10^9

題解:

本以為這是一道虛樹題····然而並沒有什麽卵關系···

考慮靜態時求ans,肯定是將每個有寶藏的點按dfs序排個序···然後兩兩間求最短距離相加,最後再加上末首位的點的最短距離就可以了····相當於模擬走動的過程

然而題目要加點和刪點···直接用set求前驅後繼維護ans就可以了···註意邊界條件···

md一個sbset題調了我一個上午···我可能是智障吧····註意set的end()返回的是叠代器最後的一個位置··這個位置並沒有賦值···因此要求末位的點需要*--set.end();

代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
using namespace std;
set<int>st;
set<int>:: iterator t;
const int inf=0x3f3f3f3f;
const int N=1e5+5;
int first[N],go[N*2],next[N*2],val[N*2],tot,n,m,dfn[N],cnt,jud[N],id[N],deep[N],g[N][20];
long long dis[N],ans;
inline int R()
{
  char c;int f=0;
  for(c=getchar();c<0||c>9;c=getchar());
  for(;c<=9&&c>=0;c=getchar())
    f=(f<<3)+(f<<1)+c-0;
  return f;
}
inline void comb(int a,int b,int c)
{
  next[++tot]=first[a],first[a]=tot,go[tot]=b,val[tot]=c;
  next[++tot]=first[b],first[b]=tot,go[tot]=a,val[tot]=c;
}
inline void dfs(int u,int fa)
{
  dfn[u]=++cnt;id[cnt]=u;
  for(int e=first[u];e;e=next[e])
  {
    int v=go[e];if(v==fa)  continue;
    dis[v]=(long long)val[e]+dis[u];g[v][0]=u;deep[v]=deep[u]+1;
    dfs(v,u);
  }
}
inline int get(int a,int b)
{
  if(deep[a]<deep[b])  swap(a,b);
  int i,j; 
  for(i=0;(1<<i)<=deep[a];i++);i--;
  for(j=i;j>=0;j--)
    if(deep[a]-(1<<j)>=deep[b])  a=g[a][j];
  if(a==b)  return a;
  for(i=17;i>=0;i--)
    if(g[a][i]!=g[b][i])
      a=g[a][i],b=g[b][i];
  return g[a][0];
}
inline long long Dis(int a,int b)
{
  int lca=get(a,b);
  return dis[a]+dis[b]-dis[lca]*2;
}
inline int pre(int x)
{
  t=st.find(dfn[x]);
  return t==st.begin()?0:id[*--t];
}
inline int nxt(int x)
{
  t=st.find(dfn[x]);
  return ++t==st.end()?0:id[*t];
}
inline void Insert(int x)
{
  st.insert(dfn[x]);
  int l=pre(x);int r=nxt(x);
  if(l)  ans+=Dis(l,x);
  if(r)  ans+=Dis(r,x);
  if(l&&r)  ans-=Dis(l,r);
}
inline void Delete(int x)
{
  int l=pre(x);int r=nxt(x);
  if(l)  ans-=Dis(l,x);
  if(r)  ans-=Dis(r,x);
  if(l&&r)  ans+=Dis(l,r);
  st.erase(dfn[x]);
}
int main()
{
  //freopen("a.in","r",stdin);
  n=R(),m=R();int a,b,c;
  for(int i=1;i<n;i++)
  {
    a=R(),b=R(),c=R();
    comb(a,b,c);
  }
  deep[1]=1;dfs(1,0);
  for(int i=1;i<=17;i++)
    for(int j=1;j<=n;j++)
      g[j][i]=g[g[j][i-1]][i-1];
  while(m--)
  {
    a=R();jud[a]=(jud[a]+1)%2;
    if(jud[a])
      Insert(a);
    else
      Delete(a);
    printf("%lld\n",st.size()?ans+Dis(id[*st.begin()],id[*--st.end()]):0);
  }
  return 0;
}

刷題總結——尋寶遊戲(bzoj3991 dfs序)