ZOJ-3630-Information 解题报告

本文探讨了一个涉及城市间消息传递网络的强连通分量问题,通过采用Tarjan算法解决如何在摧毁特定城市后,使得剩余网络中强连通分量数量最多且规模最小的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       强连通分量题,看错好几次还以为是双连通分量求割点之类的题。题意:A国和B国打仗,B国有n个城市,每一个城市都有一个接收器和发射器用来传递消息。有些城市可能未发送任何消息,有些城市可能未接收任何消息。现在把那些可以互相直接或间接发送和接收消息的城市编成一个组,一个组中的任意两个成员都能互相直接或间接地发送和接收消息。每一个城市都最多只能属于一个组,并且每一个组的城市都至少是两个以上,否则不算一个组。现在A国想要摧毁B国的一个城市,使得摧毁之后B国城市数最多的那个组城市数最少并输出这个组的城市数。


       我的解题思路:初看好几遍还以为求割点割完之后成员最多的双连通分量。其实,这是一个有向图。Orz,求的是强连通分量权。这里用的方法其实很暴力,枚举每一个点作为摧毁点求最大强连通分量权,然后算出这些最大强连通分量权的最小值。


       我的解题代码:强连通分量Tarjan算法,不用缩点了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int N = 10001;
const int INF = 0x3f3f3f3f;

vector<int> e[N];
int dfn[N], low[N];
int stack[N], stop;
bool instack[N];
int timer, sccn;
int n, m;
int tempans, ans;   //中间答案和最终答案

void InitRead();

void DataProcess();

void Tarjan(int x, int z);  //z是枚举的摧毁掉的点

int main()
{
    while (~scanf("%d %d", &n, &m))
    {
        InitRead();
        DataProcess();
    }
    return 0;
}

void InitRead()
{
    timer = tempans = stop = sccn = 0;
    ans = INF;      //最终答案求最小值因此赋值为无穷大INF
    memset(dfn, 0, sizeof(dfn));
    memset(instack, false, sizeof(instack));
    for (int i=0; i<n; ++i)
    {
        e[i].clear();
    }
    int a, b;
    for (int i=0; i<m; ++i)
    {
        scanf("%d %d", &a, &b);
        e[a].push_back(b);
    }
    return;
}

void DataProcess()
{
    for (int i=0; i<n; ++i)
    {
        memset(dfn, 0, sizeof(dfn));
        tempans = 0;    //中间答案求最大值因此赋值为0
        for (int j=0; j<n; ++j)
        {
            if (!dfn[j] && j != i) Tarjan(j, i);
        }
        ans = min(ans, tempans);
    }
    printf("%d\n", ans > 1 ? ans : 0);  //如果无法成员数构成一个组则输出0
    return;
}

void Tarjan(int x, int z)
{
    dfn[x] = low[x] = ++timer;
    stack[stop++] = x;
    instack[x] = true;
    int size = e[x].size();
    int y;
    for (int i=0; i<size; ++i)
    {
        y = e[x][i];
        if (y == z) continue;   //已摧毁的点忽略掉
        if (!dfn[y])
        {
            Tarjan(y, z);
            low[x] = min(low[x], low[y]);
        }
        else if (instack[y])
        {
            low[x] = min(low[x], dfn[y]);
        }
    }
    if (dfn[x] == low[x])
    {
        sccn++;
        int temp = 0;
        do
        {
            y = stack[--stop];
            instack[y] = false;
            temp++;
        } while (x != y);
        tempans = max(tempans, temp);
    }
    return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值