2020ICPC·小米 网络选拔赛第一场 D.Router Mesh
题目描述
In a Mesh networking system, there are n n_{} n MI Routers, where m m_{} m pairs of MI Routers are bidirectionally connected. In order to check the stability of the system, for each MI Router, we should determine the number of connected components after removing it and relative connections from the system. And then assess that if we should add more MI Routers into the system. Print the numbers of connected components in residual system after removing each MI Router.
输入描述:
The first line contains two integers n , m ( 1 ≤ n , m ≤ 3 × 1 0 5 ) n,m~(1\le n,m\le 3\times 10^5) n,m (1≤n,m≤3×105), denoting the number of MI Routers and bidirectional connections in the Mesh networking system.
Following m m_{} m lines each contains two integers u , v ( 1 ≤ u < v ≤ n ) u,v~(1 \le u < v \le n) u,v (1≤u<v≤n), denoting the u u_{} u-th MI Router has a bidirectional connection with the v v_{} v-th MI Router.
It’s guaranteed that there are no duplicated connections in input.
输出描述:
Print one line containing n n_{} n integers, where i i_{} i-th integer denotes the number of connected components after removing i i_{} i-th MI Router and relative connections.
示例1
输入
4 2
1 2
1 3
输出
3 2 2 1
Tarjan模改,可以用 Tarjan 算法计算出删除某一个点后联通块数量的变化,最后对每个点输出原来连通块的数量加上删点后的变化量即可,AC代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+5, M = 6e5+5;
int n, m;
int h[N], e[M], ne[M], idx,ans[N];
int dfn[N], low[N], timestamp;
int root;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++ timestamp;
int cnt = 0;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (!dfn[j])
{
tarjan(j);
low[u] = min(low[u], low[j]);
if (low[j] >= dfn[u]) cnt ++ ;
}
else low[u] = min(low[u], dfn[j]);
}
if (u != root) cnt ++ ;
ans[u]=cnt;
}
int main()
{
scanf("%d%d",&n,&m);
memset(dfn, 0, sizeof dfn);
memset(h, -1, sizeof h);
idx = timestamp = 0;
while (m -- )
{
int a, b;
scanf("%d%d",&a,&b);
add(a, b), add(b, a);
}
int cnt = 0;
for (root = 1; root <=n; root ++ ) {
if (!dfn[root]) {
cnt++;
tarjan(root);
}
}
for(int i=1;i<=n;i++) printf("%d ",cnt+ans[i]-1);
return 0;
}