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. C#--职业路线图

    非常好的一个C#的职业技术路线图

  2. HDU 4133

    注意题目中的一句话:If a number m has bigger evaluating value than all the numbers smaller than it... 这让我重新想过反 ...

  3. bram和dram差别

    选择distributed memory generator和block memorygenerator标准: Dram和bram差别: 1.bram 的输出须要时钟,dram在给出地址后既可输出数据 ...

  4. exFAT格式

    买了一个64GB的T卡,发如今Windows XP上格式化."文件系统"仅仅有exFAT选项. 用这个exFAT格式化还失败了. 给XP打上KB955704补丁,能够用exFAT格 ...

  5. 解决Unity的 The file &#39;MemoryStream&#39; is corrupted! Remove it and launch 崩溃问题

    孙广东   2015.7.30 问题:   在项目平时删除资源或者脚本资源时产生的prefab的脚本引用丢失,特别是在场景scene中丢了解决方式/// 1.又一次Clone项目/// 2.删除项目的 ...

  6. Deming管理系列(1)——开车仅仅看后视镜

    问题: 当业务经理被要求为未来的业务做计划时,他会提出一个自觉得不错的数字,而董事会往往希望能获得更大的收益,多次与其谈判.而业务经理在这方面不是新手,他有非常多可用的报告. 为什么不能让业务规划流程 ...

  7. oracle 11gR2 如何修改scan vip 地址 /etc/hosts方式

    这次帮客户搭建了一套oracle 11gR2 rac for aix环境,scan vip因为网络调整需要,需要更改以前设置好的scan vip,是采用/etc/hosts的方式,比如将scan vi ...

  8. Java-杂项:Java数组Array和集合List、Set、Map

    ylbtech-Java-杂项:Java数组Array和集合List.Set.Map 1.返回顶部 1. 之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊 ...

  9. BZOJ 4241 分块

    思路: 考虑分块 f[i][j]表示从第i块开头到j的最大值 cnt[i][j]表示从第i块开始到序列末尾j出现了多少次 边角余料处理一下就好啦~ //By SiriusRen #include &l ...

  10. COM基础

    为什么说COM的可重用性是建立在二进制级别? COM本身是语言无关,它的标准建立在二进制级别.对于使用COM组件的客户程序,它只需要要使用的COM对象信息就可以通过COM库的帮助创建和使用COM对象, ...