HDU 2767 Proving Equivalences(强连通分量)

本文介绍了解决HDU2767问题的方法,该问题是求最少添加多少条边使图变为强连通图。通过求解强连通分量并构造有向无环图,计算出度和入度为0的节点数,得到答案。

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

HDU 2767 Proving Equivalences(强连通分量)

http://acm.hdu.edu.cn/showproblem.php?pid=2767

题意:

        给你一个有向图,问你在图中最少要加多少条边能使得该图变成一个强连通图.

分析:

        首先我们求出该图的各个强连通分量,然后把每个强连通分量看出一个点(即缩点),然后我们得到了一个有向无环图(DAG).

        对于一个DAG,我们需要添加max(a,b)条边才能使其强连通.其中a为DAG中出度为0的点总数,b为DAG中入度为0的点总数.

        注意特殊情况:如果图已经强连通了,我们需要添加的边是0条,而不是1条.

可见刘汝佳 训练指南P322

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int maxn=20000+10;
int n,m;
vector<int> G[maxn];
stack<int> S;
int dfs_clock,scc_cnt;
int pre[maxn],low[maxn],sccno[maxn];
bool in0[maxn],out0[maxn];
void dfs(int u)
{
    pre[u]=low[u]=++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!sccno[v])
            low[u]=min(low[u],pre[v]);
    }
    if(low[u]==pre[u])//强连通分量起点
    {
        scc_cnt++;
        while(true)
        {
            int x= S.top(); S.pop();
            sccno[x]=scc_cnt;
            if(x==u) break;
        }
    }
}
void find_scc(int n)
{
    scc_cnt=dfs_clock=0;
    memset(pre,0,sizeof(pre));
    memset(sccno,0,sizeof(sccno));
    for(int i=0;i<n;i++)
        if(!pre[i]) dfs(i);
}
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) G[i].clear();
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u--, v--;
            G[u].push_back(v);
        }
        find_scc(n);
        for(int i=1;i<=scc_cnt;i++) in0[i]=out0[i]=true;
        for(int u=0;u<n;u++)
        {
            for(int i=0;i<G[u].size();i++)
            {
                int v=G[u][i];
                if(sccno[u] != sccno[v]) out0[sccno[u]]=in0[sccno[v]]=false;
            }
        }
        int a=0,b=0;
        for(int i=1;i<=scc_cnt;i++)
        {
            if(in0[i]) a++;
            if(out0[i]) b++;
        }
        int ans=max(a,b);
        if(scc_cnt==1) ans=0;
        printf("%d\n",ans);
    }
    return 0;
}


### 使用Tarjan算法计算强连通分量数量 #### 算法原理 Tarjan算法通过深度优先搜索(DFS)遍历有向图中的节点,记录访问顺序和低链值(low-link value),从而识别出所有的强连通分量。当发现一个节点的访问序号等于其最低可达节点编号时,表明找到了一个新的强连通分量。 #### 时间复杂度分析 该方法的时间效率取决于存储结构的选择。对于采用邻接表表示的稀疏图而言,整体性能更优,能够在线性时间内完成操作,即O(n+m)[^4];而针对稠密图则可能退化至平方级别(O())。 #### Python代码实现 下面给出一段Python程序用于演示如何基于NetworkX库构建并处理带权无环图(DAG),进而求解其中存在的全部SCC及其总数: ```python import networkx as nx def tarjan_scc(graph): index_counter = [0] stack = [] lowlinks = {} index = {} result = [] def strongconnect(node): # Set the depth index for this node to be the next available incrementing counter. index[node] = index_counter[0] lowlinks[node] = index_counter[0] index_counter[0] += 1 stack.append(node) try: successors = graph.successors(node) except AttributeError: successors = graph.neighbors(node) for successor in successors: if successor not in lowlinks: strongconnect(successor) lowlinks[node] = min(lowlinks[node], lowlinks[successor]) elif successor in stack: lowlinks[node] = min(lowlinks[node], index[successor]) if lowlinks[node] == index[node]: scc = set() while True: current_node = stack.pop() scc.add(current_node) if current_node == node: break result.append(scc) for node in graph.nodes(): if node not in lowlinks: strongconnect(node) return result if __name__ == "__main__": G = nx.DiGraph() # Create a directed graph object using NetworkX library edges_list = [(1, 2),(2, 3),(3, 1)] # Define edge list according to sample input data from hdu1269 problem statement[^5] G.add_edges_from(edges_list) components = tarjan_scc(G) print(f"Number of Strongly Connected Components found: {len(components)}") ``` 此段脚本定义了一个名为`tarjan_scc()`的功能函数接收网络对象作为参数,并返回由集合组成的列表形式的结果集,每个子集中包含了构成单个SCC的所有顶点。最后部分展示了创建测试用DAG实例的过程以及调用上述功能获取最终答案的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值