BZOJ原题链接

洛谷原题链接

若第\(i\)个点不是割点,那么只有这个点单独形成一个连通块,其它点依旧连通,则答案为\(2\times (n-1)\)。

若第\(i\)个点是割点,那么去掉这个点相关的边就会形成\(3\)种构成的连通块:

  1. 由点\(i\)本身构成。
  2. 由点\(i\)的子树(搜索树中)形成若干个连通块。
  3. 由除点\(i\)及其子树的所有其它点构成一个连通块。

于是我们可以在用\(tarjan\)找割点的过程中计算搜索树中每棵子树的大小,并统计答案即可。

  1. #include<cstdio>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 1e5 + 10;
  5. const int M = 5e5 + 10;
  6. int fi[N], di[M << 1], ne[M << 1], dfn[N], low[N], si[N], l, ti, n;
  7. bool v[N];
  8. ll S[N];
  9. inline int re()
  10. {
  11. int x = 0;
  12. char c = getchar();
  13. bool p = 0;
  14. for (; c < '0' || c > '9'; c = getchar())
  15. p |= c == '-';
  16. for (; c >= '0' && c <= '9'; c = getchar())
  17. x = x * 10 + (c - '0');
  18. return p ? -x : x;
  19. }
  20. inline void add(int x, int y)
  21. {
  22. di[++l] = y;
  23. ne[l] = fi[x];
  24. fi[x] = l;
  25. }
  26. inline int minn(int x, int y)
  27. {
  28. return x < y ? x : y;
  29. }
  30. void tarjan(int x)
  31. {
  32. int i, y, s = 0, g = 0;
  33. dfn[x] = low[x] = ++ti;
  34. si[x] = 1;
  35. for (i = fi[x]; i; i = ne[i])
  36. {
  37. y = di[i];
  38. if (!dfn[y])
  39. {
  40. tarjan(y);
  41. si[x] += si[y];
  42. low[x] = minn(low[x], low[y]);
  43. if (low[y] >= dfn[x])
  44. {
  45. g++;
  46. S[x] += 1LL * si[y] * (n - si[y]);
  47. s += si[y];
  48. if (x ^ 1 || g > 1)
  49. v[x] = 1;
  50. }
  51. }
  52. else
  53. low[x] = minn(low[x], dfn[y]);
  54. }
  55. if (v[x])
  56. S[x] += 1LL * (n - 1 - s) * (s + 1) + n - 1;
  57. else
  58. S[x] = (n - 1) << 1;
  59. }
  60. int main()
  61. {
  62. int i, x, y, m;
  63. n = re();
  64. m = re();
  65. for (i = 1; i <= m; i++)
  66. {
  67. x = re();
  68. y = re();
  69. add(x, y);
  70. add(y, x);
  71. }
  72. tarjan(1);
  73. for (i = 1; i <= n; i++)
  74. printf("%lld\n", S[i]);
  75. return 0;
  76. }

BZOJ1123或洛谷3469 [POI2008]BLO-Blockade的更多相关文章

  1. 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】

    题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...

  2. 洛谷 P3478 [POI2008]STA-Station

    题目描述 The first stage of train system reform (that has been described in the problem Railways of the ...

  3. 洛谷 P3477 [POI2008]PER-Permutation 解题报告

    P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...

  4. 洛谷P3478 [POI2008]STA-Station

    P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...

  5. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  6. 洛谷 P3469 [POI2008]BLO-Blockade (Tarjan,割点)

    P3469 [POI2008]BLO-Blockade https://www.luogu.org/problem/P3469 题目描述 There are exactly nn towns in B ...

  7. 洛谷P3469[POI2008]BLO-Blockade

    题目 割点模板题. 可以将图中的所有点分成两部分,一部分是去掉之后不影响图的连通性的点,一部分是去掉之后影响连通性的点,称其为割点. 然后分两种情况讨论,如果该点不是割点,则最终结果直接加上2*(n- ...

  8. 割点判断+luogu 3469 POI2008 BLO

    1.根节点,有2棵及以上子树 2.非根节点,有子节点dfn[u]<=low[v] #include <bits/stdc++.h> #define N 1000050 using n ...

  9. 【洛谷P3469】BLO

    题目大意:给定 N 个点,M 条边的联通无向图,求出对于每个点来说,将与这个点相连的所有边都去掉后,会少多少个联通的点对 (x,y). 题解:连通性问题从 DFS 树的角度进行考虑.对于 DFS 树当 ...

随机推荐

  1. js基础-运算符

    100 * "20" 字符串转数字 5 * "ss"  NAN "ss" 转数字返回NAN 任何数字与NAN +-*/ 都返回NAN 5/N ...

  2. yii添加验证码 和重复密码

    <?phpnamespace frontend\models; use common\models\User;use yii\base\Model;use Yii; /** * Signup f ...

  3. ssl 的jks 生成工具

    https://www.myssl.cn/tools/merge-jks-cert.html 通过key 私钥 ,和公钥pem 生成jks

  4. SpringBoot 热启动

    在开发过程中,当写完一个功能我们需要运行应用程序测试,可能这个小功能中存在多个小bug,我们需要改正后重启服务器,这无形之中拖慢了开发的速度增加了开发时间,SpringBoot提供了spring-bo ...

  5. 引用yml中自定义数据 静态引用和动态引用

    //静态 @Component public class LinusFile { public static String imageUrl; @Value("${web.uploadPat ...

  6. pycharm破解版

  7. Swagger2

    参考文档:https://www.jianshu.com/p/5ae7267385b9 官网:https://swagger.io/ 注解参考:https://blog.csdn.net/weixin ...

  8. overflow属性的用法

    <style type="text/css">div{ background-color:#00FFFF; width:150px; height:150px; ove ...

  9. MySQL之多表查询练习 与基本查询基础

    MySQL  增删查改 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into]<表名> [列名] values <列值> 例:insert i ...

  10. SQLite4Unity3d

    What's this? When I started with Unity3d development I needed to use SQLite in my project and it was ...