小米热身赛D.Router Mesh(Tarjan求点双)

 

 题目大意:

给定一个无向图,有n个点,m条边,依次去掉每一个点(仅删掉这一个点)后,原图中的连通分量个数。

对于每个点记录它属于多少个bcc。

// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 3e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 3;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Edge
{
    int next, to;
} e[maxn << 1];
int head[maxn], dfn[maxn], low[maxn], sta[maxn];
int bcc[maxn]; // 记录这个点属于多少个 BCC
int n, m, tim, k, top, cnt;
bool cut[maxn];

void add(int u, int v)
{
    e[k].next = head[u];
    e[k].to = v;
    head[u] = k++;
}
void Tarjan(int u, int fa)
{
    sta[++top] = u;
    dfn[u] = low[u] = ++tim;
    int child = 0;
    bool flag = false;
    for (int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].to;
        if (v == fa)
            continue;
        if (!dfn[v])
        {
            child++;
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if (low[v] >= dfn[u])
            {
                flag = true;
                bcc[u]++;
                while (top && sta[top] != u)
                {
                    bcc[sta[top]]++;
                    // cout << sta[top] << endl;
                    top--;
                }
            }
        }
        else
            low[u] = min(low[u], dfn[v]);
    }
    if (fa == 0)
    {
        if (child > 1)
            cut[u] = true;
    }
    else if (flag)
        cut[u] = true;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    int x, y;
    memset(head, -1, sizeof head);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        add(x, y);
        add(y, x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (!dfn[i])
        {
            cnt++;
            Tarjan(i, 0);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (cut[i])
            cout << cnt + bcc[i] - 1 << " ";
        else if (head[i] != -1)
            cout << cnt << " ";
        else
            cout << cnt - 1 << " ";
    }
    // cout << endl;
    // while (top)
    // {
    //     cout << "qwe " << sta[top] << endl;
    //     top--;
    // }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值