zoukankan      html  css  js  c++  java
  • bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径【tarjan】

    首先来分析一下,这是一张无向图,要求没有两条路联通的点对个数
    有两条路连通,无向图,也就是说,问题转化为不在一个点双连通分量里的点对个数
    tarjan即可,和求scc还不太一样……

    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N=5005;
    int n,m,h[N],cnt=1,x,y,dfn[N],low[N],tmp,s[N],top,bl[N],col,d[N],ans;
    bool v[N];
    struct qwe
    {
        int ne,no,to;
    }e[20005];
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    void add(int u,int v)
    {
     	cnt++;
        e[cnt].ne=h[u];
        e[cnt].no=u;
        e[cnt].to=v;
        h[u]=cnt;
    }
    void tarjan(int u)
    {
        dfn[u]=low[u]=++tmp;
        s[++top]=u;
        for(int i=h[u];i;i=e[i].ne)
    		if(!v[i])
    		{
    			v[i]=v[i^1]=1;
    			if(dfn[e[i].to])
    				low[u]=min(low[u],dfn[e[i].to]);
    			else
    			{
    				tarjan(e[i].to);
    				low[u]=min(low[u],low[e[i].to]);
    			}
    		}
        if(!bl[u])
        {
            ++col;
            while(low[u]<=dfn[s[top]])
    			bl[s[top--]]=col;
        }
    }
    int main()
    {
        n=read(),m=read();
        while(m--)
        {
            int x=read(),y=read();
            add(x,y);add(y,x);
        }
        for(int i=1;i<=n;i++)
    		if(!dfn[i])
    			tarjan(i);
        for(int i=1;i<=cnt;i++)
    		if(bl[e[i].no]!=bl[e[i].to])
    			d[bl[e[i].no]]++,d[bl[e[i].to]]++;
        for(int i=1;i<=col;i++)
    		if(d[i]==2)
    			ans++;
        printf("%d",(ans+1)/2);
        return 0;
    }
    
  • 相关阅读:
    读书笔记
    STL 笔记
    Centos8如何配置网桥
    命令集合
    shared_ptr给管理的对象定制析沟函数
    share_ptr指向的对象的析构动作在创建的时候被捕获
    优秀博客
    字符串单词翻转
    带权图的最短路径算法(Dijkstra,Floyd,Bellman_ford)
    生产者与消费者 c++实现
  • 原文地址:https://www.cnblogs.com/lokiii/p/9014113.html
Copyright © 2011-2022 走看看