Annoying problem

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1006    Accepted Submission(s): 330

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
 
解题:LCA居然可以解决这种问题。。。
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[maxn*];
int head[maxn],fa[maxn][],dep[maxn],ti,tot;
int n,q,timeStamp[maxn],node[maxn],dfn[maxn];
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
void dfs(int u,int f) {
timeStamp[u] = ++ti;
node[ti] = u;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == f) continue;
dfn[e[i].to] = dfn[u] + e[i].w;
fa[e[i].to][] = u;
dep[e[i].to] = dep[u] + ;
for(int j = ; j < ; ++j)
fa[e[i].to][j] = fa[fa[e[i].to][j-]][j-];
dfs(e[i].to,u);
}
}
int LCA(int u,int v) {
if(dep[u] < dep[v]) swap(u,v);
int log;
for(log = ; (<<log) <= dep[u]; ++log);
for(int i = log-; i >= ; --i)
if(dep[u] - (<<i) >= dep[v]) u = fa[u][i];
if(u == v) return u;
for(int i = log-; i >= ; --i) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][];
}
set<int>st;
int solve(int u) {
if(st.empty()) return ;
int x,y;
auto it = st.upper_bound(u);
if(it == st.begin() || it == st.end()) {
x = node[*st.begin()];
y = node[*st.rbegin()];
} else {
x = node[*it];
y = node[*(--it)];
}
u = node[u];
return dfn[u] - dfn[LCA(x,u)] - dfn[LCA(y,u)] + dfn[LCA(x,y)];
}
bool used[maxn];
int main() {
int kase,u,v,w,op,cs = ;
scanf("%d",&kase);
while(kase--) {
memset(head,-,sizeof head);
memset(used,false,sizeof used);
scanf("%d%d",&n,&q);
for(int i = ti = tot = ; i < n-; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dep[] = ;
int ret = dfn[] = ;
dfs(,-);
st.clear();
printf("Case #%d:\n",cs++);
while(q--) {
scanf("%d%d",&op,&u);
u = timeStamp[u];
if(op == && !used[u]) {
used[u] = true;
ret += solve(u);
st.insert(u);
} else if(op == && used[u]) {
used[u] = false;
st.erase(u);
ret -= solve(u);
}
printf("%d\n",ret);
}
}
return ;
}

2015 Multi-University Training Contest 1 hdu 5296 Annoying problem的更多相关文章

  1. HDU 5296 Annoying problem

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

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

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

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

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

  5. HDU 5296 Annoying problem dfs序 lca set

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

  6. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  7. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  8. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  9. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

随机推荐

  1. html5 过程解决问题收集

    1.使用画布时报错误: Uncaught TypeError: Object [object Object] has no method 'getContext' . 解决方法: $(function ...

  2. android创建桌面快捷键shortcut

    有非常多人也写过创建桌面快捷键的blog.可是大部分都仅仅讲了怎么用,事实上技术使用起来都非常easy.可是你使用后下次还知道吗? 根本原因还是不清楚原理.今天我就来讲讲shortcut创建过程. 过 ...

  3. 全面具体介绍一个P2P网贷领域的ERP系统的主要功能

        一般的P2P系统,至少包含PC站点的前端和后端.前端系统的功能.能够參考"P2P系统哪家强,功能事实上都一样" http://blog.csdn.net/fansunion ...

  4. POJ 题目3461 Oulipo(KMP)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 Descript ...

  5. UVA 11000- Bee 递推

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  6. Ruby学习笔记(二)——从管道读取数据

    在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...

  7. asf

    这些日子我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能  够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没有方向  ,所以把我的经历写出来与大家共勉, ...

  8. 2016 提高组c++ 错题

    需重做 树的重心 链表 计算机基础知识 无线通讯技术: 蓝牙,wifi,GPRS 现在常用的无线通信技术:FM调频广播(用于收音机): 2G.3G移动通信技术(中国移动.中国联通.中国电信正在运营的网 ...

  9. Hua Wei 机试题目三---2014

    一.根据对应规则进行翻译输出 描述:已知有如下的对应规则: ,则输入任意个正整数,输出经过规则翻译以后对应的结果. 例如:输入:1234:输出bcde. 题目很简单,我觉得需要注意的问题就是对于大整数 ...

  10. Zeplin(for Windows)无缝集成到了 Adobe XD

    Zeplin(for Windows)无缝集成到了 Adobe XD 大约6个月前,推出了 Zeplin 的新Adobe XD CC集成.从那时起,数十万个设计从Adobe XD导出到Zeplin.Z ...