zoukankan      html  css  js  c++  java
  • tarjan+缩点

    B - Popular Cows
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
    popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

    Input

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

    * Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

    Output

    * Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

    Sample Input

    3 3
    1 2
    2 1
    2 3
    

    Sample Output

    1

    在对图进行建立时。假设用矩阵会导致内存超限。用VEC会使后来的缩点变得麻烦。所以用邻接表是最佳选择:

    #include<stdio.h>
    #include<stack>;
    #include<string.h>
    using namespace std;
    stack<int>S;
    int dfn[10005],map[10005][110],low[10005],instack[10005],belong[10005],out[10005];
    int index,bnt;
    void tarjan(int i){
    	dfn[i] = low[i] = ++index;
    	S.push(i);
    	instack[i] = 1;
    	for(int j=1;j<=map[i][0];j++){
    		int k = map[i][j];
    		if(!dfn[k]){
    			tarjan(k);
    			low[i] = min(low[i],low[k]);
    		}
    		else if(instack[k]){
    			low[i] = min(low[i],dfn[k]);
    		}
    		
    	}
    	if(low[i]  == dfn[i]){
    		bnt ++;
    		int k;
    		do{
    		    k = S.top();
    			S.pop();
    			instack[k] = 0;
    			belong[k] = bnt;
    		}while(i!=k);
    	}
    }
    int main(){
    	int m,n,sum;
    	while(~scanf("%d%d",&n,&m)){
    		int a,b,j;
    	   index = bnt = sum = 0;
    		memset(map,0,sizeof(map));
    		memset(instack,0,sizeof(instack));
    		memset(out,0,sizeof(out));
    		memset(dfn,0,sizeof(dfn));
    		for(int i=0;i<m;i++){
    			scanf("%d%d",&a,&b);
    			map[a][++map[a][0]] = b;
    		}
    		for(int i=1;i<=n;i++)
    		if(!dfn[i]){
    			tarjan(i);
    		}
    		for(int i=1;i<=n;i++){
    			for(j = 1;j<=map[i][0];j++)
    			if(belong[i]!=belong[map[i][j]])
    			out[belong[i]]++;
    		}
    		int z=0;
    		for(int i=1;i<=bnt;i++){
    			if(out[i]==0){
    				z++;
    				for(int j=1;j<=n;j++)
    				if(belong[j]==i)sum++;
    			}
    		}
    		if(z==1)
    		printf("%d
    ",sum);
    		else printf("0
    ");
    	
    	}
    }


  • 相关阅读:
    【Python之路】特别篇--Python装饰器
    【Python之路】特别篇--Python文件操作
    解决centos7命令行中文乱码
    微信小程序,错误{"errMsg":"request:fail 小程序要求的 TLS 版本必须大于等于 1.2"}
    nodejs 下载远程图片
    屏蔽ps联网激活
    RTMP开发记录 测试服务器搭建篇
    微信公众号文章爬虫抓取实现原理!
    如何更改vs2013中git的远程仓库url地址
    用过企业微信APP 后,微信接收不到消息,解决方案
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5056004.html
Copyright © 2011-2022 走看看