BZOJ1123或洛谷3469 [POI2008]BLO-Blockade
BZOJ原题链接
洛谷原题链接
若第\(i\)个点不是割点,那么只有这个点单独形成一个连通块,其它点依旧连通,则答案为\(2\times (n-1)\)。
若第\(i\)个点是割点,那么去掉这个点相关的边就会形成\(3\)种构成的连通块:
- 由点\(i\)本身构成。
- 由点\(i\)的子树(搜索树中)形成若干个连通块。
- 由除点\(i\)及其子树的所有其它点构成一个连通块。
于是我们可以在用\(tarjan\)找割点的过程中计算搜索树中每棵子树的大小,并统计答案即可。
#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int M = 5e5 + 10;
int fi[N], di[M << 1], ne[M << 1], dfn[N], low[N], si[N], l, ti, n;
bool v[N];
ll S[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
void tarjan(int x)
{
int i, y, s = 0, g = 0;
dfn[x] = low[x] = ++ti;
si[x] = 1;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (!dfn[y])
{
tarjan(y);
si[x] += si[y];
low[x] = minn(low[x], low[y]);
if (low[y] >= dfn[x])
{
g++;
S[x] += 1LL * si[y] * (n - si[y]);
s += si[y];
if (x ^ 1 || g > 1)
v[x] = 1;
}
}
else
low[x] = minn(low[x], dfn[y]);
}
if (v[x])
S[x] += 1LL * (n - 1 - s) * (s + 1) + n - 1;
else
S[x] = (n - 1) << 1;
}
int main()
{
int i, x, y, m;
n = re();
m = re();
for (i = 1; i <= m; i++)
{
x = re();
y = re();
add(x, y);
add(y, x);
}
tarjan(1);
for (i = 1; i <= n; i++)
printf("%lld\n", S[i]);
return 0;
}
BZOJ1123或洛谷3469 [POI2008]BLO-Blockade的更多相关文章
- 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】
题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...
- 洛谷 P3478 [POI2008]STA-Station
题目描述 The first stage of train system reform (that has been described in the problem Railways of the ...
- 洛谷 P3477 [POI2008]PER-Permutation 解题报告
P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...
- 洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...
- 洛谷 P3467 [POI2008]PLA-Postering
P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...
- 洛谷 P3469 [POI2008]BLO-Blockade (Tarjan,割点)
P3469 [POI2008]BLO-Blockade https://www.luogu.org/problem/P3469 题目描述 There are exactly nn towns in B ...
- 洛谷P3469[POI2008]BLO-Blockade
题目 割点模板题. 可以将图中的所有点分成两部分,一部分是去掉之后不影响图的连通性的点,一部分是去掉之后影响连通性的点,称其为割点. 然后分两种情况讨论,如果该点不是割点,则最终结果直接加上2*(n- ...
- 割点判断+luogu 3469 POI2008 BLO
1.根节点,有2棵及以上子树 2.非根节点,有子节点dfn[u]<=low[v] #include <bits/stdc++.h> #define N 1000050 using n ...
- 【洛谷P3469】BLO
题目大意:给定 N 个点,M 条边的联通无向图,求出对于每个点来说,将与这个点相连的所有边都去掉后,会少多少个联通的点对 (x,y). 题解:连通性问题从 DFS 树的角度进行考虑.对于 DFS 树当 ...
随机推荐
- border&background1
1.border-radius border-top-left-radius:10px; = border-top-left-radius:10px 10px; (水平10px 竖直10px 被正圆切 ...
- 第五周特种JAVA健民欧巴分享经验
1.int为原始数据类型,没有特殊含义: 另一个为引用类型:该类型定义后可以开辟内存空间: 2.特殊值null:代表一个对象变量不引用任何对象//null不是为空,就是什么都没有. 3. “==” ...
- pipy国内镜像的网址
pipy国内镜像目前有:豆瓣 http://pypi.douban.com/simple/阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https: ...
- Mysql 表约束 非空、唯一、主键、自增长、默认、外键约束(基础6)
非空(not null).唯一(unique key).主键(primary key).自增长(auto_increment).默认约束(default) 准备基础环境: mysql> crea ...
- 八、AbstractFactory 抽象工厂模式
设计原理: 代码清单: 抽象 Factory : public abstract class Factory { public static Factory getFactory(String cla ...
- php拓展
https://github.com/phalcon/zephirhttp://blog.csdn.net/black_OX/article/details/43700707
- 树的子结构(python)
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # -*- coding:utf-8 -*- # class TreeNode: # def __ ...
- python基础入门学习简单程序练习
1.简单的乘法程序 i = 256*256 print('The value of i is', i) 运行结果: The value of i is 65536 2.执行python脚本的两种方式 ...
- Appium+python自动化1-环境搭建
一.前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.小编擅长Python,所以接下来的教程是appium+ ...
- git 忽略 IntelliJ .idea文件
git 忽略 IntelliJ .idea文件 2016年10月22日 11:31:27 阅读数:6196 标签: git 更多 个人分类: git 版权声明:本文为博主原创文章,未经博主允许不得 ...