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. Redis-Cluster集群原理

    一.redis-cluster 官方推荐的 redis 集群解决方案,优点在于去中心化, 去中间件,也就是说,集群中的每个节点都是平等的关系,都是对等的,每个节点都保存各自的数据和整个集群的状态.每个 ...

  2. OA项目知识总结2

    BaseAction的抽取 项目中的每个实体类都对应一个action  每个action都都要继承ActionSupport类 已以及实现ModelDriver接口  并且需要注入service 虽然 ...

  3. LoadRunner结果分析 – TPS

    针对吞吐率和 TPS 的关系,这个在结果分析中如何使用,就个人经验和朋友讨论后,提出如下建议指导,欢迎同僚指正. 相关定义 响应时间 = 网络响应时间 + 应用程序响应时间 响应时间 =(N1+N2+ ...

  4. CF789A. Anastasia and pebbles

    /* CF789A. Anastasia and pebbles http://codeforces.com/contest/789/problem/A 水题 题意:有两个背包,每次分别可取k个物品, ...

  5. rabbitMQ学习笔记(一)Windows 与Linux下rabbitMQ的安装

    版权声明:本文为博主原创文章,未经博主允许不得转载. Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definiti ...

  6. CF894A QAQ

    CF894A QAQ 题意翻译 题目大意: 给定一个字符串,字符串长度<=100,现在要求出字符串中'QAQ'的个数,注意,'QAQ'可以不连续,只要有顺序就可以了,例如QABQ也有一个QAQ ...

  7. 在eclipse中关联android源代码

    1打包源代码成jar: 1 新建一个java项目 2  import  想打包的源代码文件 3 export 这个文件 : 选择java->jar file .  这里会让你选择输出路径 2 加 ...

  8. UVA - 1642 Magical GCD 数学

                                  Magical GCD The Magical GCD of a nonempty sequence of positive integer ...

  9. 完整注册+JQuery验证+selert后台校验

    Java代码 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  10. Scrapy Architecture overview--官方文档

    原文地址:https://doc.scrapy.org/en/latest/topics/architecture.html This document describes the architect ...