博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5296 Annoying problem
阅读量:6872 次
发布时间:2019-06-26

本文共 2506 字,大约阅读时间需要 8 分钟。

Annoying problem

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


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
 

#include 
using namespace std;#define prt(k) cerr<<#k" = "<
<
> i & 1) x = f[x][i]; return x;}int LCA(int x, int y){ if (dep[x] > dep[y]) swap(x, y); ///dep[x] <= dep[y]; y = swim(y, dep[y] - dep[x]); if (x == y) return y; for (int i=maxh; i>=0; i--) { if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; } return f[x][0];}int Q;set
se;set
::iterator it;int dist(int x, int y){ int lca = LCA(x, y); return len[x] - len[lca] + len[y] - len[lca];}int solve(int u){ if (se.empty()) return 0; int x, y; int t = *se.begin(); it = se.lower_bound( u); y = *it; it--; x = *(it ); int t2 = *se.rbegin(); x = id[x]; y = id[y]; if (t2 < u || t > u) { x = id[t]; y = id[t2]; } u = id[u]; return len[u] - len[LCA(x,u) ] - len[LCA(y,u)] + len[LCA(x,y) ];}int main(){ int re; cin>>re; int ca=1; while (re--) { cin>>n>>Q; mm = 0; memset(head,-1,sizeof head); for (int i=0; i

转载地址:http://hgsfl.baihongyu.com/

你可能感兴趣的文章
ORA-09925: Unable to create audit trail file Linux-x86_64
查看>>
如何跳出嵌套语句之return
查看>>
API概述
查看>>
python2.6 安装rsa的包
查看>>
undo表空间使用率过高,且迟迟不释放问题
查看>>
scons *** no sconstruct file found求解决办法
查看>>
BIND基础配置详解
查看>>
火狐增加安全端口,每次用都得查,好麻烦,自己记录一下
查看>>
c# 多线程排队队列实现的源码
查看>>
LDA入门与Java实现
查看>>
19_css背景控制.html
查看>>
计算机网络测试和故障诊断的发展
查看>>
Delphi 与 DirectX 之 DelphiX(29): TDIB.AddMonoNoise();
查看>>
Windows Server 2008 FTP用户目录隔离模式
查看>>
zookeeper-kafka环境搭建,生产者消费者终端测试
查看>>
Catnut 微博app第一个版本发布了
查看>>
python实现linux下指定目录下文件中的单词个数统计
查看>>
SQL SERVER存储过程中如何使用事务与try catch
查看>>
我的友情链接
查看>>
常见算法的记录
查看>>