题意:

  给一棵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. 【BZOJ】【1085】【SCOI2005】骑士精神

    IDA*算法 Orz HZWER A*+迭代加深搜索=IDA* 这题的估价相当于一个可行性剪枝,即如果当前走的步数s+未归位的点数>搜索深度k,则剪枝 /******************** ...

  2. .run文件安装

    比如realplay.run 安装方法如下 chmod +x realplay.run ./realplay.run 然后他就会执行安装了,在过程中可能会要求你输入yes或no 安装完后就可以用了

  3. PAT-乙级-1046. 划拳(15)

    1046. 划拳(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 划拳是古老中国酒文化的一个有趣的组成部分 ...

  4. java 中 String 类的几个问题

    首先,我们要搞清楚,在java中,引用和基本数据类型是存储在栈中的.而对象是存储在堆中的. 只有一个例外,就是String对象. 例如: String str1="test"; S ...

  5. POJ 1663

    #include<iostream>//cheng da cai zi using namespace std; int main() { int time; cin>>tim ...

  6. 一天,python搞个分析NGINX日志的脚本

    准备给ZABBIX用的. 统计接口访问字次,平均响应时间,4XX,5XX次数 以后可以再改进.. #!/usr/bin/env python # coding: utf-8 ############# ...

  7. http://www.oschina.net/translate/elasticsearch-getting-started?cmp

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp

  8. 用DateTime.ToString(string format)输出不同格式的日期

    http://www.cnblogs.com/xvqm00/archive/2009/02/19/1394093.html DateTime.ToString()函数有四个重载.一般用得多的就是不带参 ...

  9. 国内为什么没有好的 Stack Overflow 的模仿者?

    国内为什么没有好的 Stack Overflow 的模仿者? 个人觉得, 高端的程序员会直接上stackoverflow提问, 所以国内中文的stackoverflow必然面对低端程序员. 鉴于中国程 ...

  10. [Unity菜鸟] Character控制移动

    1. 给角色加角色控制器组件,然后用以下代码可以控制角色移动和跳跃 float speed = 6.0f; float jumpSpeed = 8.0f; float gravity = 20.0f; ...