2020ICPC·小米 网络选拔赛第一场 D.Router Mesh

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 (1n,m3×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 (1u<vn), 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值