当前位置:网站首页>【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
2022-07-04 15:18:00 【SSL_TJH】
Duathlon 铁人两项
题目链接:luogu P4630
题目大意
给你一个无向图,然后你可以按顺序选三个点 a,b,c,保证 a 可以到 b,b 可以到 c,而且存在方案使得这两个路径的交点只有 b。
然后问你有多少个满足的三元组。
思路
首先如果这个是森林的话那我们可以很好的搞。
(反正各种方法随便你)
那我们现在是一个图,那就不好搞了。
那我们考虑能不能把它变成树?于是就可以用圆方树了。
圆方树
在讲圆方树之前,让我们先给出一些定义:
点双连通图
如果是有向图,就有一个强连通图。那无向图就会有一个点双连通图和一个边双连通图。
边双连通子图这里没用到就不说了,我们给出点双连通的子图的定义:图中任意两不同点之间都有至少两条点不重复的路径。
(因为一个点的比较不好搞所以我们默认没有孤立点,如果题目中有就按照题目要求特殊处理)
然后你会发现有一个近似等价的定义是不存在割点的图。
发现反例只有两个点一条边的情况(或者你也可以认为不是反例,因为你起点终点一定要重复的)
点双连通分量
跟强连通分量一样,就是一个图的极大点双连通子图。
但是你如果画个图你不难发现就是一个点是可能在若干个点双中的,但是一个边只会在一个中。
圆方树
那在圆方树中,我们把原来的点看做原点,每个点双对应一个方点。
(不难看出方点数量最多为 n n n,所以记得总共要开 2 n 2n 2n)
然后一个方点向它点双里面的点(原点)连边。
然后这个图就变成了若干格菊花,每个菊花通过原图的割点连接起来。
然后你不难想象任意一套树上路径都是圆方点交错的。
建
考虑用类似 Tarjan 的方法。
那因为无向图,所以我们找到新点的时候更新 l o w low low 是跟 l o w low low 更新,找到原来的点的时候就要跟 d f n dfn dfn 更新。
那你会发现也是找环的顶端,那条件就是 d f n u = l o w x dfn_u=low_x dfnu=lowx(然后 u u u 就是最顶端)
然后你就可以建出来了。
这道题
那如果是树上的很好搞,我们考虑把环通过圆方树弄成树,然后看看方点那边要怎么处理。
然后我们其实会发现你一个点双里面任意两个点之间,对于其他的每个点都有一种方法可以走过。
(小小证明就是你可以把这个点双拉成一个环,然后就没啦)
然后你就发现如果你要经过一个方点,你里面的点都可以经过,那我们可以把方点的权值赋做它点双的大小,然后路径上的方点的权值和就是固定了 s , f s,f s,f 满足条件的 c c c 的个数。
然后你发现你算重了,因为相邻的两个方点之间是通过点连接起来的,所以每个点其实都会充当一个连接的位置,所以我们要把点的权值弄成 − 1 -1 −1。
然后这里是要求所有的,我们就小小树形 DP 一下,考虑每个点对所有包含他的路径的贡献即可。
(子树内部,子树和外面)
代码
#include<cstdio>
#include<vector>
#include<iostream>
#define ll long long
using namespace std;
const int N = 1e5 + 100;
int n, m;
vector <int> G[N];
struct YF_tree {
int dfn[N], low[N], sta[N], tot, deg[N << 1], fa[N << 1][21];
int val[N << 1], num, sz[N << 1];
vector <int> GG[N << 1];
ll ans;
void link(int x, int y) {
GG[x].push_back(y); GG[y].push_back(x);
}
void tarjan(int now) {
dfn[now] = low[now] = ++dfn[0]; sta[++sta[0]] = now; num++;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i];
if (!dfn[x]) {
tarjan(x); low[now] = min(low[now], low[x]);
if (dfn[now] == low[x]) {
tot++;
while (sta[sta[0]] != x) {
link(sta[sta[0]], tot);
sta[0]--; val[tot]++;
}
link(sta[sta[0]], tot); sta[0]--; val[tot]++;
link(now, tot); val[tot]++;
}
}
else low[now] = min(low[now], dfn[x]);
}
}
void dfs(int now, int father) {
if (now <= n) sz[now] = 1;
for (int i = 0; i < GG[now].size(); i++) {
int x = GG[now][i];
if (x != father) {
dfs(x, now);
ans += 2ll * sz[now] * sz[x] * val[now];
sz[now] += sz[x];
}
}
ans += 2ll * sz[now] * (num - sz[now]) * val[now];
}
void Init() {
tot = n; for (int i = 1; i <= n; i++) val[i] = -1;
for (int i = 1; i <= n; i++) if (!dfn[i]) num = 0, tarjan(i), dfs(i, 0);
}
}T;
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y);
G[x].push_back(y); G[y].push_back(x);
}
T.Init();
printf("%lld", T.ans);
return 0;
}
边栏推荐
- Detailed process of DC-2 range construction and penetration practice (DC range Series)
- 智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
- Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
- DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
- Principle and general steps of SQL injection
- Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
- [acwing] 58 weeks 4490 dyeing
- Four point probe Industry Research Report - market status analysis and development prospect prediction
- Go语言循环语句(第10课下)
- Accounting regulations and professional ethics [7]
猜你喜欢
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
How can programmers improve the speed of code writing?
Understand asp Net core - Authentication Based on jwtbearer
Transformer中position encoding实践
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
2022年国内云管平台厂商哪家好?为什么?
祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
Overflow: the combination of auto and Felx
Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
Readis configuration and optimization of NoSQL (final chapter)
随机推荐
Jump table instance
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
太方便了,钉钉上就可完成代码发布审批啦!
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
js中的数组筛选fliter
世界环境日 | 周大福用心服务推动减碳环保
FIREBIRD使用经验总结
Implement graph data construction task based on check point
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
如何为ONgDB核心项目源码做贡献
表单传递时,如何隐式将值传过去
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
Oracle监听器Server端与Client端配置实例
Practice: fabric user certificate revocation operation process
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
力扣今日题-1200. 最小绝对差
APOC自定义函数和过程
Redis 的内存淘汰策略和过期删除策略的区别
[acwing] 58 weeks 4490 dyeing