题目链接:
http://poj.org/problem?id=2186
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
USACO 2003 Fall
|
已知n个人,m种欣赏关系((a,b)说明a欣赏b) ,欣赏关系可以传递。
求能被其他所有人欣赏的的人的个数。
解题思路:
tarjan求出SCC,每个SCC缩成一个点,求出出度为0的SCC个数cnt,如果cnt>1,说明存在不相连的SCC,输出0。如果cnt=1,输出出度为0的SCC中点的个数。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 11000 struct Edge { int v; struct Edge * next; }edge[51000],*head[Maxn]; int low[Maxn],dfn[Maxn],in[Maxn],cnt; bool iss[Maxn]; stack<int>mys; int scc,t,n,m; void add(int a,int b) { ++cnt; edge[cnt].v=b; edge[cnt].next=head[a]; head[a]=&edge[cnt]; } void tarjan(int cur) { low[cur]=dfn[cur]=++t; struct Edge *p=head[cur]; mys.push(cur); iss[cur]=true; while(p) { int v=p->v; if(!dfn[v]) //没有被访问 { tarjan(v); low[cur]=min(low[cur],low[v]); } else if(iss[v]) { low[cur]=min(low[cur],dfn[v]); } p=p->next; } int temp; if(low[cur]==dfn[cur]) { ++scc; do { temp=mys.top(); mys.pop(); in[temp]=scc; iss[temp]=false; }while(temp!=cur); } } void solve() { for(int i=1;i<=n;i++) if(!dfn[i]) //没有被访问 tarjan(i); //system("pause"); /*for(int i=1;i<=n;i++) printf("i:%d in:%d\n",i,in[i]); system("pause");*/ struct Edge * p; memset(iss,0,sizeof(iss)); //统计出度为0的强联通快数量 for(int i=1;i<=n;i++) { p=head[i]; while(p) { int v=p->v; if(in[i]!=in[v]) //不在一个SCC里 { iss[in[i]]=true; break; } p=p->next; } } int ans=0,tt; for(int i=1;i<=scc;i++) { if(!iss[i]) //出度为0的SCC { ans++; tt=i; } } //printf("%d\n",ans); //system("pause"); if(ans>1) printf("0\n"); //相互不连通 else { ans=0; for(int i=1;i<=n;i++) //求出个数 if(in[i]==tt) ans++; printf("%d\n",ans); } } void init() { memset(head,NULL,sizeof(head)); cnt=0; memset(iss,0,sizeof(iss)); memset(dfn,0,sizeof(dfn)); //mys.clear(); scc=t=0; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)) { init(); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); } solve(); } return 0; }