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. python第三方库之numpy基础

    前言 numpy是python的科学计算模块,底层实现用c代码,运算效率很高.numpy的核心是矩阵narray运算. narray介绍 矩阵拥有的属性 ndim属性:维度个数 shape属性:维度大 ...

  2. SVMtrain的参数c和g的优化

    SVMtrain的参数c和g的优化 在svm训练过程中,需要对惩罚参数c和核函数的参数g进行优化,选取最好的参数 知道测试集标签的情况下 是让两个参数c和g在某一范围内取离散值,然后,取测试集分类准确 ...

  3. 新一代的USB 3.0传输规格

    通用序列总线(USB) 从1996问世以来,一统个人电脑外部连接界面,且延伸至各式消费性产品,早已成为现代人生活的一部分.2000年发表的USB 2.0 High-speed规格,提供了480Mbps ...

  4. 64_p5

    php-nette-bootstrap-2.4.3-1.fc26.noarch.rpm 20-Feb-2017 07:19 16290 php-nette-caching-2.5.3-1.fc26.n ...

  5. NFS生产场景优化

    1.硬件上多块网卡bond,增加吞吐量,至少千兆.sas/ssd磁盘组raid5或raid10 2.服务端配置:/data 172.16.1.0/24(rw,sync,all_squash,anonu ...

  6. 20180830 安装git时报错,

    安装:https://blog.csdn.net/u013256816/article/details/54743470 解决问题:https://blog.csdn.net/daojibruce/a ...

  7. meta标签的使用方法(PC端)

    <!DOCTYPE html> <html lang="en"> <head> <!--设定页面使用的字符集--> <meta ...

  8. gentoo emerge unable to sync

    gentoo emerge unable to sync Author: Tubo After setting SYNC to customized URL: SYNC="rsync://m ...

  9. Java 中的值传递和引用传递问题

    Java 中的值传递和引用传递问题 public class Operation { int data = 50; void change(int data) { data = data + 100; ...

  10. HTML5实战与剖析之跨文档消息传递(iframe传递信息)

    在来自不同域名的页面间传递消息一般统称为跨文档消息传送,简称XDM.如,www.leemagnum.com域中的页面与位于一个内嵌框架中的http://blog.csdn.net/lee_magnum ...