【POJ】Popular Cows 强连通

本文介绍了一种使用强连通分量分析解决流行牛问题的方法。问题描述了一个包含N头牛的群体,每头牛通过一系列的关系认为其他牛很流行。任务是确定哪些牛被认为是最流行的,即被所有其他牛所崇拜。文章详细解释了如何构建图模型,利用Tarjan算法进行强连通分量分析,并最终确定最流行牛的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Popular Cows
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 22560
Accepted: 9243

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

传送门:【POJ】Popular Cows

题目大意:有n头牛,再给m个关系,每个关系(u,v)表示u崇拜v,如果u崇拜v,v崇拜w,则说u崇拜w,现在问有多少的牛被其他所牛的牛崇拜。

题目分析:强连通缩点。缩点前判断图是否连通,如果不连通,输出0。否则,缩点。看缩点后是否有多于1个分量的出度等于0,有,输出0。否则,计算所有牛中属于的分量出度为0的个数,输出即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define clear( a , x ) memset ( a , x , sizeof a )

const int MAXN = 10005 ;
const int MAXE = 50005 ;

struct Edge {
	int v , n ;
	Edge ( int var = 0 , int next = 0 ) : v(var) , n(next) {}
} ;

struct SCC {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int Dfn[MAXN] , Low[MAXN] , dfs_clock ;
	int scc[MAXN] , scc_cnt ;
	int S[MAXN] , top ;
	bool ins[MAXN] ;
	bool ou[MAXN] ;
	
	void init () {
		top = 0 ;
		cntE = 0 ;
		scc_cnt = 0 ;
		dfs_clock = 0 ;
		clear ( ou , 0 ) ;
		clear ( ins , 0 ) ;
		clear ( Dfn , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v ) {
		edge[cntE] = Edge ( v , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	void Tarjan ( int u ) {
		Dfn[u] = Low[u] = ++ dfs_clock ;
		S[top ++] = u ;
		ins[u] = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( !Dfn[v] ) {
				Tarjan ( v ) ;
				Low[u] = min ( Low[u] , Low[v] ) ;
			}
			else if ( ins[v] )
				Low[u] = min ( Low[u] , Dfn[v] ) ;
		}
		if ( Low[u] == Dfn[u] ) {
			++ scc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				scc[v] = scc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	
	void find_scc ( int n ) {
		REPF ( i , 1 , n )
			if ( !Dfn[i] )
				Tarjan ( i ) ;
	}
	
	void solve ( int n ) {
		REPF ( u , 1 , n )
			for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
				int v = edge[i].v ;
				if ( scc[u] != scc[v] ) {
					ou[scc[u]] = 1 ;
				}
			}
		int num = 0 , cnt = 0 ;
		REPF ( i , 1 , scc_cnt )
			if ( !ou[i] )
				++ cnt ;
		if ( cnt > 1 ) {
			printf ( "0\n" ) ;
			return ;
		}
		REPF ( i , 1 , n )
			if ( !ou[scc[i]] )
				++ num ;
		printf ( "%d\n" , num ) ;
	}
} ;

SCC C ;
int p[MAXN] ;

int find ( int x ) {
	int o = x , ans , tmp ;
	while ( p[o] != o )
		o = p[o] ;
	ans = o ;
	o = x ;
	while ( p[o] != o ) {
		tmp = p[o] ;
		p[o] = ans ;
		o = tmp ;
	}
	return ans ;
}

void work () {
	int n , m ;
	int u , v ;
	while ( ~scanf ( "%d%d" , &n , &m ) && n ) {
		C.init () ;
		REPF ( i , 1 , n )
			p[i] = i ;
		while ( m -- ) {
			scanf ( "%d%d" , &u , &v ) ;
			C.addedge ( u , v ) ;
			int x = find ( u ) ;
			int y = find ( v ) ;
			p[x] = y ;
		}
		REPF ( i , 1 , n )
			p[i] = find ( i ) ;
		int flag = 0 ;
		REPF ( i , 2 , n )
			if ( p[i] != p[i - 1] )
				flag = 1 ;
		if ( flag ) {
			printf ( "0\n" ) ;
			continue ;
		}
		C.find_scc ( n ) ;
		C.solve ( n ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值