POJ 2186 Popular cows 【传送门】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 43139 | Accepted: 17547 |
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.
Source
题目大意:如果牛A喜欢B,B喜欢C,那么A也喜欢C。所以C是三只牛中的明星牛。求被所有牛喜欢的牛的数量。
解题思路:先求缩点后的图出度为0的子图的个数,如果子图出度为0个数等于1,那么对应子图里的点的个数就是答案;否则答案为0,因为两个出度为0,就不可能做到所有牛都喜欢自己了。
AC代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
#define INF 999999
#define MAXN 100000+5
//struct edge{
// int to;
// //int val;
//};
//int next[MAXN];
vector<int> G[MAXN];
int dfn[MAXN],low[MAXN];
bool instack[MAXN];
stack<int> s;
int timing; //时间戳
int color[MAXN]; //代表当前结点所属第几个scc
int colorcnt[MAXN]; //代表第某个scc有几个结点
int cnt; //scc个数
int V,E;
int ans,id;
void init()
{
for(int i=0;i<=V;i++)
G[i].clear();
while(!s.empty())
s.pop();
mem(dfn,0);
mem(color,0);
mem(colorcnt,0);
mem(instack,false);
mem(low,0);
timing=cnt=0;
}
void tarjan(int u)
{
timing++;
dfn[u]=low[u]=timing;
s.push(u);
instack[u]=true;
for(int i=0;i<G[u].size();i++)
{
//int v=next[u];
int v=G[u][i];
if(dfn[v]==0)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cnt++;
int temp;
do
{
//记录每个点属于哪个scc
temp=s.top();
s.pop();
instack[temp]=false;
color[temp]=cnt;
// colorcnt[cnt]++;
}while(u!=temp);
}
}
int de[MAXN];
void chudu(){
for(int i=1;i<=V;i++){
for(int j=0;j<G[i].size();j++){
int v=G[i][j];
if(color[i]!=color[v]){
de[color[i]]++;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> V >> E && V+E)
{
init();
mem(de,0);
int du[MAXN];
int a,b;
for(int i=0;i<E;i++)
{
cin >> a >> b;
G[a].push_back(b);
}
for(int i=1;i<=V;i++)
if(dfn[i]==0)
tarjan(i);
ans=0;
chudu();
for(int i=1;i<=cnt;i++){
if(de[i]==0){
id=i;
ans++;
}
}
// for(int i=1;i<=V;i++){
// cout << i << " color: " << color[i] << " colorcnt: " << colorcnt[i] << endl;
// }
if(ans==1){
ans=0;
for(int i=1;i<=V;i++){
if(color[i]==id)
ans++;
}
cout << ans << endl;
}else{
cout << "0\n";
}
}
return 0;
}
/*
7 8
1 2 2 3 3 1 1 7
4 5 5 6 6 4 4 7
5 6
1 2 2 3 3 1
1 4 4 5 5 4
5 4
1 2 2 3 3 4 4 5
7 9
1 2 2 3 3 1 1 7 7 1
4 7 4 5 5 6 6 4
4 4
1 2 2 3 3 1 4 1
*/