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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章