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)
Total Submission(s): 1006 Accepted Submission(s): 330
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 ?
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)
The next q line represents the answer to each operation.
#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的更多相关文章
- HDU 5296 Annoying problem
Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5296 Annoying problem LCA+树状数组
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
- 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 ...
- 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: ...
- 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 ...
- 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 ...
随机推荐
- [SharePoint2010开发入门经典]一、SPS2010介绍
本章概要: 1.熟悉SPS基本特性 2.理解SPS基础架构 3.开发SPS工具
- Libevent学习笔记
学习: /Users/baidu/Documents/Data/Interview/服务器-检索端/libevent参考手册(中文版).pdf 讲的不好.翻译的..
- Flume 读取实时更新的日志文件
http://blog.csdn.net/bright60/article/details/50728306 我用了第一种方法. 1. 日志文件每天roate一个新文件 a) 方案一 There i ...
- rails的数据库查询方法
rails的数据库查询方法 学习了:http://blog.csdn.net/menxu_work/article/details/8664962 学习了:http://www.cnblogs.com ...
- UI设计师不可不知的安卓屏幕知识-安卓100分享
http://www.android100.org/html/201505/24/149342.html UI设计师不可不知的安卓屏幕知识-安卓100分享 不少设计师和工程师都被安卓设备纷繁的屏幕搞得 ...
- netty可靠性
Netty的可靠性 首先,我们要从Netty的主要用途来分析它的可靠性,Netty目前的主流用法有三种: 1) 构建RPC调用的基础通信组件,提供跨节点的远程服务调用能力: 2) NIO通信框架,用于 ...
- SQL 数据库性能优化
http://blog.csdn.net/yzllz001/article/details/54848513 1. 减少数据访问(减少磁盘访问) 2. 返回更少数据(减少网络传输或磁盘访问) 3. ...
- C# WebAPI小记
新建WebAPI项目 新建一个Model 安装Entity Framework 添加连接字符串 去Web.config 中 <configuration> 节点中最下面添加 在Word中编 ...
- Aspx小记
关闭按钮 protected void Close_Click(object sender, EventArgs e) { //Page.RegisterStartupScript("clo ...
- 第一次接触Arduino
1.百度百科: Arduino包含两个主要的部分:硬件部分是可以用来做电路连接的Arduino电路板:另外一个则是 Arduino IDE,你的计算机中的程序开发环境.你只要在IDE中编写程序代码,将 ...