1123: [POI2008]BLO

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1030  Solved: 440
[Submit][Status][Discuss]

Description

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

Input

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

Output

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

Sample Input

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

Sample Output

8
8
16
14
8

HINT

 

Source

 

[Submit][Status][Discuss]

分析

如果一个点不是割点,那么删去后不会对其他点之间的连通性造成影响;如果是一个割点,影响只和其连接的几个块的大小有关。因此Tarjan求割点的同时注意维护子树大小即可。

代码

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. #define N 5000000
  11. #define LL long long
  12.  
  13. int n, m; LL ans[N];
  14.  
  15. int hd[N], to[N], nt[N], tot;
  16.  
  17. int dfn[N], low[N], tim;
  18.  
  19. int tarjan(int u, int f)
  20. {
  21. dfn[u] = low[u] = ++tim; ans[u] = (n - ) << ;
  22.  
  23. int cnt = , siz; LL sum = , tmp = ;
  24.  
  25. for (int i = hd[u]; ~i; i = nt[i])if (f != to[i])
  26. {
  27. if (!dfn[to[i]])
  28. {
  29. siz = tarjan(to[i], u);
  30. low[u] = min(low[u], low[to[i]]);
  31. if (low[to[i]] >= dfn[u])
  32. ans[u] += 2LL * siz * sum, sum += siz;
  33. else
  34. tmp += siz;
  35. }
  36. else
  37. low[u] = min(low[u], dfn[to[i]]);
  38. }
  39.  
  40. ans[u] += 2LL * (n - (sum + )) * sum;
  41.  
  42. return sum + tmp + ;
  43. }
  44.  
  45. signed main(void)
  46. {
  47. scanf("%d%d", &n, &m);
  48.  
  49. memset(hd, -, sizeof(hd)), tot = ;
  50.  
  51. for (int i = ; i <= m; ++i)
  52. {
  53. int x, y; scanf("%d%d", &x, &y);
  54.  
  55. nt[tot] = hd[x]; to[tot] = y; hd[x] = tot++;
  56. nt[tot] = hd[y]; to[tot] = x; hd[y] = tot++;
  57. }
  58.  
  59. memset(dfn, , sizeof(dfn)); tim = ; tarjan(, -);
  60.  
  61. for (int i = ; i <= n; ++i)
  62. printf("%lld\n", ans[i]);
  63. }

BZOJ_1123.cpp

  1. #include <cstdio>
  2.  
  3. template <class T>
  4. inline T min(const T &a, const T &b)
  5. {
  6. return a < b ? a : b;
  7. }
  8.  
  9. typedef long long lnt;
  10.  
  11. const int mxn = ;
  12. const int mxm = ;
  13.  
  14. int n, m;
  15.  
  16. int hd[mxn];
  17. int to[mxm];
  18. int nt[mxm];
  19.  
  20. int dfn[mxn];
  21. int low[mxn];
  22.  
  23. lnt ans[mxn];
  24.  
  25. lnt tarjan(int u, int f)
  26. {
  27. static int tim = ;
  28.  
  29. ans[u] = (n - ) << ;
  30. dfn[u] = low[u] = ++tim;
  31.  
  32. lnt siz, sum = , tmp = ;
  33.  
  34. for (int i = hd[u], v; i; i = nt[i])
  35. if ((v = to[i]) != f)
  36. {
  37. if (!dfn[v])
  38. {
  39. siz = tarjan(v, u);
  40.  
  41. low[u] = min(low[u], low[v]);
  42.  
  43. if (low[v] >= dfn[u])
  44. ans[u] += 2LL * sum * siz, sum += siz;
  45. else
  46. tmp += siz;
  47. }
  48. else
  49. low[u] = min(low[u], dfn[v]);
  50. }
  51.  
  52. ans[u] += 2LL * (n - sum - ) * sum;
  53.  
  54. return sum + tmp + ;
  55. }
  56.  
  57. signed main(void)
  58. {
  59. scanf("%d%d", &n, &m);
  60.  
  61. for (int i = ; i < m; ++i)
  62. {
  63. static int x, y, tot;
  64.  
  65. scanf("%d%d", &x, &y);
  66.  
  67. nt[++tot] = hd[x], to[tot] = y, hd[x] = tot;
  68. nt[++tot] = hd[y], to[tot] = x, hd[y] = tot;
  69. }
  70.  
  71. tarjan(, );
  72.  
  73. for (int i = ; i <= n; ++i)
  74. printf("%lld\n", ans[i]);
  75. }

