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
解题方法:
不懂Tarjan求割点的可以看这个,Tarjan求割点
话说为啥cout输出让我无限re啊,囧。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int maxm = 1000005;
int n, m, head[maxn], edgecnt, ind, low[maxn], dfn[maxn], siz[maxn];
LL ans[maxn];
struct edge{int v, nxt; } E[maxm];
void init(){
memset(head, -1, sizeof(head)); edgecnt = ind = 0;
}
void addedge(int u, int v){
E[edgecnt].v = v, E[edgecnt].nxt = head[u], head[u] = edgecnt++;
}
void tarjan(int x)
{
int t = 0;
siz[x] = 1;
low[x] = dfn[x] = ++ind;
for(int i = head[x]; ~i; i = E[i].nxt){
int v = E[i].v;
if(dfn[v]){
low[x] = min(low[x], dfn[v]);
}
else{
tarjan(v);
siz[x] += siz[v];
low[x] = min(low[x], low[v]);
if(dfn[x] <= low[v]){
ans[x] += (LL) t * siz[v];
t += siz[v];
}
}
}
ans[x] += (LL) t * (n - t - 1);
}
int main(){
init();
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++){
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
tarjan(1);
for(int i = 1; i <= n; i++){
printf("%lld\n", (ans[i] + n - 1) * 2);
}
return 0;
}