poj2186(Tarjin缩点)

题目链接:http://poj.org/problem?id=2186


Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 38414   Accepted: 15657

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

思路:这其实是一道Tarjin的模板题(水题)。。。

不过之前看到这道题是在白书上,后来查了一下,那个算法叫Kosaraju算法,今天又补了个Tarjin的做法,两者各有千秋吧。

顺便又搞了个Tarjin的模板,美滋滋 (o゚▽゚)o

代码 :

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN = 10010; 
const int MAXM = 50010;
int du[MAXN];
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];    
int Index, top;
int scc;                                               
bool Instack[MAXN];
int num[MAXN];                                                                                           
vector<int> g[MAXN];
void Tarjan(int u)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    int len=g[u].size();
    for (int i = 0; i < len; i++)
    {
        v=g[u][i];
        if (!DFN[v])
        {
            Tarjan(v);
            if (Low[u] > Low[v])
            {
                Low[u] = Low[v];
            }
        }
        else if (Instack[v] && Low[u] > DFN[v])
        {
            Low[u] = DFN[v];
        }
    }
    if (Low[u] == DFN[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc; 
			num[scc]++;
        }
        while (v != u);
    }
    return ;
}

void solve(int N)
{
    memset(DFN, 0, sizeof(DFN));
    memset(Instack, false, sizeof(Instack));
    Index = scc = top = 0;
    for (int i = 1; i <= N; i++)
    {
        if (!DFN[i])
        {
            Tarjan(i);
        }
    }
    return ;
}

int main()
{
	int m,n;  
    scanf("%d%d",&n,&m);  
    for(int i=1;i<=m;i++)  
    {  
        int x,y;  
        scanf("%d%d",&x,&y);  
        g[x].push_back(y); 
    } 
	solve(n);
	for(int i=1;i<=n;i++)
	{
		int len=g[i].size();
		for(int j=0;j<len;j++)
		{
			 int cur=g[i][j];
			 if(Belong[i]!=Belong[cur])
			 {
			 	  du[Belong[i]]++; 
			 }
		}
	}
	int sum=0;
	int ind;
	for(int i=1;i<=scc;i++)
	{
		if(du[i]==0)
		{
		   sum++;
		   ind=i;   
	    }
	}
	if(sum!=1)
	printf("0\n");
	else
	{
		printf("%d\n",num[ind]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/star_moon0309/article/details/80781015