当前位置:网站首页>51nod 2877熟练使用tarjan的知识
51nod 2877熟练使用tarjan的知识
2022-08-08 23:29:00 【Lqingyyyy】

我一开始想到tarjan 没想到咋做 看了题解直呼妙阿
考虑一个点有多个子块 若子块中的点不能回到父节点 那么这个子块 就和 其他子块是不能互相到达的
这时候只需要记录 已经遍历的多少子块即可 这样不会重复
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 1e6 + 10,M = 5e5 + 10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
int head[N],to[M * 2],last[M * 2],cnt;
void add(int a,int b){
to[++cnt] = b;
last[cnt] = head[a];
head[a] = cnt;
}
int n,m;
ll dfn[N],low[N],times,sz[N],pn; ll ans[N];
void tarjan(int x){
dfn[x] = low[x] = ++times;
sz[x] = 1;
ll pn = 0;
for(int i = head[x]; i != -1; i = last[i]){
int j = to[i];
if(!dfn[j]){
tarjan(j);
sz[x] += sz[j];
if(dfn[x] <= low[j]){
ans[x] += sz[j] * pn;
pn += sz[j];
}
low[x] = min(low[x],low[j]);
}else low[x] = min(low[x],dfn[j]);
}
ans[x] += pn * (n - pn - 1);
}
int main(){
memset(head,-1,sizeof head);
cin >> n >> m;
for(int i = 1; i <= m; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
tarjan(1);
for(int i = 1; i <= n; i++){
cout << (ans[i] + n - 1) * 2 << endl;
}
return 0;
}
边栏推荐
- 【PP-YOLOv2】测试自定义的数据集
- 微信小程序开发一些函数使用方法
- STM8L LCD digital tube driver, thermometer LCD display
- (2022牛客多校五)B-Watches(二分)
- 【Verilog基础】关于芯片中信号串扰的理解
- 机器学习之知识点(一)
- Virtual router redundancy protocol VRRP - double-machine hot backup
- 【Verilog基础】PPA优化问题总结(含面积优化、速度优化)
- 树莓派wiringPi库的使用补充
- stm32 uses serial port to receive idle interrupt + dma to achieve variable length dma reception
猜你喜欢
随机推荐
PMP考点有哪些啊?
北斗网络同步时钟与GPS卫星时钟同步设备的区别
JSDay2-两个数组的交集
(2022牛客多校五)B-Watches(二分)
stm32 uses serial port to receive idle interrupt + dma to achieve variable length dma reception
使用Mongoose populate实现多表关联存储与查询,内附完整代码
JSDay2- 长度最小的子数组
2022牛客多校六 A-Array(构造+哈夫曼)
用工具实现 Mock API 的整个流程
Qt入门(五)——文件操作、热键和鼠标的读取(txt窗口的实现)
域前置通信过程和溯源思路
php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
makefile 自动编译 目录和子目录的 C文件
MySQL 高级知识【 MyISAM 读锁写锁限制】
Application Layer Protocol - RADIUS
meta learning
Virtual router redundancy protocol VRRP - double-machine hot backup
【PP-YOLOv2】训练自定义的数据集
2022牛客多校六 J-Number Game(简单推理)
树莓派wiringPi库的使用补充





![[GYCTF2020]Ezsqli-1|SQL注入](/img/02/dd53f409d278c85b0d48108c4c0148.png)



