题目描述

Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通。

输入

输入n<=100000 m<=500000及m条边

输出

输出n个数,代表如果把第i个点去掉,将有多少对点不能互通。

样例输入

5 5
1 2
2 3
1 3
3 4
4 5

样例输出

8
8
16
14
8


题解

DFS树

Tarjan求点双太暴力了。。。还是选择一个优雅一点的做法——DFS树

考虑在DFS的过程中计算删除每个点的答案。

由于DFS树没有横叉边,只有返祖边,因此一个点把整棵树分为一些部分(下面简称“块”):父树部分、子树部分。

考虑割掉一个点,什么样的“块”是连通的:由于没有横叉边,因此只有可能是子树“块”与父树“块”相连,然后它们可以连通。即只有所有与父树相连的连通块是相互连通的。

那么就相当于把能够连通的“块”看作1个“块”,于是任意两个不同“块”中的点都是不连通的。

于是就可以将总的点对数减去在同一“块”(这里的“块”指合并以后的)得到答案:$ANS_i=C_n^2-\sum C_{si}^2$。

最后答案需要乘以2,因为题目中的点对是有序点对,需要计算2次。

#include <cstdio>
#include <algorithm>
#define N 100010
#define M 1000010
#define C(n) (((ll)(n) * (n - 1)) >> 1)
using namespace std;
typedef long long ll;
struct edge
{
int to , next;
}a[M];
int n , head[N] , cnt , deep[N] , low[N] , si[N] , num[N];
ll sum[N];
inline void add(int x , int y)
{
a[++cnt].to = y , a[cnt].next = head[x] , head[x] = cnt;
}
void dfs(int x , int fa)
{
int i , ts = 0;
si[x] = 1;
for(i = head[x] ; i ; i = a[i].next)
{
if(a[i].to == fa) continue;
if(!deep[a[i].to])
{
deep[a[i].to] = low[a[i].to] = deep[x] + 1 , dfs(a[i].to , x) , low[x] = min(low[x] , low[a[i].to]) , si[x] += si[a[i].to];
if(low[a[i].to] < deep[x]) ts += si[a[i].to];
else sum[x] += C(si[a[i].to]);
}
else low[x] = min(low[x] , deep[a[i].to]);
}
sum[x] += C(ts + n - si[x]);
}
int main()
{
int m , x , y , i;
scanf("%d%d" , &n , &m);
ll s = C(n);
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
deep[1] = 1 , dfs(1 , 0);
for(i = 1 ; i <= n ; i ++ ) printf("%lld\n" , (s - sum[i]) << 1);
return 0;
}

【bzoj1123】[POI2008]BLO DFS树的更多相关文章

  1. 【dfs+连通分量】Bzoj1123 POI2008 BLO

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  2. BZOJ1123: [POI2008]BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 614  Solved: 235[Submit][Status] ...

  3. BZOJ1123:[POI2008]BLO(双连通分量)

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  4. bzoj1123 [POI2008]BLO——求割点子树相乘

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1123 思路倒是有的,不就是个乘法原理吗,可是不会写...代码能力... 写了一堆麻麻烦烦乱七 ...

  5. BZOJ1123 [POI2008]BLO(割点判断 + 点双联通缩点size)

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...

  6. [BZOJ1123]:[POI2008]BLO(塔尖)

    题目传送门 题目描述 Byteotia城市有n个towns.m条双向roads.每条road连接两个不同的towns,没有重复的road.所有towns连通. 输入格式 输入n,m及m条边. 输出格式 ...

  7. 【bzoj1123】BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2222  Solved: 1090[Submit][Status ...

  8. 【BZOJ-1123】BLO Tarjan 点双连通分量

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 970  Solved: 408[Submit][Status][ ...

  9. BZOJ 1123: [POI2008]BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1030  Solved: 440[Submit][Status] ...

随机推荐

  1. 使用Linux命名将代码上传到GitHub

    GitHub代码上传教程 https://my.oschina.net/baishi/blog/520791 这篇文章讲得挺清楚的,但是在上传的时候出现了问题 ! [rejected] master ...

  2. 利用login-path对MySQL安全加固

      Preface       Connection security is  one of the most important safety strategies which we should ...

  3. Linux-history的用法

    history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...] history的作用是显示或操纵历史 ...

  4. 【shell脚本学习-3】

    part-1 #!/bin/bash:<<FTP#test [ 1 -eq 2] #条件测试x="abc" #不允许有空格y="abc" [ &qu ...

  5. hadoop生态搭建(3节点)-01.基础配置

    # 基础配置# ==================================================================node1 vi /etc/hostname nod ...

  6. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...

  7. Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop

    I have learnt some experience about Big Data during my summer vocation,I was told that The first thi ...

  8. 环形链表II 142 使用快慢指针(C++实现)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. dijkstra算法学习

    dijkstra算法学习 一.最短路径 单源最短路径:计算源点到其他各顶点的最短路径的长度 全局最短路径:图中任意两点的最短路径 Dijkstra.Bellman-Ford.SPFA求单源最短路径 F ...

  10. flask与javascript及ajax

    flask与javascript及ajax 1.      flask+js 1.1.    最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...