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. cygwin在Windows8.1中设置ssh的问题解决

    为了在Windows 8.1上直接使用Linux环境和hadoop开发,装了cygwin,同时设置ssh无密码登录.   但正常ssh-keygen后复制到authorised_keys后登录出现提示 ...

  2. [LeetCode][Python]15:3Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...

  3. chrome 下的 proxy 插件安装

    Install “Proxy SwitchyOmega” extensions for chrome.

  4. oracle重新启动步骤

    1. 停应用层的各种程序. $lsnrctl stop   3. 在独占的系统用户下,备份控制文件: SQL>alter database backup controlfile to trace ...

  5. iOS 开发 UI 搭建心得(一)—— 驾驭 StoryBoard

    本系列文章中,我们将一起认识.了解当下 iOS 开发中几种常见的 UI 构建方式,分析他们分别适合的使用场景,以便让我们在以后的开发中,能够在恰当的时间.场景下做出最佳的选择,提升开发效率,增强程序的 ...

  6. 读写分离提高 SQL Server 并发性

    转自:http://www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=476 在一些大型的网站或者应用中,单台的SQL Serv ...

  7. 显示标题栏中标题左侧的小图icon

    如何显示网站logo,定义网站收藏夹图标 代码与解释 <link rel="shortcut icon" href="/path/favicon.ico" ...

  8. oracle数据库获取指定表的列的相关信息

    1.很多时候我们需要从数据库中获取指定表的所有列的相关属性,如 name,commens,datatype,datalength,pk等.下面就是制定的语句. select c.TABLE_NAME ...

  9. asp.net 下载文件(图片、word、excel等)

    string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...

  10. nginx gzip on

    # Gzip settings. gzip on; gzip_http_version 1.0;默认值是1.1 gzip_comp_level ; #压缩级别,1压缩比最小处理速度最快,9压缩比最大但 ...