HDU 5296 Annoying problem dfs序 lca set
Annoying problem
Now there are two kinds of operation:
1 x: If the node x is not in the set S, add node x to the set S
2 x: If the node x is in the set S,delete node x from the set S
Now there is a annoying problem: In order to select a set of edges from tree after each operation which makes any two nodes in set S connected. What is the minimum of the sum of the selected edges’ weight ?
For each test:
The first line has 2 integer number n,q(0<n,q<=100000) describe the number of nodes and the number of operations.
The following n-1 lines each line has 3 integer number u,v,w describe that between node u and node v has an edge weight w.(1<=u,v<=n,1<=w<=100)
The following q lines each line has 2 integer number x,y describe one operation.(x=1 or 2,1<=y<=n)
The next q line represents the answer to each operation.
6 5
1 2 2
1 5 2
5 6 2
2 4 2
2 3 2
1 5
1 3
1 4
1 2
2 5
0
6
8
8
4
题意:
给出一棵树,每个边都有一个权值,现在有一个空的集合,两种操作,
1 x吧x节点放到集合中(如果还没放入)
2 x把x节点从集合中拿出来(已放入)。
每次操作后输出最小的边权和,保证这些边可以将这些点连起来。
题解:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
#include<set>
using namespace std;
const int N = 1e5+, M = , mod = , inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0
int in[N],dis[N],head[N],t,vis[N],fa[N][],deep[N],cas=,ff[N],cur,v[N];
vector<pair<int ,int > >G[N];
set<int >s;
struct edge{int to,next,v;}e[N*];
void add(int u,int v,int val) {e[t].to = v;e[t].next = head[u];e[t].v = val; head[u]=t++;}
void dfs(int x,int f) {
in[x] = cur;
ff[cur] = x;
cur++;
for(int i=;i<G[x].size();i++) {
if(G[x][i].first!=f) {
dis[G[x][i].first] = dis[x]+G[x][i].second;
dfs(G[x][i].first,x);
}
}
}
void dfs1(int x) {
vis[x] =;
for(int i=;i<=;i++) {
if(deep[x] < (<<i)) break;
fa[x][i] = fa[fa[x][i-]][i-];
}
for(int i=head[x];i;i=e[i].next) {
if(vis[e[i].to]) continue;
deep[e[i].to] = deep[x]+;
fa[e[i].to][] = x;
dfs1(e[i].to);
}
}
int lca(int x,int y) {
if(deep[x] < deep[y]) swap(x,y);
int t = deep[x] - deep[y];
for(int i=;i<=;i++)
if(t&(<<i)) x = fa[x][i];
for(int i=;i>=;i--)
if(fa[x][i]!=fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
if(x==y) return x;
return fa[x][];
}
int solve(int u){
if (s.empty())
return ;
int x, y;
set<int>::iterator it = s.lower_bound(u), itx = it;
itx--;
if (it == s.end() || it == s.begin()) {
it = s.begin();
itx = s.end();
itx--;
}
y = (*it);
x = (*itx);
y = ff[y];
x =ff[x];
u=ff[u];
//cout<<u<<" "<<x<<" "<<y<<endl;
return dis[u]-dis[lca(u,x)]-dis[lca(u,y)]+dis[lca(x,y)];
}
void init() {
int n,m;
for(int i=;i<N;i++) G[i].clear();s.clear();
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
memset(v,,sizeof(v));
memset(deep,,sizeof(deep));
memset(ff,,sizeof(ff));
memset(fa,,sizeof(fa));
memset(in,,sizeof(in));
cur=;t=;
scanf("%d%d",&n,&m);
for(int i=;i<=n-;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(make_pair(b,c));
G[b].push_back(make_pair(a,c));
add(a,b,c);
add(b,a,c);
}
dfs(,);dfs1();int ans=;
printf("Case #%d:\n",cas++);
for(int i=;i<=m;i++) {
int a,b;
scanf("%d%d",&a,&b);
b=in[b];
if(a==) {
if(!v[b]){
v[b]=;
if(s.size()==){
s.insert(b);
}
else{
ans+=solve(b);
s.insert(b);
}
}
}
else {
if(v[b]){
v[b]=;
s.erase(b);
if(s.size()!=){
ans-=solve(b);
}
}
}
printf("%d\n",ans);
}
}
int main() {int T;
scanf("%d",&T);
while(T--) {
init();
}
return ;
}
HDU 5296 Annoying problem dfs序 lca set的更多相关文章
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...
- HDU 5296 Annoying problem LCA+树状数组
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU 5296 Annoying problem
Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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 ...
- hdu_5293_Tree chain problem(DFS序+树形DP+LCA)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDOJ 5296 Annoying problem LCA+数据结构
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
随机推荐
- springboot的常见配置
1.Springboot热部署 热部署的意思就是当任何类发生改变时,通过JVM类加载的方式加载到虚拟机上,这样就不需要我们重启Application类了 做法: 1)添加一个依赖到pom.xml上: ...
- Tomcat 初探(一) 简介
简述 大部分入了 Java 开发这个坑的朋友,都需要把自己的网站发布到 Web 服务器上,相信也听过 Tomcat 的鼎鼎大名.Tomcat 是由 Sun 公司软件架构师詹姆斯·邓肯·戴维森开发的 W ...
- Oracle学习系类篇(一)
1.表空间介绍 oarcle数据库真正存放数据的是数据文件(data files),Oarcle表空间(tablespaces)实际上是一个逻辑的概念,他在物理上是并不存在的,那么把一组data fi ...
- layer.js漂亮的弹出框
它的官方网站:http://layer.layui.com/ 消息.确认框.ifame.自定义文本.旋转木马,都有按钮,是一款强大的js 弹出框: 以下为本人的简单介绍: layer.open({ t ...
- 反射另一个app中的View
FrameLayout fl = (FrameLayout) findViewById(R.id.content); View v = null; try { Context context = cr ...
- Java 自带MD5 校验文件
http://www.iteye.com/topic/1127319 前天第一次发表博客到论坛,关于Java文件监控一文,帖子地址在:http://www.iteye.com/topic/112728 ...
- Hessian 接口使用示例总结
一.使用hessian接口准备 首先,hessian接口的使用,必须要准备hessian接口的jar包,本文使用的jar包如下:hessian-4.0.7.jar; Hessian接口的使用一般是在两 ...
- runloop源代码
https://github.com/zzf073/runloopDemo /** * 调度例程 * 当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程 ...
- 【AnjularJS系列5 】— scopes、module、controller
第五篇, scopes.module.controller 这一篇,感觉,在前面几篇就使用过的属性,但,总觉得没有理解透彻,有待完善!~ 1.scopes A.定义:$scope是一个把view(一个 ...
- 页面定制CSS代码初探(三):设置正文最小高度
前言 没想到再次写这篇,已经过去1年半了. 现在审美也发生了改变,一开始做的样式全删了,只保留了h2的样式.原先认为界面要宽,两边留太多空很浪费,看惯了知乎和简书,觉得默认最大1000px的排版也不错 ...