题目:
Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16768 | Accepted: 6740 |
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.
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.
* 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.
Source
题意:
给N头奶牛 ,求被所有奶牛欢迎的奶牛的数量,欢迎关系为单向且可传递
代码:
//problem_id poj 2186
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x1f1f1f1f
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
//first graph
int head[10005];
int point[50005];
int next[50005];
//reverse graph
int head_rev[10005];
int point_rev[50005];
int next_rev[50005];
//graph after find strong component
int head_new[10005];
int point_new[50005];
int next_new[50005];
//record the exit time of every in dfs
struct end_time
{
int num;
int exit_time;
}et[10005];
bool cmp(end_time e1,end_time e2)
{
return e1.exit_time>e2.exit_time;
}
bool fl[10005];
int con_cnt[10005];//the point count of every component contain
int pt_in_com[10005];//which component of every point in
int e_time;//deep search over time
int new_node_cnt;//how many point of the new graph have
int new_edge_cnt;//how many edge of the new graph have
int out_dig[10005];//out digree of the new graph
void init()
{
memset(head,-1,sizeof(head));
memset(point,-1,sizeof(point));
memset(next,-1,sizeof(next));
memset(head_rev,-1,sizeof(head_rev));
memset(point_rev,-1,sizeof(point_rev));
memset(next_rev,-1,sizeof(next_rev));
memset(head_new,-1,sizeof(head_new));
memset(point_new,-1,sizeof(point_new));
memset(next_new,-1,sizeof(next_new));
memset(con_cnt,0,sizeof(con_cnt));
memset(pt_in_com,0,sizeof(pt_in_com));
memset(out_dig,0,sizeof(out_dig));
e_time=0;
new_node_cnt=0;
new_edge_cnt=0;
}
void dfs(int v)
{
fl[v]=1;
for(int e=head[v];e!=-1;e=next[e])
{
if(!fl[point[e]])
{
dfs(point[e]);
}
}
et[v].num=v;
et[v].exit_time=e_time++;
}
void dfs_rev(int v)
{
fl[v]=1;
pt_in_com[v]=new_node_cnt;
con_cnt[new_node_cnt]++;
for(int e=head_rev[v];e!=-1;e=next_rev[e])
{
if(!fl[point_rev[e]])
{
dfs_rev(point_rev[e]);
}
else if(pt_in_com[point_rev[e]]!=0&&pt_in_com[point_rev[e]]!=new_node_cnt)
{
//add new edge and update out digree in new graph
new_edge_cnt++;
next_new[new_edge_cnt]=head_new[pt_in_com[point_rev[e]]];
head_new[pt_in_com[point_rev[e]]]=new_edge_cnt;
point_new[new_edge_cnt]=new_node_cnt;
out_dig[pt_in_com[point_rev[e]]]++;
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
next[i]=head[a];
head[a]=i;
point[i]=b;
next_rev[i]=head_rev[b];
head_rev[b]=i;
point_rev[i]=a;
}
memset(fl,0,sizeof(fl));
for(int i=1;i<=n;i++)
{
if(!fl[i])
{
dfs(i);
}
}
sort(et+1,et+n+1,cmp);
memset(fl,0,sizeof(fl));
for(int i=1;i<=n;i++)
{
if(!fl[et[i].num])
{
new_node_cnt++;
dfs_rev(et[i].num);
}
}
int res=0;
for(int i=1;i<=new_node_cnt;i++)
{
if(out_dig[i]==0)
{
if(res!=0)
{
res=0;
break;
}
res=i;
}
}
if(res==0)
{
printf("0\n");
}
else
{
printf("%d\n",con_cnt[res]);
}
}
return 0;
}