一本通1525:电力

博客围绕图论和算法展开,运用C++语言进行相关实现。图论在信息技术领域有重要应用,结合C++语言能高效处理相关问题,可用于解决多种实际场景中的算法需求。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ri register
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;
inline int rd() {
    int x = 0, y = 1;
    char c = getchar();

    for (; c < '0' || c > '9'; c = getchar())
        if (c == '-')
            y = -1;

    for (; c >= '0' && c <= '9'; c = getchar())
        x = (x << 1) + (x << 3) + (c ^ 48);

    return x * y;
}
const int N = 1e4 + 5;
int siz[N], n, m, u, v, cnt, num[N], bar[N], dfn[N], low[N], tot, ans[N];
vector<int>e[N];
void dfs(int x) {
    num[x] = cnt;
    bar[cnt]++;

    for (auto i : e[x])
        if (!num[i])
            dfs(i);
}
void tarjan(int x) {
    dfn[x] = low[x] = ++tot;
    siz[x] = 1;
    int r = bar[num[x]] - 1;

    for (auto i : e[x])
        if (dfn[i])
            low[x] = min(low[x], dfn[i]);
        else {
            tarjan(i);
            siz[x] += siz[i];
            low[x] = min(low[x], low[i]);

            if (low[i] >= dfn[x])
                ans[x]++, r -= siz[i];
        }

    ans[x] += r > 0;
}
signed main() {
    while (1) {
        cnt = tot = 0;
        memset(num, 0, sizeof num);
        memset(bar, 0, sizeof bar);
        memset(low, 0, sizeof low);
        memset(dfn, 0, sizeof dfn);
        memset(siz, 0, sizeof siz);
        memset(ans, 0, sizeof ans);
        n = rd();
        m = rd();

        for (int i = 1; i <= n; i++)
            e[i].clear();

        if (!n && !m)
            break;

        while (m--)
            u = rd() + 1, v = rd() + 1, e[u].push_back(v), e[v].push_back(u);

        for (int i = 1; i <= n; i++)
            if (!num[i])
                ++cnt, dfs(i);

        for (int i = 1; i <= n; i++)
            if (!dfn[i])
                tarjan(i);

        int s = 0;

        for (int i = 1; i <= n; i++)
            s = max(s, ans[i] + cnt - 1);

        cout << s << '\n';
    }

    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值