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
1 2
2 3
1 3
3 4
4 5
Sample Output
8
8
16
14
8
8
16
14
8
HINT
Source
分析
如果一个点不是割点,那么删去后不会对其他点之间的连通性造成影响;如果是一个割点,影响只和其连接的几个块的大小有关。因此Tarjan求割点的同时注意维护子树大小即可。
代码
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> using namespace std; #define N 5000000
#define LL long long int n, m; LL ans[N]; int hd[N], to[N], nt[N], tot; int dfn[N], low[N], tim; int tarjan(int u, int f)
{
dfn[u] = low[u] = ++tim; ans[u] = (n - ) << ; int cnt = , siz; LL sum = , tmp = ; for (int i = hd[u]; ~i; i = nt[i])if (f != to[i])
{
if (!dfn[to[i]])
{
siz = tarjan(to[i], u);
low[u] = min(low[u], low[to[i]]);
if (low[to[i]] >= dfn[u])
ans[u] += 2LL * siz * sum, sum += siz;
else
tmp += siz;
}
else
low[u] = min(low[u], dfn[to[i]]);
} ans[u] += 2LL * (n - (sum + )) * sum; return sum + tmp + ;
} signed main(void)
{
scanf("%d%d", &n, &m); memset(hd, -, sizeof(hd)), tot = ; for (int i = ; i <= m; ++i)
{
int x, y; scanf("%d%d", &x, &y); nt[tot] = hd[x]; to[tot] = y; hd[x] = tot++;
nt[tot] = hd[y]; to[tot] = x; hd[y] = tot++;
} memset(dfn, , sizeof(dfn)); tim = ; tarjan(, -); for (int i = ; i <= n; ++i)
printf("%lld\n", ans[i]);
}
BZOJ_1123.cpp
#include <cstdio> template <class T>
inline T min(const T &a, const T &b)
{
return a < b ? a : b;
} typedef long long lnt; const int mxn = ;
const int mxm = ; int n, m; int hd[mxn];
int to[mxm];
int nt[mxm]; int dfn[mxn];
int low[mxn]; lnt ans[mxn]; lnt tarjan(int u, int f)
{
static int tim = ; ans[u] = (n - ) << ;
dfn[u] = low[u] = ++tim; lnt siz, sum = , tmp = ; for (int i = hd[u], v; i; i = nt[i])
if ((v = to[i]) != f)
{
if (!dfn[v])
{
siz = tarjan(v, u); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u])
ans[u] += 2LL * sum * siz, sum += siz;
else
tmp += siz;
}
else
low[u] = min(low[u], dfn[v]);
} ans[u] += 2LL * (n - sum - ) * sum; return sum + tmp + ;
} signed main(void)
{
scanf("%d%d", &n, &m); for (int i = ; i < m; ++i)
{
static int x, y, tot; scanf("%d%d", &x, &y); nt[++tot] = hd[x], to[tot] = y, hd[x] = tot;
nt[++tot] = hd[y], to[tot] = x, hd[y] = tot;
} tarjan(, ); for (int i = ; i <= n; ++i)
printf("%lld\n", ans[i]);
}
@Author: YouSiki