@Author: YouSiki

BZOJ 1123: [POI2008]BLO的更多相关文章

  1. BZOJ 1123: [POI2008]BLO( tarjan )

    tarjan找割点..不是割点答案就是(N-1)*2, 是割点的话就在tarjan的时候顺便统计一下 ------------------------------------------------- ...

  2. bzoj 1123 [POI2008]BLO Tarjan求割点

    [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1540  Solved: 711[Submit][Status][Discu ...

  3. BZOJ 1123 [POI2008]BLO(Tarjan算法)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1123 [题目大意] Byteotia城市有n个towns,m条双向roads. 每条r ...

  4. bzoj 1123 [POI2008]BLO——点双连通分量

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1123 点双连通分量缩点,然后各种各样. 结果不会写了.比如新连边.记录一个点是割点缩成的点还 ...

  5. BZOJ 1123: [POI2008]BLO 求割点_乘法原理_计数

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

  6. BZOJ1123: [POI2008]BLO

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

  7. Bzoj 1131[POI2008]STA-Station (树形DP)

    Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...

  8. [POI2008]BLO(Tarjan)

    [POI2008]BLO Description Byteotia城市有\(n\)个 towns \(m\)条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所 ...

  9. BZOJ 1123 BLO

    tarjan求割点计算答案.注意不是每一棵子树都算答案.开个变量记一下. #include<iostream> #include<cstdio> #include<cst ...

随机推荐

  1. <转>SQL语句大全

    本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 一.基  础 1.说明:创建数据库CREATE DATABASE ...

  2. jmeter(一)基础介绍

    参考书籍:段念<软件性能测试与案例剖析>——第二版 推荐一本书<零成本实现web性能测试——基于Apache—jmeter>,主要内容是一些关于jmeter的实战使用,想学习的 ...

  3. tween.js

     简要教程 tween.js是一款可生成平滑动画效果的js动画库.相关的动画库插件还有:snabbt.js 强大的jQuery动画库插件和Tweene-超级强大的jQuery动画代理插件. tween ...

  4. 设置root用户不保存终端历史记录到.bash_history

    在.bashrc的最后行追加 unset HISTFILE cat .bash_history 还能看到 unset HISTFILE 之前保留的命令unset HISTFILE 之后的命令不会保留, ...

  5. 批量去除Teleport Pro整站下载文件冗余代码

    teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...

  6. BZOJ 4003 【JLOI2015】城池攻占

    Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, ...

  7. 将数据库备份到AZURE blob storage

    1创建一个Storage Account 1)点击Browse->Storage accounts 2) 填写Storage account,请记住这个名字,之后创建credential需要用到 ...

  8. Cannot resolve external dependency com.android.support:multidex:1.0.0

    multiDexEnabled true 去掉这一行,就OK了,是没有多dex支持么,但是又提供了这个

  9. struts2: config-browser-plugin 与 convention-plugin 学习

    struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了.convention-plugin. config-browser-plugin这二个 ...

  10. [转]linux 系统监控、诊断工具之 IO wait

    1.问题: 最近在做日志的实时同步,上线之前是做过单份线上日志压力测试的,消息队列和客户端.本机都没问题,但是没想到上了第二份日志之后,问题来了: 集群中的某台机器 top 看到负载巨高,集群中的机器 ...