HDU 5296 Annoying problem
Annoying problem
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 203 Accepted Submission(s): 60
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.
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
Case #1:
0
6
8
8
4
#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef unsigned long long ll; const int N = 233333;
int n, m, head[N], mm;
struct Edge
{
int to, next, w;
} e[N << 1];
void add(int u, int v, int w = 1)
{
e[mm].to = v;
e[mm].next = head[u];
e[mm].w = w;
head[u] = mm++;
}
int sz[N], dep[N];
int f[N][22]; /// f[i][j] 表示 i 的第 2^j 个祖先
int dfn[N]; ///dfs index
int cur;
int id[N]; /// you dfs xu qiu chu bian hao
int len[N];
void dfs(int u, int fa) /// 点从 1 開始标号
{
f[u][0] = fa;
sz[u] = 1;
dfn[u] = ++cur;
id[cur] = u;
for (int i=head[u]; ~i; i=e[i].next)
{
int v = e[i].to;
int w = e[i].w;
if (v != fa)
{
dep[v] = dep[u] + 1;
len[v] = len[u] + w;
dfs(v, u);
sz[u] += sz[v];
}
}
}
int maxh;
void gao()
{
cur = 0;
dep[0] = -1;
len[1] = dep[1] = 0;
f[1][0] = 1;
dfs(1, 0);f[1][0] = 1;
int j;
for (j=1; (1<<j)<n; j++)
for (int i=1; i<=n; i++)
f[i][j] = f[f[i][j-1]][j-1];
maxh = j - 1;
}
int swim(int x, int k)
{
for (int i=0; i<=maxh; i++)
if (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<int> se;
set<int>::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<n-1; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
gao();
printf("Case #%d:\n", ca++);
se.clear();
int ans = 0;
while (Q--)
{
int op, u;
scanf("%d%d", &op, &u);
u = dfn[u];
if (op==1)
{
it = se.find(u);
if (it==se.end())
{
ans += solve(u);
se.insert(u);
}
}
else
{
it = se.find(u);
if (it != se.end())
{
se.erase(u);
ans -= solve(u);
}
}
printf("%d\n", ans);
}
}
return 0;
}
HDU 5296 Annoying problem的更多相关文章
- 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+树状数组
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- 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 ...
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDOJ 5296 Annoying problem LCA+数据结构
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...
- 【HDOJ】5296 Annoying problem
LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] ...
- 2015 Multi-University Training Contest 1 - 1009 Annoying problem
Annoying problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Mean: 给你一个有根树和一个节点集合 ...
随机推荐
- [Lydsy1806月赛] 质数拆分
(mmp我已经不知道是第几次写NTT被卡了) 可以发现质数个数是 N/log(N) 级别的,1.5*10^5之内也只有 10000 多一点质数. 所以我们第一层暴力卷积,常数可以优化成 1/2. 然后 ...
- HDU 6035 Colorful Tree(补集思想+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...
- 【推导】【找规律】【二分】hdu6154 CaoHaha's staff
题意:网格图.给你一个格点多边形的面积,问你最少用多少条边(可以是单位线段或单位对角线),围出这么大的图形. 如果我们得到了用n条边围出的图形的最大面积f(n),那么二分一下就是答案. n为偶数时,显 ...
- 【矩阵乘法】Gym - 101412C - One-Dimensional Cellular Automaton
给你一个一维细胞自动机,第i个格子在时刻t的状态是这样获得的,问你t时刻的状态. 把0时刻的状态视作一个列向量,发现状态转移其实是一个n*n的矩阵(以n=5为例), B C A B C ...
- 1.1(JavaScript学习笔记)、JavaScript基础
一.JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型. 它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端 ...
- Problem H: 零起点学算法87——打印所有低于平均分的分数
#include<stdio.h> int main(){ ],b[]; while(scanf("%d",&n)!=EOF){ ; ;i<n;i++){ ...
- JS操作小数运算,结果莫名其妙出现多位小数问题
Number类型: Number类型是ECMAScript中最常用和最令人关注的类型了:这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被成为双精度数值),为支持各种数据类型 ...
- 改变element-ui滚动条设置,
基于vue的滚动条组件之--element隐藏组件滚动条scrollbar使用 在项目中,总是需要用到滚动条,但windows浏览器默认的滚动条是很丑的,为了页面美观,可以考虑优化滚动条样式. vu ...
- C#读取Windows日志
管理-->事件查看器 可以查看[应用程序].[安全].[系统]等分类的日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
- Tesseract训练中文字体识别
注:目前仅说明windows下的情况 前言 网上已经有大量的tesseract的识别教程,但是主要有两个缺点: 大多数比较老,有部分内容已经不适用. 大部分只是就英文的训练进行探索,很少针对中文的训练 ...