当前位置:网站首页>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;
}
边栏推荐
- 设计分享|基于单片机的P0口驱动LED闪烁
- 域前置通信过程和溯源思路
- 机器学习建模高级用法!构建企业级AI建模流水线
- 每日一R「01」跟着大佬学 Rust
- grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
- 可以在易方达基金上买基金吗,安全吗
- (Codeforce 757)E. Bash Plays with Functions(积性函数)
- Introduction to Qt (4) - Continuous playback of pictures (the use of two timers)
- -Wl,--start-group ... -Wl,--end-group for resolving circular dependencies of several libraries
- (newcoder 15079)无关(容斥原理)
猜你喜欢
随机推荐
[GYCTF2020]Ezsqli-1|SQL注入
php convert timestamp to just, minutes ago, hours ago, days ago format
JSDay1-两数之和
grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
2021 RoboCom 世界机器人开发者大赛-本科组(决赛)7-4猛犸不上 Ban(最短路)
【LaTex异常与错误】 - 公式编号的参考引用命令\eqref发生错误Undefined control sequence——可能是因为没加载宏包amsmath
Small program figure display banner
域前置通信过程和溯源思路
动手写prometheus的exporter-01-Gauge(仪表盘)
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
每日一R「01」跟着大佬学 Rust
Porting ucos2 code from stm32 to GD32
Kubernetes 资源核心原理
如何搭建一套自己公司的知识共享平台
Virtual router redundancy protocol VRRP - double-machine hot backup
Hi3516 use wifi module
待完善:tf.name_scope() 和 tf.variable_scope()的区别
JS中的预编译(AO、GO详解)
sess.restore() 和 tf.import_meta_graph() 在使用时的一些关联
Introduction to Qt (5) - file operation, hotkey and mouse reading (implementation of txt window)