【bzoj3757】 蘋果樹

來源:互聯網
上載者:User

標籤:

http://www.lydsy.com/JudgeOnline/problem.php?id=3757 (題目連結)

MD調了好久,最後蒯了幾個標程交上去,沒想到都RE了。。。最後才看到: 
 
= =

題意:求樹上兩點間路徑上有多少個不同的點權。

Solution 
  像這種樹鏈剖分解決不了的問題,大概就是樹上莫隊。 
  我們dfs一遍對樹分塊,這個很簡單,不會的見bzoj1086。之後對詢問(u,v)以在u所在的塊的位置為第一關鍵字,v的dfs序為第二關鍵字。其實這對於我們之後處理問題沒有任何協助,只是保證莫隊的複雜度為nsqrtn,因為每個塊的大小為sqrt(n),塊的個數也是sqrt(n),所以這樣以dfs序為第二關鍵字排序後,在每個塊內進行修改的話,最壞情況頂多是走遍整棵樹,O(n)。 
  怎麼插入刪除以及求解呢?蒯了一個神題解,不過有公式恐懼症的我看了幾眼就不想看了,直接磨標程去了。。 
  

用S(v, u)代表 v到u的路徑上的結點的集合。 
用root來代表根結點,用lca(v, u)來代表v、u的最近公用祖先。 
那麼 
S(v, u) = S(root, v) xor S(root, u) xor lca(v, u) 
其中xor是集合的對稱差。 
簡單來說就是節點出現兩次消掉。 
lca很討厭,於是再定義 
T(v, u) = S(root, v) xor S(root, u) 
觀察將curV移動到targetV前後T(curV, curU)變化: 
T(curV, curU) = S(root, curV) xor S(root, curU) 
T(targetV, curU) = S(root, targetV) xor S(root, curU) 
取對稱差: 
T(curV, curU) xor T(targetV, curU)= (S(root, curV) xor S(root, curU)) xor (S(root, targetV) xor S(root, curU)) 
由於對稱差的交換律、結合律: 
T(curV, curU) xor T(targetV, curU)= S(root, curV) xorS(root, targetV) 
兩邊同時xor T(curV, curU): 
T(targetV, curU)= T(curV, curU) xor S(root, curV) xor S(root, targetV) 
發現最後兩項很爽……哇哈哈 
T(targetV, curU)= T(curV, curU) xor T(curV, targetV) 
(有公式恐懼症的不要走啊 T_T) 
也就是說,更新的時候,xor T(curV, targetV)就行了。 
即,對curV到targetV路徑(除開lca(curV, targetV))上的結點,將它們的存在性取反即可。 
” 
——vfk部落格

搞了這麼多,其實道理很簡單。對於一個詢問,用數組p[i]記錄顏色i在當前詢問中出現了幾次;vis[i]記錄節點i是否已經算過了,如果已經算過了,那麼就是要刪除當前節點,反之則是要加入當前節點,順便更新答案ans即可。

代碼的話我也不知道是不是正確的= = 


代碼:

// bzoj3757#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#define MOD 1000000007#define inf 2147483640#define LL long long#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);using namespace std;inline LL getint() {    LL x=0,f=1;char ch=getchar();    while (ch>‘9‘ || ch<‘0‘) {if (ch==‘-‘) f=-1;ch=getchar();}    while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}    return x*f;}const int maxn=100010;struct edge {int to,next;}e[maxn<<2];struct ask {int u,v,a,b,id;}t[maxn];int cnt,n,q,m,block,top,ans;int head[maxn],dfn[maxn],fa[maxn][20],bin[20],deep[maxn],p[maxn];int st[maxn],pos[maxn],vis[maxn],c[maxn],a[maxn],res[maxn];void insert(int u,int v) {    e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;    e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;}int dfs(int x) {    dfn[x]=++cnt;    int size=0;    for (int i=1;i<20;i++) fa[x][i]=fa[fa[x][i-1]][i-1];    for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa[x][0]) {            deep[e[i].to]=deep[x]+1;            fa[e[i].to][0]=x;            size+=dfs(e[i].to);            if (size>=block) {                m++;                for (int j=1;j<=size;j++) pos[st[top--]]=m;                size=0;            }        }    st[++top]=x;    return size+1;}bool cmp(ask a,ask b) {    return pos[a.u]==pos[b.u] ? dfn[a.v]<dfn[b.v] : pos[a.u]<pos[b.u];}int lca(int x,int y) {    if (deep[x]<deep[y]) swap(x,y);    int t=deep[x]-deep[y];    for (int i=0;bin[i]<=t;i++) if (bin[i]&t) x=fa[x][i];    for (int i=19;i>=0;i--)        if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];    return x==y?x:fa[x][0];}void work(int x) {    if (!vis[x]) {vis[x]=1;p[c[x]]++;if (p[c[x]]==1) ans++;}    else {vis[x]=0;p[c[x]]--;if (p[c[x]]==0) ans--;}}void solve(int u,int v) {    while (u!=v) {        if (deep[u]>deep[v]) work(u),u=fa[u][0];        else work(v),v=fa[v][0];    }}int main() {    bin[0]=1;for (int i=1;i<20;i++) bin[i]=bin[i-1]<<1;    scanf("%d%d",&n,&q);    for (int i=1;i<=n;i++) scanf("%d",&c[i]);    for (int i=1;i<=n;i++) {        int u,v;        scanf("%d%d",&u,&v);        insert(u,v);    }    block=(int)sqrt(n);    cnt=0;    dfs(e[head[0]].to);    m++;    while (top) pos[st[top--]]=m;    for (int i=1;i<=q;i++) {        scanf("%d%d%d%d",&t[i].u,&t[i].v,&t[i].a,&t[i].b);        if (dfn[t[i].u]>dfn[t[i].v]) swap(t[i].u,t[i].v);        t[i].id=i;    }    sort(t+1,t+1+q,cmp);    solve(t[1].u,t[1].v);    int x=lca(t[1].u,t[1].v);    work(x);    res[t[1].id]=ans;    if (p[t[1].a] && p[t[1].b] && t[1].a!=t[1].b) res[t[1].id]--;    work(x);    for (int i=2;i<=q;i++) {        solve(t[i-1].u,t[i].u);        solve(t[i-1].v,t[i].v);        x=lca(t[i].u,t[i].v);        work(x);        if (p[t[i].a] && p[t[i].b] && t[i].a!=t[i].b) res[t[i].id]=ans-1;        else res[t[i].id]=ans;        work(x);    }    for (int i=1;i<=q;i++) printf("%d\n",res[i]);    return 0;}

  

【bzoj3757】 蘋果樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.