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

题解

好久没打$tarjan$了,码在这当模板存着。

 //It is made by Awson on 2017.9.24
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
const int N = ;
const int M = ;
int Read() {
char ch = getchar();
int sum = ;
while (ch < '' || ch > '') ch = getchar();
while (ch >= '' && ch <= '') sum = (sum<<)+(sum<<)+ch-, ch = getchar();
return sum;
} int n, m, u, v;
struct tt {
int from, to, next;
}edge[M+];
int path[N+], top;
int dfn[N+], low[N+], t;
bool vis[N+];
int S[N+], topS;
int sccno[N+], sccnum;
int in[N+], tot[N+]; void add(int u, int v) {
edge[++top].to = v;
edge[top].from = u;
edge[top].next = path[u];
path[u] = top;
}
void tarjan(int r) {
dfn[r] = low[r] = ++t;
vis[r] = ;
S[topS++] = r;
for (int i = path[r]; i; i = edge[i].next) {
if (!dfn[edge[i].to]) {
tarjan(edge[i].to);
low[r] = Min(low[edge[i].to], low[r]);
}
else if (vis[edge[i].to])
low[r] = Min(low[r], dfn[edge[i].to]);
}
if (dfn[r] == low[r]) {
sccnum++;
while (topS > && S[topS] != r) {
vis[S[--topS]] = ;
sccno[S[topS]] = sccnum;
tot[sccnum]++;
}
}
}
void work() {
n = Read(), m = Read();
for (int i = ; i <= m; i++) {
u = Read(), v = Read();
add(u, v);
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i);
for (int i = ; i <= m; i++)
if (sccno[edge[i].to] != sccno[edge[i].from])
in[sccno[edge[i].from]]++;
int ans = , cntt = ;
for (int i = ; i <= sccnum; i++)
if (!in[i]) cntt++, ans=i;
if (cntt == ) printf("%d\n", tot[ans]);
else printf("0\n");
}
int main() {
work();
return ;
}

[HAOI 2006]受欢迎的牛的更多相关文章

  1. HAOI 2006 受欢迎的牛 (洛谷2341)

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

  2. BZOJ 1051 HAOI 2006 受欢迎的牛

    [题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...

  3. bzoj1051 [HAOI2006]受欢迎的牛

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

  4. bzoj 1051 (强连通) 受欢迎的牛

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

  5. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

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

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

  7. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  8. 【BZOJ】1051: [HAOI2006]受欢迎的牛(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1051 这题还好-1A了..但是前提还是看了题解的 囧.....一开始认为是并查集,oh,不行,,无法 ...

  9. BZOJ 1051 受欢迎的牛(Tarjan缩点)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4573  Solved: 2428 [Submit][S ...

随机推荐

  1. 201621123040《Java程序设计》第十周学习总结

    1.本周学习总结 2.书面作业 2.1常用异常 2.1.1自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 算术异常ArithmeticException(除数为0的情况) 类 ...

  2. 20162317袁逸灏 第八周实验报告:实验二 Java面向对象程序设计

    20162317袁逸灏 第八周实验报告:实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 ...

  3. 使用genstring和NSLocalizedString实现App文本的本地化

    OS提供了简便的方法来实现本地化,其中用的最多的就是NSLocalizedString. 首先查看下NSLocalizedString是什么: #define NSLocalizedString(ke ...

  4. Android网络传输中必用的两个加密算法:MD5 和 RSA 及Base64加密总结

    (1)commons-codec包简介 包含一些通用的编码解码算法.包括一些语音编码器,Hex,Base64.MD5 一.md5.base64.commons-codec包 commons-codec ...

  5. 故障公告:IIS应用程序池停止工作造成博客站点无法访问

    非常抱歉,今天凌晨博客站点负载均衡中所有3台服务器的IIS应用程序池突然停止工作,造成 1:20-7:45 左右博客站点无法正常访问,由此给您带来很大的麻烦,请您谅解. 服务器操作系统是 Window ...

  6. nodeJS基于smtp发邮件

    邮件的协议smtp是tcp/ip族中的一个协议,所以我们这次考虑使用net模块来发送邮件. const net = require('net') const assert = require('ass ...

  7. nodejs 全局变量

    1.全局对象 所有模块都可以调用 1)global:表示Node所在的全局环境,类似于浏览器中的window对象. 2)process:指向Node内置的process模块,允许开发者与当前进程互动. ...

  8. 泛型的 typeof

    static void Main(string[] args) { TestTypeOf<string>(); Console.ReadKey(); } static void TestT ...

  9. Python内置函数(4)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...

  10. 新概念英语(1-143)A walk through the woods

    Lesson 143 A walk through the woods 林中散步 Listen to the tape then answer this question. What was so f ...