Solution -「CF 855G」Harry Vs Voldemort
\(\mathcal{Description}\)
Link.
给定一棵 \(n\) 个点的树和 \(q\) 次加边操作。求出每次操作后,满足 \(u,v,w\) 互不相等,路径 \((u,w)\) 与 \((v,w)\) 无重复边的有序三元组 \((u,v,w)\) 的个数。
\(n,q\le10^5\)。
\(\mathcal{Solution}\)
考虑原树上,以某个点为 \(w\) 的贡献。记 \(\operatorname{contr}(u)\) 为 \(u\) 的贡献,则有:
\]
又发现一个边双中的每个点都应是等价的。所以对于以 \(u\) 为顶点的边双,维护 \(val_u=\sum_{v\in son_u}siz_v^2\) 和大小 \(s_u\),我们也能求出它的贡献:
\]
加边时,暴力爬树,并用并查集维护连通边双即可。
复杂度 \(\mathcal O(n\log n)\)(并查集不带启发式合并)。
\(\mathcal{Code}\)
#include <cstdio>
#include <assert.h>
typedef long long LL;
const int MAXN = 1e5;
int n, ecnt, head[MAXN + 5];
int fa[MAXN + 5], dep[MAXN + 5], siz[MAXN + 5], blk[MAXN + 5];
LL ans, val[MAXN + 5];
struct Edge { int to, nxt; } graph[MAXN * 2 + 5];
inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
}
inline char fgc () {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread ( p = buf, 1, 1 << 17, stdin ), p == q ) ? EOF : *p ++;
}
inline int rint () {
int x = 0; char d = fgc ();
for ( ; d < '0' || '9' < d; d = fgc () );
for ( ; '0' <= d && d <= '9'; d = fgc () ) x = x * 10 + ( d ^ '0' );
return x;
}
inline void wint ( const LL x ) {
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
struct DSU {
int fa[MAXN + 5];
inline void init () { for ( int i = 1; i <= n; ++ i ) fa[i] = i; }
inline int find ( const int x ) { return x ^ fa[x] ? fa[x] = find ( fa[x] ) : x; }
inline bool unite ( int x, int y ) {
x = find ( x ), y = find ( y );
return x ^ y ? fa[x] = y, true : false;
}
} dsu;
inline void init ( const int u ) {
siz[u] = blk[u] = 1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fa[u] ) {
dep[v] = dep[fa[v] = u] + 1, init ( v );
siz[u] += siz[v];
val[u] += 1ll * siz[v] * siz[v];
}
}
}
inline void calc ( const int u, const int k ) {
assert ( u == dsu.fa[u] );
int s = blk[u];
ans += 1ll * k * s * ( 1ll * ( n - s ) * ( n - s ) - val[u] - 1ll * ( n - siz[u] ) * ( n - siz[u] ) );
ans += 2ll * k * s * ( s - 1 ) * ( n - s );
ans += 1ll * k * s * ( s - 1 ) * ( s - 2 );
}
inline void merge ( const int u, const int v ) {
assert ( u == dsu.fa[u] && v == dsu.fa[v] && dep[u] < dep[v] );
calc ( u, -1 ), calc ( v, -1 );
val[u] -= 1ll * siz[v] * siz[v], val[u] += val[v], blk[u] += blk[v];
calc ( u, 1 ), dsu.unite ( v, u );
}
int main () {
n = rint (), dsu.init ();
for ( int i = 1, u, v; i < n; ++ i ) {
u = rint (), v = rint ();
link ( u, v ), link ( v, u );
}
init ( 1 );
for ( int i = 1; i <= n; ++ i ) calc ( i, 1 );
wint ( ans ), putchar ( '\n' );
for ( int q = rint (), u, v; q --; ) {
u = rint (), v = rint ();
while ( dsu.find ( u ) ^ dsu.find ( v ) ) {
if ( dep[dsu.find ( u )] < dep[dsu.find ( v )] ) u ^= v ^= u ^= v;
u = dsu.find ( u );
merge ( dsu.find ( fa[u] ), u );
}
wint ( ans ), putchar ( '\n' );
}
return 0;
}
Solution -「CF 855G」Harry Vs Voldemort的更多相关文章
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
随机推荐
- Tomcat8/9的catalina.out中文乱码问题解决
OS: Red Hat Enterprise Linux Server release 7.8 (Maipo) Tomcat: 9 中文显示为???问号 在$CATALINA_HOME/conf下的l ...
- 基于Apache Hudi + Flink的亿级数据入湖实践
本次分享分为5个部分介绍Apache Hudi的应用与实践 实时数据落地需求演进 基于Spark+Hudi的实时数据落地应用实践 基于Flink自定义实时数据落地实践 基于Flink+Hudi的应用实 ...
- SYCOJ2140祝福短信
题目-祝福短信 (shiyancang.cn) 1 #include<bits/stdc++.h> 2 using namespace std; 3 map<string,bool& ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- 【分享数据】vm-insert的压缩比达到29倍
vm-insert采用remote-write的http协议来接收metric数据,然后按照一定算法转发到vm-storage群集. vm-insert到vm-storage这里是用了自己的二进制协议 ...
- 【记录一个问题】opencv中 cv::dft()与cv::ocl_dft()计算的结果相差较大
以一个跟踪算法来测试: 使用cv::dft(), 矩阵未按照2次幂对齐,最终跟踪平均准确率 84.3% 使用cv::dft(),矩阵使用cv::copyMakeBorder对齐,最终跟踪平均准确率 8 ...
- Cesium参考资源
Reference resources cesium官网 cesium 下载 cesium官方文档 APIs cesium-workshop github cesium 官方示例 cesium git ...
- 学习Java第9天
今天所作的工作: 反射,枚举类型与泛型 明天工作: 1.线程 2.网络通信 所遇到的问题及解决方法: 反射基本思想,泛型类似于类模板. 理解反射太难了,转悠了好半天,关键是理解反射的思想,才容易学.
- Python 序列类型小结
序列是python中最基本的数据结构. 序列中每一个元素都有其对应的索引,索引是从0开始,0,1,2......依次类推 python中的序列类型有:字符串str.列表list.元组tuple.Uni ...
- plsql 触发器介绍 语句级别触发器、行级别触发器。
/* 分类: 1.DDL触发器 执行create,alter,drop操作时,会激活的触发器 2.DML触发器 执行增.删除.修改时,激活的触发器 3.系统事件触发器 执行特定的系统事件时(启动.加载 ...