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. java+selenium+testNG+excel 实现 web 网页的自动化测试

    webdriver的关键字从excel读取,这样测试人员只需要在excel中填写相关用例即可 前端微站和后台系统的用例可整合在同一excel中,这样可实现前端与后台的闭循环测试 除了一些基本的校验规则 ...

  2. 关于sql连接查询(内联、左联、右联、全联)

    内连接(INNER JOIN)(典型的连接运算,使用像   =   或   <>   之类的比较运算符).包括相等连接和自然连接. 内连接使用比较运算符根据每个表共有的列的值匹配两个表中的 ...

  3. UVa11183 - Teen Girl Squad(最小树形图-裸)

    Problem I Teen Girl Squad  Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...

  4. 请用Java设计一个Least Recently Used (LRU) 缓存

    LRU介绍:LRU是Least Recently Used的缩写,即最少使用页面置换算法,是为虚拟页式存储管理服务的, 思路介绍: 能够使用两个标准的数据结构来实现.Map和Queue.由于须要支持多 ...

  5. 初次使用Android Studio时的配置

    一.第一次安装: Android Studio安装完毕后,第一次启动AS前.为了避免又一次下载新版本号的SDK.操作例如以下: AS启动前.请先将bin文件夹的idea.properties文件里添加 ...

  6. MongoDB初探系列之四:MongoDB与Java共舞

    因为版本号不同,可能API也有所不同.本次学习用的是3.0版本号. 1.使用的mongodb的jdbc驱动版本号为:mongo-java-driver-3.0.0.jar 2.本节仅仅是简介JDBC操 ...

  7. Spring容器装饰者模式应用之实现业务类与服务类自由组合的解决方式

    在不论什么一个项目中都不可或缺的存在两种bean,一种是实现系统核心功能的bean,我们称之为业务类,第二种是与系统核心业务无关但同一时候又提供十分重要服务bean,我们称之为服务类.业务类的bean ...

  8. Flask上下文管理机制

    前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...

  9. shopping car 3.0

    #!/usr/bin/env python# -*- coding: utf-8 -*-# @File : 购物车3.0.py# @Author: Anthony.waa# @Date : 2018/ ...

  10. Servlet基础(二)

    1.什么是Servlet   java类,提供web形式的访问   servlet就是按照javaee中servlet规范所编写的java类   能够被浏览器通过URL形式访问到 2.怎么在javae ...