• 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom(强连通分量模板)


    题目描述
    约翰的N (2 <= N <= 10,000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别 上鲜花,她们要表演圆舞.

    只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好, 顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.

    为了跳这种圆舞,她们找了 M(2<M< 50000)条绳索.若干只奶牛的蹄上握着绳索的一端, 绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶 牛.有的奶牛可能握有很多绳索,也有的奶牛可能一条绳索都没有.

    对于一只奶牛,比如说贝茜,她的圆舞跳得是否成功,可以这样检验:沿着她牵引的绳索, 找到她牵引的奶牛,再沿着这只奶牛牵引的绳索,又找到一只被牵引的奶牛,如此下去,若最终 能回到贝茜,则她的圆舞跳得成功,因为这一个环上的奶牛可以逆时针牵引而跳起旋转的圆舞. 如果这样的检验无法完成,那她的圆舞是不成功的.

    如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.

    给出每一条绳索的描述,请找出,成功跳了圆舞的奶牛有多少个组合?

    输入输出格式
    输入格式:

    Line 1: Two space-separated integers: N and M

    Lines 2…M+1: Each line contains two space-separated integers A and B that describe a rope from cow A to cow B in the clockwise direction.

    输出格式:

    Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.

    输入输出样例
    输入样例#1:

    5 4
    2 4
    3 5
    1 2
    4 1

    输出样例#1:

    1

    说明

    Explanation of the sample:

    ASCII art for Round Dancing is challenging. Nevertheless, here is a representation of the cows around the stock tank:

          _1___
          /**** 
       5 /****** 2
      / /**TANK**|
       ********/
        ******/  3
         4____/  /
         \_______/
    

    Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don’t have the second rope they’d need to be able to pull both ways, thus they can not properly perform the Round Dance.
    (强连通分量裸题,详情看代码注释)
    (贴一个讲强连通分量的博客https://blog.csdn.net/huangdanning/article/details/81983969)

    #include <iostream>
    #include <cstdio>
    #include <vector>
    using namespace std;
    const int maxn=1e4+10;
    int dfn[maxn],low[maxn],sta[maxn],vis[maxn],top,cnt,ans; 
    //int color[maxn],sum; 染色数组用来缩点,此题用不到 
    //dfn[i]表示i是第几个搜到的,cnt用来计数编号 
    //low[i]表示i及i的子孙中dfn的最小值
    //sta表示栈数组,放的是当前强连通分量说包括的点
    //vis[i]表示i是否在栈中 
    vector <int> ve[maxn];//存图 
    void tarjan(int u)
    {
       //赋初值
       dfn[u]=low[u]=cnt++; 
       //入栈,标记	
       sta[++top]=u;
       vis[u]=1;
       
       for(int i=0;i<ve[u].size();i++)
       {
       	  int v=ve[u][i];
       	  //如果没有搜索过,则搜索 
          if(!dfn[v])
    	  {
    	     tarjan(v);
    	     //搜完后更新low[u]的值 
    		 low[u]=min(low[u],low[v]);	
    	  }	
       	  else 
       	  {
       	  	 
       	     if(vis[v])	
       	  	 {
       	  	 	//如果能和u组成强连通分量,更新low[u]的值  
       	  	    low[u]=min(low[u],low[v]);	
    		 }
    	  }
       }
       //如果u能和它的子孙组成强连通分量,则计算点的个数 
       if(dfn[u]==low[u])
       {
       	   int num=0;
           //color[u]=++sum;
    	   while(sta[top]!=u)
    	   {
    	   	  num++;
    	   	  //color[sta[top]]=sum;
    	   	  vis[sta[top--]]=0;
    	   }
           vis[sta[top--]]=0;
           num++; 
           //点的个数为1的不算,即孤立的单点不算 
    	   if(num>1)
    	     ans++;   	
       }	
    } 
    int main(void)
    {
    	int n,m;
    	top=-1;
    	cnt=1;
    	//sum=0;
    	ans=0;
    	scanf("%d%d",&n,&m);
    	for(int i=0;i<m;i++)
    	{
    		int u,v;
    		scanf("%d%d",&u,&v);
    		ve[u].push_back(v);
    	}
    	for(int i=1;i<=n;i++)
    	{
    		//如果没被搜索过则搜索 
    		if(!dfn[i])
    		{
    		  tarjan(i);
    		} 	  	
    	}
        printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    tomcat正常启动后http://localhost:8080/报错404
    关于导入本地maven项目pom.xml出现missing artifact org....报错处理
    #PHP 数组添加元素、统计数组相同元素个数、改变数组key值~_~
    photoshopcs5 win7安装报错的解决
    重启云服务器以后需要立刻执行的操作
    Android ADT离线更新办法
    Android无法更新sdk的解决办法
    分布式助手Zookeeper(四)
    分布式助手Zookeeper(三)
    分布式助手Zookeeper(二)
  • 原文地址:https://www.cnblogs.com/qinjames/p/10554684.html
Copyright © 2020-2023  润新知