dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑:

1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的点......

2.假设x在集合中的点某一側,则找距离x的dfs序最小和最大的点

将x插入这个集合最少要走的距离为 dist[x]-dist[LCA(left,x)]-dist[LCA(right,x)]+dist[LCA(left,right)]

删除同理

Annoying problem

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 334    Accepted Submission(s): 95

Problem 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
 
Author
FZUACM
 
Source
 

/* ***********************************************
Author :CKboss
Created Time :2015年07月21日 星期二 21时06分11秒
File Name :HDOJ5296.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=120100;
const int INF=0x3f3f3f3f; struct Edge
{
int to,next,cost;
}edge[maxn*2]; int Adj[maxn],Size;
void init() { memset(Adj,-1,sizeof(Adj)); Size=0; } void Add_Edge(int u,int v,int c)
{
edge[Size].to=v; edge[Size].cost=c;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int n,q;
int dist[maxn],ti[maxn],cnt; void dfs(int len,int u,int fa)
{
dist[u]=len; ti[u]=cnt;
for(int i=Adj[u];~i;i=edge[i].next)
{
int to=edge[i].to;
int cost=edge[i].cost;
if(to==fa) continue;
cnt++;
dfs(len+cost,to,u);
}
} /********************** LCA **********************************/ const int DEG=20;
int fa[maxn][DEG];
int deg[maxn]; void BFS(int root)
{
queue<int> q; memset(deg,0,sizeof(deg));
memset(fa,0,sizeof(fa)); deg[root]=0;
fa[root][0]=root;
q.push(root);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=1;i<DEG;i++)
{
fa[u][i]=fa[fa[u][i-1]][i-1];
}
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa[u][0]) continue;
deg[v]=deg[u]+1;
fa[v][0]=u;
q.push(v);
}
}
} int LCA(int u,int v)
{
if(deg[u]>deg[v]) swap(u,v);
int hu=deg[u],hv=deg[v];
int tu=u,tv=v;
for(int det=hv-hu,i=0;det;i++,det=det/2)
{
if(det&1) tv=fa[tv][i];
}
if(tu==tv) return tu;
for(int i=DEG-1;i>=0;i--)
{
if(fa[tu][i]==fa[tv][i]) continue;
tu=fa[tu][i]; tv=fa[tv][i];
}
return fa[tu][0];
} struct Node
{
int val,cnt;
bool operator<(const Node& nd) const { return cnt<nd.cnt; }
bool operator==(const Node& nd) const { return val==nd.val; }
bool operator!=(const Node& nd) const { return val!=nd.val; }
}; set<Node> st; int CL(int flag,Node ND)
{
if(flag==0) st.erase(ND); set<Node>::iterator it1,it2;
int x=ND.val;
it2=st.upper_bound(ND);
it1=it2; it1--; /// check if in mid
if(it1->val!=0&&it2->val!=n+10) /// in mid
{
int left=it1->val;
int right=it2->val; if(flag) st.insert(ND);
return dist[x]-dist[LCA(x,left)]-dist[LCA(x,right)]+dist[LCA(left,right)];
}
else // in side
{
if(it2->val==n+10) /// all in left
{
it2=st.begin(); it2++;
int left=it2->val;
int right=it1->val;
if(flag) st.insert(ND);
return dist[x]-dist[LCA(x,left)]-dist[LCA(x,right)]+dist[LCA(left,right)];
}
else if(it1->val==0) /// all in right
{
int left=it2->val;
it1=st.end();
it1--; it1--;
int right=it1->val; if(flag) st.insert(ND);
return dist[x]-dist[LCA(x,left)]-dist[LCA(x,right)]+dist[LCA(left,right)];
}
}
} /// return change val of solve
int solve(int kind,int x)
{
Node ND = (Node){x,ti[x]}; /// if in mid find nearst point
set<Node>::iterator it1,it2; if(kind==1) // add
{
if(st.count(ND)==1) return 0;
if(st.size()==2)
{
st.insert(ND);
return 0;
}
else if(st.size()==3)
{
it1=st.begin(); it1++;
int v=it1->val;
st.insert(ND);
return dist[x]+dist[v]-2*dist[LCA(v,x)];
}
else
{
return CL(1,ND);
}
}
else if(kind==2) // remove
{
if(st.count(ND)==0) return 0;
if(st.size()==3)
{
st.erase(ND);
return 0;
}
else if(st.size()==4)
{
it1=st.begin();
it1++; int v=it1->val;
it1++; int u=it1->val;
st.erase(ND);
return dist[u]+dist[v]-2*dist[LCA(u,v)];
}
else
{
return CL(0,ND);
}
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&q);
init();
for(int i=0,u,v,c;i<n-1;i++)
{
scanf("%d%d%d",&u,&v,&c);
Add_Edge(u,v,c); Add_Edge(v,u,c);
}
cnt=1; st.clear();
st.insert((Node){0,-INF});
st.insert((Node){n+10,INF});
dfs(0,1,1); BFS(1); int all=0;
printf("Case #%d:\n",cas++);
while(q--)
{
int k,x;
scanf("%d%d",&k,&x); if(k==1) all+=solve(k,x);
else if(k==2) all-=solve(k,x); printf("%d\n",all);
}
} return 0;
}

