http://poj.org/problem?id=2186
//PKU_2186_Popular Cows_ǿlͨ��_���(kosaraju)
#include <iostream>
using namespace std;
struct Point
{
int Index;
Point *Next;
Point(int index = 0)
{
Index = index;
Next = NULL;
}
};
struct HeadPoint
{
Point *List;
Point *LastPoint;
HeadPoint()
{
List = NULL;
LastPoint = NULL;
}
void Add(int index)
{
if (LastPoint != NULL)
{
LastPoint->Next = new Point(index);
LastPoint = LastPoint->Next;
}
else
{
LastPoint = List = new Point(index);
}
}
~HeadPoint()
{
Point *p = List;
while (p != NULL)
{
Point *q = p;
p = p->Next;
delete q;
}
}
};
HeadPoint *MAP = NULL;
HeadPoint *MAP2 = NULL;
HeadPoint *FinalMap = NULL;
int *nPost = NULL;
int *nBelong = NULL;
bool *visited = NULL;
int nPostorder = 0;
int nBe = 0;
int N,M;
void DFS1(int index)
{
visited[index] = true;
for (Point *p = MAP[index].List;p != NULL;p = p->Next)
{
if (!visited[p->Index])
DFS1(p->Index);
}
nPost[++nPostorder] = index;
}
void DFS2(int index)
{
visited[index] = true;
for (Point *p = MAP2[index].List;p != NULL;p = p->Next)
{
if (!visited[p->Index])
DFS2(p->Index);
}
nBelong[index] = nBe;
}
void init()
{
scanf("%d%d",&N,&M);
MAP = new HeadPoint[N + 1]; //0 not use
MAP2 = new HeadPoint[N + 1];
nPost = new int[N + 1];
visited = new bool[N + 1];
nBelong = new int[N + 1];
for (int i = 0;i < M;i++)
{
int m,n;
scanf("%d%d",&m,&n);
MAP[m].Add(n);
MAP2[n].Add(m);
}
}
void clear()
{
delete []MAP;
delete []MAP2;
delete []FinalMap;
delete []nPost;
delete []visited;
delete []nBelong;
}
void kosaraju()
{
nBe = nPostorder = 0;
memset(visited,false,(N + 1) * sizeof(bool));
for (int i = 1;i < N + 1;i++)
{
if (!visited[i])
DFS1(i);
}
memset(visited,false,(N + 1) * sizeof(bool));
for (int i = N ;i > 0;i--)
{
if (!visited[nPost[i]])
{
nBe++;
DFS2(nPost[i]);
}
}
}
int Answer()
{
FinalMap = new HeadPoint[nBe + 1];
for (int i = 1;i < N + 1;i++)
{
for (Point *p = MAP[i].List;p != NULL;p = p->Next)
{
if (nBelong[i] != nBelong[p->Index])
FinalMap[nBelong[i]].Add(nBelong[p->Index]);
}
}
int FIndex = -1;
int sum = 0;
for (int i = 1;i < nBe + 1;i++)
{
if (FinalMap[i].List == NULL)
{
FIndex = i;
sum++;
}
}
if(sum == 1)
{
int ret = 0;
for(int i = 1;i < N + 1;i++)
{
if(nBelong[i] == FIndex)
ret++;
}
return ret;
}
else
return 0;
}
int main()
{
init();
kosaraju();
cout<<Answer()<<endl;
clear();
return 0;
}
/*
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
Hint
Cow 3 is the only cow of high popularity.
*/