Annoying problem

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5296

Description

Coco has a tree, whose nodes are conveniently labeled by 1,2,…,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty.

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 ?

Input

one integer number T is described in the first line represents the group number of testcases.( T<=10 )

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)

Output

Each testcase outputs a line of "Case #x:" , x starts from 1.

The next q line represents the answer to each operation.

Sample Input

1

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

Sample Output

Case #1:

0

6

8

8

4

Hint

题意

给你一棵树,现在有两个操作

1.把某个点染成黑色

2.把某个点染成白色

然后每次操作结束后,问你黑色点构成的树的所有边权和是多少。

一开始全是白色的点。

题解:

首先黑色点构成的树一定是唯一的。

然后我们加入一个点,就只用考虑以前那堆点中dfs序比他小和比他大的点u,v。

如果找不到的话,找字典序最大和最小的点就好了。

然后这棵树增加的距离就是dis(x,u)+dis(x,v)+dis(u,v)

删除点也是一样的。

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+7;
int flag[maxn],idx[maxn],cnt=1,n,q,LCA[maxn][21],deep[maxn],G[maxn][21];
vector<pair<int,int> >E[maxn];
set<pair<int,int> >S;
long long ans = 0;
void init()
{
ans = 0;
S.clear();cnt=1;
memset(idx,0,sizeof(idx));
memset(flag,0,sizeof(flag));
memset(LCA,0,sizeof(LCA));
memset(G,0,sizeof(G));
memset(deep,0,sizeof(deep));
for(int i=0;i<maxn;i++)E[i].clear();
}
void dfs(int x,int fa)
{
idx[x]=cnt++;
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i].first;
if(v==fa)continue;
LCA[v][0]=x,deep[v]=deep[x]+1,G[v][0]=E[x][i].second;
dfs(v,x);
}
} long long QueryDis(int u , int v){
long long ans = 0;
if(deep[u] < deep[v]) swap( u , v );
for(int i = 20 ; i >= 0 ; -- i ) if( deep[u] - (1 << i) >= deep[v] ) ans += G[u][i],u = LCA[u][i];
if( u == v ) return ans;
for(int i = 20 ; i >= 0 ; -- i ) if( LCA[u][i] != LCA[v][i] ) ans += (G[u][i] + G[v][i]) , u = LCA[u][i] , v = LCA[v][i];
return ans + G[u][0] + G[v][0];
} void Lca_init()
{
for(int j = 1 ; j <= 20 ; ++ j)
for(int i = 1 ; i <= n ; ++ i)
if(LCA[i][j-1]){
LCA[i][j]=LCA[LCA[i][j-1]][j-1];
G[i][j] = G[i][j-1] + G[LCA[i][j-1]][j-1];
}
}
long long solve(int x)
{
if(S.size()==0)return 0;
set<pair<int,int> >::iterator it;
it=S.lower_bound(make_pair(idx[x],x));
int d1,d2;
if(it==S.begin()||it==S.end())
{
d1=S.begin()->second;
d2=S.rbegin()->second;
}
else
{
d1=it->second;
it--;
d2=it->second;
}
return QueryDis(d1,x)+QueryDis(d2,x)-QueryDis(d1,d2);
}
void solve()
{
init();
scanf("%d%d",&n,&q);
for(int i=1;i<n;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
E[x].push_back(make_pair(y,z));
E[y].push_back(make_pair(x,z));
}
dfs(1,-1);
Lca_init();
while(q--)
{
int op,x;scanf("%d%d",&op,&x);
if(op==1&&flag[x]==1);
else if(op==2&&flag[x]==0);
else if(op==1)flag[x]=1,ans+=solve(x),S.insert(make_pair(idx[x],x));
else if(op==2)flag[x]=0,S.erase(make_pair(idx[x],x)),ans-=solve(x);
printf("%lld\n",ans/2);
}
} int main()
{
int t;scanf("%d",&t);
for(int i=1;i<=t;i++)
{
printf("Case #%d:\n",i);
solve();
}
return 0;
}

HDU 5296 Annoying problem dfs序 lca的更多相关文章

  1. HDU 5296 Annoying problem dfs序 lca set

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

  2. HDU 5296 Annoying problem (LCA,变形)

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

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

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

  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. hdu_5293_Tree chain problem(DFS序+树形DP+LCA)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...

  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. HDOJ 5296 Annoying problem LCA+数据结构

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

  9. HDU 3966 dfs序+LCA+树状数组

    题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...

随机推荐

  1. React Native 与 夜神模拟器的绑定

    之前一直用真机去调试, 每回更新一次都需要手动摇晃手机后才能reload JS, OMG,太麻烦了. 后来寻思模拟器网上推荐用Geny...什么的模拟器,但是那个模拟器还需要VBox一起用. 有点麻烦 ...

  2. 【Tomcat】 windows下注册tomcat服务以及设置jvm参数

    注册服务: 1 >cd /d D:\Java\tomcat-7.0.57-Css\bin //进入目录 1 >service.bat install  //注册服务,同理删除服务为 rem ...

  3. juery获取元素的方法

    1 从集合中通过指定的序号获取元素 html: 复制代码 代码如下: <div> <p>0</p> <p>1</p> <p>2& ...

  4. #题目:有10 台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次

    #题目:有10 台被监控主机.一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次 #测试机器:虚拟机Linux ...

  5. [HTML]增加input标签的multiple属性上传的文件数

    .发现问题 <input type="file" name="myfile[]" multiple="multiple"/> 最 ...

  6. MySQL之正则表达式

    一.介绍 正则表达式用来描述或者匹配符合规则的字符串.它的用法和like比较相似,但是它又比like更强大,能够实现一些很特殊的规则匹配:正则表达式需要使用REGEXP命令,匹配上返回"1& ...

  7. Tomcat集群的session共享

    配置Tomcat的session共享可以有三种解决方案: 第一种是以负载均衡服务器本身提供的session共享策略,每种服务器的配置是不一样的并且nginx本身是没有的. 第二种是利用web容器本身的 ...

  8. C# 6.0 新特性 (四)

    原文: 1.http://www.cnblogs.com/BoyceYang/p/3711343.html 2.http://www.cnblogs.com/lhking/p/3660182.html ...

  9. vue引入自己写的js文件

    话不多说,直接上代码呀~ 先来个结构图: 中规中矩的vue-cli就写了一个自己的js文件 那么我想要引入到vue组件里. 1.首先写我的js文件 2.引入到vue组件!!!一定要用{}把方法名拿过来 ...

  10. dubbo 搭建开发环境

    本文是基于maven的,预先使用,先装maven. dubbo是一个分布式服务框架,提供一个SOA的解决方案.简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互. 本文旨在搭建 ...