tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解

--------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
 
#define rep( i, n ) for( int i = 0; i < n; ++i )
#define clr( x, c ) memset( x, c, sizeof( x ) )
#define Rep( i, n ) for( int i = 1; i <= n; ++i )
 
using namespace std;
 
const int maxn = 10000 + 5;
const int maxm = 50000 + 5;
 
struct edge {
int to;
edge* next;
};
 
edge* pt;
edge* head[ maxn ];
edge EDGE[ maxm ];
 
void init() {
pt = EDGE;
clr( head, 0 );
}
 
void add_edge( int u, int v ) {
pt -> to = v;
pt -> next = head[ u ];
head[ u ] = pt++;
}
 
int dfs_clock, scc_cnt;
int dfn[ maxn ], low[ maxn ], scc[ maxn ];
stack< int > S;
 
void tarjan( int x ) {
dfn[ x ] = low[ x ] = ++dfs_clock;
S.push( x );
for( edge* e = head[ x ]; e; e = e -> next ) {
int to = e -> to;
if( ! dfn[ to ] ) {
tarjan( to );
low[ x ] = min( low[ x ], low[ to ] );
} else if( ! scc[ to ] ) 
   low[ x ] = min( low[ x ], dfn[ to ] );
}
if( low[ x ] == dfn[ x ] ) {
scc_cnt++;
for( ; ; ) {
int o = S.top();
S.pop();
scc[ o ] = scc_cnt;
if( o == x ) break;
}
}
}
 
void TARJAN( int n ) {
dfs_clock = scc_cnt = 0;
clr( dfn, 0 );
clr( scc, 0 );
rep( i, n ) if( ! dfn[ i ] ) tarjan( i );
}
 
int out[ maxn ];
 
int main() {
init();
int n, m;
cin >> n >> m;
while( m-- ) {
int u, v;
scanf( "%d%d", &u, &v );
add_edge( u - 1, v - 1 );
}
TARJAN( n );
clr( out, 0 );
rep( i, n ) {
int x = scc[ i ];
for( edge* e = head[ i ]; e; e = e -> next )
   if( x != scc[ e -> to ] ) out[ x ]++;
   
}
int ans = -1, cnt = 0;
Rep( i, scc_cnt ) if( ! out[ i ] )
   ans = i, cnt++;
   
if( ans == -1 || cnt > 1 ) printf( "-1\n" );
else {
cnt = 0;
rep( i, n ) 
   if( scc[ i ] == ans ) cnt++;
   
cout << cnt << "\n";
}
return 0;
}

--------------------------------------------------------------------------

1051: [HAOI2006]受欢迎的牛

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2712  Solved: 1426
[Submit][Status][Discuss]

Description

每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。

Input

第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)

Output

一个数,即有多少头牛被所有的牛认为是受欢迎的。

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

100%的数据N<=10000,M<=50000

Source

BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )的更多相关文章

  1. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2092  Solved: 1096[Submit][Sta ...

  2. bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...

  3. BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8172  Solved: 4470[Submit][Sta ...

  4. BZOJ 1051: [HAOI2006]受欢迎的牛 缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  5. 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点

    Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...

  6. bzoj 1051 [HAOI2006]受欢迎的牛(tarjan缩点)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1051 题解:缩点之后判断出度为0的有几个,只有一个那么输出那个强连通块的点数,否者 ...

  7. 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  8. BZOJ 1051: [HAOI2006]受欢迎的牛

    Description 一个有向图,求所以能被别的点到达的点的个数. Sol Tarjan + 强连通分量 + 缩点. 缩点以后找强连通分量,缩点,然后当图有且仅有1个出度为1的点时,有答案. Cod ...

  9. BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...

随机推荐

  1. Mysql 语句汇总(性能篇)

    查询mysql 哪些表正在被锁状态: show open TABLES where In_use > 0; show open table from XXX(数据库名);//查看数据库哪些表正在 ...

  2. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)

    目的: 1. 通过网页读取watchdog的信息 2. 通过网页设置watchdog 准备工作: 1. 选择一个web框架,选用 cherrypy $ sudo apt-get install pyt ...

  3. OA项目之打印

    打印 若此页有一个打印按钮: <input type="button" id="btnPrint" class="button_sm7" ...

  4. 从ACM中删除一个已经创建的Library

    从ACM中删除一个已经创建的Library,无法通过界面操作,须要手工从DB中删除.须要删除的表记录有: RECENTUPDATE 找到字段Name等于该libraryName的那条记录删除掉 del ...

  5. Unity3d 游戏汉化之IL注入文本替换--木石世纪

    近期下了个游戏叫木石世纪(Timber and Stone),沙盒游戏类,看着还不错. 搜了下游戏资料,有人求汉化可是因为是小众游戏,没人出汉化.看了眼是Unity3d的,既然是.Net的,仅仅要资源 ...

  6. android中退出当前应用程序的四种方法

    android中退出当前应用程序的四种方法 [IT168 技术]Android程序有很多Activity,比如说主窗口A,调用了子窗口B,如果在B中直接finish(), 接下里显示的是A.在B中如何 ...

  7. 达内TTS6.0课件oop_day05

  8. C. Table Decorations(Codeforces Round 273)

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. CloseHandle(),TerminateThread(),ExitThread()的区别

    线程的handle用处:线程的handle是指向“线程的内核对象”的,而不是指向线程本身.每个内核对象只是内核分配的一个内存块,并且只能由内核访问.该内存块是一种数据结构,它的成员负责维护对象的各种信 ...

  10. HTML与CSS绘制简单DIV布局

    HTML代码<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...