题意:

  给一棵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. asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数

    实体类    public class User    {        public int Id { get; set; }        public string Name { get; se ...

  2. execl执行解释器文件以及shell命令

    问题描述:        execl执行解释器文件以及shell命令 问题解决: 具体源文件:

  3. 【POJ】【3710】Christmas Game

    博弈论 贾志豪论文上的题目……题解请看论文 Orz了一下Hzwer Source Code Problem: User: sdfzyhy Memory: 716K Time: 0MS Language ...

  4. uva 10859

    刘书例题  树形dp #include <cstdio> #include <cstdlib> #include <cmath> #include <map& ...

  5. Eclipse对svn操作切换账号或更换svn地址方法

    1. 切换账号,主要是删除配置文件达到重新更新svn的时候,弹出框让重新输入新的svn用户名和密码. 1.通过删除SVN客户端的账号配置文件   1)查看你的Eclipse中使用的是什么SVN Int ...

  6. Python Requests模块讲解4

    高级用法 会话对象 请求与响应对象 Prepared Requests SSL证书验证 响应体内容工作流 保持活动状态(持久连接) 流式上传 块编码请求 POST Multiple Multipart ...

  7. 【设计模式六大原则4】接口隔离原则(Interface Segregation Principle)

      定义:客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来:类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不是最小接口,则类B和类 ...

  8. sql over()---转载

    1.使用over子句与rows_number()以及聚合函数进行使用,可以进行编号以及各种操作.而且利用over子句的分组效率比group by子句的效率更高. 2.在订单表(order)中统计中,生 ...

  9. hdu 1875 畅通工程再续(最小生成树,基础)

    题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...

  10. 【hadoop2.6.0】MapReduce原理

    看了几篇博文,感觉还是云里雾里的. http://blog.csdn.net/opennaive/article/details/7514146 http://www.aboutyun.com/thr ...