HDOJ 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 (LCA,变形)

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

  3. HDU 5296 Annoying problem dfs序 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...

  4. HDU 5296 Annoying problem dfs序 lca set

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

  5. HDU 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. hdu5296(2015多校1)--Annoying problem(lca+一个公式)

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. 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 ...

  8. 【HDOJ】5296 Annoying problem

    LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] ...

  9. HDU5296 Annoying problem(LCA)

    //#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> # ...

随机推荐

  1. ASP.NET-internat身份验证

    ASP.NET-internat身份验证默认在webconfig中配置的代码是这样的 <system.web> <compilation debug="true" ...

  2. ASP.NET-Session与复杂数据类型

    原文链接:http://www.cnblogs.com/fish-li/archive/2013/05/28/3104750.html Session与复杂数据类型 Session有三种工作模式,拿A ...

  3. 一起talk C栗子吧(第一百一十二回:C语言实例--线程同步概述)

    各位看官们,大家好.上一回中咱们说的是线程间通信的样例,这一回咱们说的样例是:线程同步.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,提到同步.我想大家都不陌生,由于我们在前面章回中介绍 ...

  4. 百度 谷歌 Twitter,这么多短链接服务(Short Url)究竟哪家强?

    一.短链接是什么 url=HPqdQ5VR3vA39x7ZWoWyNzwWnsDhTbh66BTpdzsJLroBDzFRm4JV-G818Zc027uZrwe7zxtxnD4H2FUahftpUK& ...

  5. 百度地图 key申请以及基础地图的演示

    之前做过一个拼车的项目,用到了百度地图,如今做电商项目,也遇到了要使用地图,可是刚来这公司不久项目不是自己做的,今天一个同事说定位那边有点问题,所以如今不忙,好好搞下地图,为了以后业务扩展或者出现故障 ...

  6. PHP第八课 字符串拆分经常使用函数

    课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...

  7. 数据结构—单链表(类C语言描写叙述)

    单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...

  8. 除了信号触发线程与接收者线程相同的情况能直接调用到slot,其它情况都依赖事件机制(解决上面代码收不到信号的问题其实很简单,在线程的run();函数中添加一个事件循环就可以了,即加入一句exec();),信号槽不就是一个回调函数嘛

    MainWindow::MainWindow(QWidget *parent) :   QMainWindow(parent)   {   pThreadCon = new CSerialThread ...

  9. bzoj3442: 学习小组(费用流好题)

    3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...

  10. Google Maps API 将开始收费

    Google Maps API 将开始收费 一.总结 一句话总结:国外的话openstreetmap或许不错 国内的话就高德吧 二.Google Maps API 将开始收费 曾经免费的 Google ...