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 树当 ...
随机推荐
- js基础-运算符
100 * "20" 字符串转数字 5 * "ss" NAN "ss" 转数字返回NAN 任何数字与NAN +-*/ 都返回NAN 5/N ...
- yii添加验证码 和重复密码
<?phpnamespace frontend\models; use common\models\User;use yii\base\Model;use Yii; /** * Signup f ...
- ssl 的jks 生成工具
https://www.myssl.cn/tools/merge-jks-cert.html 通过key 私钥 ,和公钥pem 生成jks
- SpringBoot 热启动
在开发过程中,当写完一个功能我们需要运行应用程序测试,可能这个小功能中存在多个小bug,我们需要改正后重启服务器,这无形之中拖慢了开发的速度增加了开发时间,SpringBoot提供了spring-bo ...
- 引用yml中自定义数据 静态引用和动态引用
//静态 @Component public class LinusFile { public static String imageUrl; @Value("${web.uploadPat ...
- pycharm破解版
- Swagger2
参考文档:https://www.jianshu.com/p/5ae7267385b9 官网:https://swagger.io/ 注解参考:https://blog.csdn.net/weixin ...
- overflow属性的用法
<style type="text/css">div{ background-color:#00FFFF; width:150px; height:150px; ove ...
- MySQL之多表查询练习 与基本查询基础
MySQL 增删查改 一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into]<表名> [列名] values <列值> 例:insert i ...
- SQLite4Unity3d
What's this? When I started with Unity3d development I needed to use SQLite in my project and it was ...