题意:

  给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值。(记住只能利用原来给的树边。给的树边已经有向。10万个点,10万个操作)

思路:只能用 O(nlogn)的复杂度。官方题解:

  

  重点也就是要找到集合S中的以x和y为端点一条链,使得操作点u到达这条链是最近的。删除也是这样,找到这条链,删除u到这链的路长。

步骤:

  (1)记录从根遍历的DFS序。

  (2)计算每个点到根的路径所经过边的权之和。

  (3)对于每个操作,无论哪种,如果u的序最大,那么在比u的序小的里面挑一个最大x和一个最小y。再按照式子就可算出,如果是插入就加上这个权和,否则就是减去。

 #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
vector<int> chld[N]; //孩子
unordered_map<int,int> weight[N];
int dis[N];
int mapp[N];
int seq[N]; //seq记录DFS序
int anti_seq[N]; //anti记录第几个访问的是谁
bool inset[N];
int ans, num;
set<int> sett; void DFS(int x)
{
seq[x]=++num;
for(int i=; i<chld[x].size(); i++)
{
int t=chld[x][i];
if(!seq[t]) DFS(t);
}
} unordered_map<int,int> vis;
int LCA(int a,int b)
{
vis.clear();
while(mapp[a])
{
vis[a]=;
a=mapp[a];
}
vis[a]=;
while( !vis[b] ) b=mapp[b];
return b;
} void cal_weight(int n)
{
for(int i=; i<=n; i++)
{
int sum=, s=i;
while(mapp[s])
{
sum+=weight[mapp[s]][s];
s=mapp[s];
}
dis[i]=sum;
}
} int ins(int u)
{
sett.insert(seq[u]);
if(sett.size()==) {ans=;return ;} set<int>::iterator it=sett.find(seq[u]); int x=, y=;
if(*it==*sett.begin()) //已经是最小
{
x=anti_seq[*(++it)];
y=anti_seq[*(sett.rbegin())];
}
else if(*it==*sett.rbegin()) //已经是最大
{
x=anti_seq[*(--it)];
y=anti_seq[*(sett.begin())];
}
else
{
x=anti_seq[ *(--it)];
y=anti_seq[ *(++(++it)) ];
} ans+=dis[u]-dis[LCA(u,x )]- dis[LCA(u,y )] + dis[LCA(x,y)];
return ans;
} int del(int u)
{
if(sett.size()==)
{
sett.erase(seq[u]);
ans=;
return ;
} set<int>::iterator it=sett.find(seq[u]);
int x=, y=;
if(*it==*sett.begin()) //已经是最小
{
x=anti_seq[*(++it)];
y=anti_seq[*(sett.rbegin())];
}
else if(*it==*sett.rbegin()) //已经是最大
{
x=anti_seq[*(--it)];
y=anti_seq[*(sett.begin())];
}
else
{
x=anti_seq[ *(--it)];
y=anti_seq[ *(++(++it)) ];
}
ans-=dis[u]-dis[LCA(u,x )]- dis[LCA(u,y )] + dis[LCA(x,y)];
sett.erase(seq[u]);
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
int a, b, c, t, q, n, j=;
cin>>t;
while(t--)
{
scanf("%d%d", &n, &q);
for(int i=; i<=n+; i++) chld[i].clear(),weight[i].clear(); memset(inset, , sizeof(inset));
memset(seq, , sizeof(seq));
memset(anti_seq, , sizeof(anti_seq));
memset(mapp, , sizeof(mapp));
memset(dis,,sizeof(dis));
sett.clear();
ans=;
num=; for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
chld[a].push_back(b);
weight[a][b]=c;
mapp[b]=a;
}
printf("Case #%d:\n",++j);
int root=n; //获得树根
while(mapp[root]) root=mapp[root];
DFS(root); //获得DFS序 for(int i=; i<=n; i++) anti_seq[seq[i]]=i; //反向索引
cal_weight(n); //计算每个点到根的权
while(q--)
{
scanf("%d%d",&a,&b);
if(a==) //如果不存在于集合中,则插,有则不插
{
if(inset[b]) printf("%d\n", ans); //已经存在,不必计算
else
{
inset[b]=;
printf("%d\n", ins(b));
}
}
else //如果存在于集合中,则删,否则不删。
{
if(inset[b]) //存在,要删
{
inset[b]=;
printf("%d\n",del(b));
}
else printf("%d\n",ans);
}
}
}
return ;
}

AC代码

HDU 5296 Annoying problem (LCA,变形)的更多相关文章

  1. HDU 5296 Annoying problem LCA+树状数组

    题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  2. HDU 5296 Annoying problem dfs序 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...

  3. HDU 5296 Annoying problem dfs序 lca set

    Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...

  4. HDU 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDOJ 5296 Annoying problem LCA+数据结构

    dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...

  7. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  8. hdu5296(2015多校1)--Annoying problem(lca+一个公式)

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. 【HDOJ】5296 Annoying problem

    LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] ...

随机推荐

  1. 剑指offer--面试题17

    题目:合并两个排序的单向链表 自己所写代码如下: ListNode* MergeSortedLists(ListNode* pHead1, ListNode* pHead2) { if(pHead1 ...

  2. Java多线程——<二>将任务交给线程,线程声明及启动

    一.任务和线程 <thinking in java>中专门有一小节中对线程和任务两个概念进行了具体的区分,这也恰好说明任务和线程是有区别的. 正如前文所提到的,任务只是一段代码,一段要达成 ...

  3. The service ‘xxx’ configured for WCF is not registered with the Autofac container

    最近在使用autofac.wcf时,报如下异常: Exception Details: System.InvalidOperationException: The service 'xxx' conf ...

  4. Java Socket 基础例子

    1.服务器端代码 package com.lanber.socket; import java.io.DataInputStream; import java.io.DataOutputStream; ...

  5. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  6. VisualSvn Server介绍

    1 .VisualSvn Server  VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和Visual Studio集成在一起,但是不免费 ...

  7. 【转】Windows平台SSH登录Linux并使用图形化界面

    备注:经验证本文提供的方法可行且比使用VNC简洁一些.略有修改.   [日期:2011-09-06] 来源:Linux社区  作者:tianhuadihuo   http://www.linuxidc ...

  8. 单例模式与Android

    http://blog.csdn.net/ljianhui/article/details/29275655 多线程下的单例模式是不安全的 Android中的单例模式 Android中存在着大量的单例 ...

  9. 张小龙谈如何写E-mail软件

    编者语:鼎鼎大名的Foxmail软件制作者,你一定不会陌生吧!本刊第三期特刊还刊登过此君的生活照一张,可谓威风八面.小编此次突发奇想,“死缠烂打”,费了九牛二虎之力,终于约他写了一篇有关写E-mail ...

  10. python optparse模块

    为了从更快的的使用这个模块,我们会省略一些东西,开始吧. 1. 加载模块 from optparse import OptionParser 2. 实例化OptionParse,可以理解为表明usea ...