POJ2186 Popular Cows

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

 
 
正解:tarjan+缩环成点
解题报告:
  有一周(4天)没写题了,马上期末考试了,一直在搞学科,但是还是想刷道题保持手感。。。考完期末就可以搞一个暑假竞赛了
  题目大意是给定一个有向图,找到所有点都能到达的点的个数。
  一眼秒题,tarjan算法,然后缩环,发现只有一个出边为0的环,则环中所有点都是合法解;如果不止一个,那么无解。
  POJ上交了一发,一遍AC,手感还不错。
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
int n,m;
int ecnt,cnt,total;
int first[MAXN],next[MAXM],to[MAXM];
int belong[MAXN];
int dfn[MAXN],low[MAXN];
bool pd[MAXN];
int Stack[MAXN],top;
int out[MAXN];
int ans,jilu; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void Init(){
ecnt=; cnt=; total=; top=;
memset(first,,sizeof(first));
memset(pd,,sizeof(pd));
memset(dfn,,sizeof(dfn));
memset(Stack,,sizeof(Stack));
} inline void link(int x,int y){
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
} inline void dfs(int x){
dfn[x]=++total; low[x]=dfn[x];
pd[x]=;
Stack[++top]=x;
for(int i=first[x];i;i=next[i]) {
int v=to[i];
if(!dfn[v]) {
dfs(v);
low[x]=min(low[x],low[v]);
}
else if(pd[v] && low[v]<low[x]) low[x]=low[v];
}
if(dfn[x]==low[x]) {
cnt++;
int now=Stack[top];
do{
belong[now]=cnt;
pd[now]=; top--;
if(now==x) break;
now=Stack[top];
}while();
}
} inline void tarjan(){
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i);
} inline void solve(){
while(scanf("%d%d",&n,&m)!=EOF) {
Init();
int x,y;
for(int i=;i<=m;i++) {
x=getint(); y=getint();
link(x,y);
}
tarjan();
for(int i=;i<=n;i++){
for(int j=first[i];j;j=next[j]) {
int v=to[j];
if(belong[v]!=belong[i]) {
out[belong[i]]++;
}
}
}
ans=,jilu=;
for(int i=;i<=cnt;i++){
if(out[i]==) {
ans++; jilu=i;
}
}
if(ans==) {
ans=;
for(int i=;i<=n;i++) {
if(belong[i]==jilu) ans++;
}
printf("%d\n",ans);
}
else printf("0\n");
}
} int main()
{
solve();
return ;
}
上一篇:springboot访问出错,mapperScan导包错误java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.() at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_172] at java.


下一篇:Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'