您的位置:首页 > 运维架构

poj 2186 Popular Cows

2015-06-13 15:56 393 查看
Language:
Default

Popular Cows

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 25257Accepted: 10345
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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int N = 10000 + 10;
const int M = 50000 + 10;

int n, m;
int head1, tot1, head2
, tot2;
bool vis
;
vector<int> v; //后序访问顺序的顶点列表
int cmp
; //所属强连通分量的拓扑序
bool mark
;
int tmp;

struct Edge
{
int v, next;
}e1[M], e2[M];

void init()
{
v.clear();
tot1 = tot2 = 0;
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
}

void adde(int *head, Edge *edge, int &tot, int u, int v)
{
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}

void dfs(int u)
{
vis[u] = true;
for(int i=head1[u]; i!=-1; i=e1[i].next)
{
int v = e1[i].v;
if(!vis[v]) dfs(v);
}
v.push_back(u);
}

void rdfs(int u, int k)
{
vis[u] = true;
cmp[u] = k;
for(int i=head2[u]; i!=-1; i=e2[i].next)
{
int v = e2[i].v;
if(!vis[v]) rdfs(v, k);
}
}

void disp(int *head, Edge *edge)
{
for(int i=1; i<=n; i++)
{
printf("%d : ", i);
for(int j=head[i]; j!=-1; j=edge[j].next)
cout<<edge[j].v<<" ";
cout<<endl;
}
}

int SCC()//查找出有多少个强连通分量
{
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++)
if(!vis[i]) dfs(i);

memset(vis, 0, sizeof(vis));
int k=0;
for(int i=v.size()-1; i>=0; i--)
if(!vis[v[i]])
rdfs(v[i], k++);

return k;
}

void dfs2(int u)
{
vis[u] = true;
if(!mark[cmp[u]])
{
mark[cmp[u]] = true;
tmp++;
}
for(int i=head2[u]; i!=-1; i=e2[i].next)
{
int v = e2[i].v;
if(!vis[v])
dfs2(v);
}
}

void solve()
{
int k = SCC();
//cout<<k<<endl;
int t = v[0];
//cout<<t<<endl;
memset(vis, 0, sizeof(vis));
memset(mark, 0, sizeof(mark));
tmp = 0;
dfs2(t);

//printf("tmp : %d\n", tmp);
int ans = 0;
if(tmp == k)
{
for(int i=1; i<=n; i++)
if(cmp[i] == k-1)
ans++;
}
printf("%d\n", ans);
}

int main()
{
while(~scanf("%d%d", &n, &m))
{
int u, v;
init();
for(int i=0; i<m; i++)
{
scanf("%d%d", &u, &v);
adde(head1, e1, tot1, u, v); //正向图
adde(head2, e2, tot2, v, u); //反向图
}
// disp(head1, e1);
// cout<<endl;
// disp(head2, e2);
solve();
}
return 0;
}

/*

9 111 2
2 3
2 4
3 4
4 5
5 3
5 6
6 7
7 8
8 9
9 6

*/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: