zoukankan      html  css  js  c++  java
  • poj--2186--Popular Cows (scc+缩点)

    Popular Cows

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 2   Accepted Submission(s) : 1
    Problem 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
    /*找出最受欢迎的牛*/
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<algorithm>
    using namespace std;
    #define MAX 50010
    struct node
    {
    	int u,v;
    	int next;
    }edge[MAX];
    int low[MAX],dfn[MAX];
    int sum,sumin,sumout;
    int sccno[MAX],scc_cnt;
    int head[MAX],dfs_clock,cnt;
    bool Instack[MAX];
    vector<int>G[MAX];
    vector<int>scc[MAX];
    stack<int>s;
    int in[MAX],out[MAX];
    int m,n;
    void init()
    {
    	memset(head,-1,sizeof(head));
    	cnt=0;
    }
    void add(int u,int v)
    {
    	edge[cnt].u=u;
    	edge[cnt].v=v;
    	edge[cnt].next=head[u];
    	head[u]=cnt++;
    }
    void getmap()
    {
    	int a,b;
    	while(m--)
    	{
    		scanf("%d%d",&a,&b);
    		add(a,b);
    	}
    }
    void tarjan(int u,int fa)
    {
    	int v;
    	low[u]=dfn[u]=++dfs_clock;
    	s.push(u);
    	Instack[u]=true;
    	for(int i=head[u];i!=-1;i=edge[i].next)
    	{
    		v=edge[i].v;
    		if(!dfn[v])
    		{
    			tarjan(v,u);
    			low[u]=min(low[u],low[v]);
    		}
    		else if(Instack[v])
    		low[u]=min(low[u],dfn[v]);
    	}
    	if(low[u]==dfn[u])
    	{
    		scc_cnt++;
    		scc[scc_cnt].clear();
    		for(;;)
    		{
    			v=s.top();
    			s.pop();
    			Instack[v]=false;
    			scc[scc_cnt].push_back(v);
    			sccno[v]=scc_cnt;
    			if(u==v) break;
    		}
    	}
    }
    void find(int l,int r)
    {
    	memset(low,0,sizeof(low));
    	memset(dfn,0,sizeof(dfn));
    	memset(sccno,0,sizeof(sccno));
    	memset(Instack,false,sizeof(Instack));
    	scc_cnt=dfs_clock=0;
    	for(int i=l;i<=r;i++)
    	if(!dfn[i])
    	tarjan(i,-1);
    }
    void suodian()
    {
    	for(int i=1;i<=scc_cnt;i++)
    	G[i].clear(),in[i]=0,out[i]=0;
    	for(int i=0;i<cnt;i++)
    	{
    		int u=sccno[edge[i].u];
    		int v=sccno[edge[i].v];
    		if(v!=u)
    		{
    			G[u].push_back(v);
    			out[u]++,in[v]++;
    		}
    	}
    }
    void solve()
    {
    	int sum=0;
    	int ans=0;
    	for(int i=1;i<=scc_cnt;i++)
    	{
    		if(out[i]==0)
    		//出度为零说明在新图里他是叶子节点,现在统计叶子节点个数 
    		{
    			sum++;
    			ans+=scc[i].size();
    		}
    	}
    	if(sum>1) printf("0
    ");
    	if(sum==0) printf("%d
    ",n);
    	//如果只有1个scc,说明全部的牛都是最受欢迎的 
    	if(sum==1) printf("%d
    ",ans);
    	//如果sum==0说明在新图里叶子节点只有一个
    	//那么当前的scc中所有的牛都是最受欢迎的 
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		init();
    		getmap();
    		find(1,n);
    		suodian();
    		solve();
    	}
    	return 0;
    }


  • 相关阅读:
    为富人服务,挣富人的钱 Leone
    javascript 高效数组随机顺序 Leone
    Atitit 开发2d游戏的技术选型attilax总结
    Atitit 软件项目非法模块与功能的管理与  监狱管理的对比 原理与概论attilax总结
    qqzoneQQ空间漏洞扫描器的设计attilax总结
    Atitit机器学习原理与概论book attilax总结
    Atiti  qq空间破解(3)gui图形化通用cli执行器atiuse
    Atitit.attilax重要案例 项目与解决方案与成果 v6 qa15
    Atitit dsl对于数组的处理以及main函数的参数赋值
    Atitit 类库冲突解决方案  httpclient4.5.2.jar
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273781.html
Copyright © 2011-2022 走看看