[Luogu] 受欢迎的牛
https://www.luogu.org/problemnew/show/P2341
Tarjan 缩点 + 判断出度
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int N = 1e4 + ;
const int M = 5e4 + ; #define yxy getchar() int Tarjan_tim, now = , n, m, topp, bel;
int Stack[N], head[N], dfn[N], low[N], belong[N], Cnt[N], Out[N];
struct Node {int u, v, nxt;} G[M];
bool vis[N]; inline int read(){
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} inline void add(int u, int v){
G[now].u = u; G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;
} void Tarjan(int u){
dfn[u] = low[u] = ++ Tarjan_tim;
vis[u] = ;
Stack[++ topp] = u;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(!dfn[v]) {
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(vis[v]) low[u] = min(low[u], low[v]);
}
if(dfn[u] == low[u]){
vis[u] = ; belong[u] = ++ bel;
while(Stack[topp] != u){
vis[Stack[topp]] = ;
belong[Stack[topp]] = bel;
topp --;
}
topp --;
}
} int main()
{
n = read(); m = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= m; i ++) {
int u = read(); int v = read();
add(u, v);
}
for(int i = ; i <= n; i ++) if(!dfn[i]) Tarjan(i);
for(int u = ; u <= n; u ++){
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(belong[u] != belong[v]) Out[belong[u]] ++;
}
Cnt[belong[u]] ++;
}
int js = , Ans;
for(int i = ; i <= bel; i ++){
if(!Out[i]) {js ++; Ans = Cnt[i];}
}
if(js != ) cout << "";
else cout << Ans;
return ;
}
[Luogu] 受欢迎的牛的更多相关文章
- [Luogu 2341] HAOI2006 受欢迎的牛
[Luogu 2341] HAOI2006 受欢迎的牛 智能推的水题,一看是省选题就给做了,做一半才发现 Tarjan 算法忘干净了. Tarjan 求出SCC,算出每一个 SCC 包含原图的点数(s ...
- [Luogu P2341] [HAOI2006]受欢迎的牛 (缩点+bitset)
题面 传送门:https://www.luogu.org/problemnew/show/P2341 Solution 前排提示,本蒟蒻做法既奇葩又麻烦 我们先可以把题目转换一下. 可以把一头牛喜欢另 ...
- 洛谷 P2341 【受欢迎的牛】
题库:洛谷 题号:2341 题目:受欢迎的牛 link:https://www.luogu.org/problemnew/show/P2341 思路:因为奶牛的爱慕关系具有传递性,所以每个环(强连通分 ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4773 Solved: 2541[Submit][Sta ...
- bzoj 1051 (强连通) 受欢迎的牛
题目:这里 题意: Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
随机推荐
- 【Linux】一步一步学Linux——Linux发展史(01)
目录 00. 目录 01. Linux概述 02. Linux简史 03. Linux主要特性 04. Linux之父 05. Linux相关术语 06. Linux其它 07. Linux应用领域 ...
- 求亲篇:数据库操作,SqlHelper,增删改查
1.利用SqlHelper类 2.简单的数据绑定输出 string strSql = "select * from login"; DataTable dt = SqlHelper ...
- idea下spring boot jpa写原生sql的时候,报Cannot resolve table错误
错误如图 打开View→Tool Windows→Persistence选项 在弹出的Persistence窗口的项目上右键,选择Generate Persistence Mapping→By Dat ...
- hdu 2102 a计划问题。。 双层dfs问题
Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长 ...
- 获取ApplicationContext进而获取Ioc实例方法
在正常情况下 spring管理的类可直接调用Ioc容器中的实例,但在一些特殊情况下(例如拦截器中获取dao实例),Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该B ...
- .Net DLL类库引用时没有注释信息
自己编写的类库提供给别人引用时,别人获取不到DLL内部的方法.变量的注释信息,无法了解内部情况和使用方法. 原因:没有随DLL类库一同输出注释文档 解决方案: 在VS界面中选中提供给别人的类库项目 在 ...
- "多层感知器"--MLP神经网络算法
提到人工智能(Artificial Intelligence,AI),大家都不会陌生,在现今行业领起风潮,各行各业无不趋之若鹜,作为技术使用者,到底什么是AI,我们要有自己的理解. 目前,在人工智能中 ...
- snort_inline
snort_inline Link http://snort-inline.sourceforge.net/oldhome.html What is snort_inline? snort_inl ...
- php 5.6.36 安装mcrypt
相关扩展安装步骤 libmcrypt downloadUrl:https://sourceforge.net/projects/mcrypt/files/MCrypt/ versionCode:2.5 ...
- iOS开发微信支付的介绍与实现
1.前期准备 1) 到微信开放平台注册账号 需要登录邮箱验证 填写您的商户信息 2) 进入管理中心 --- 移动应用 --- 创建移动应用 --- 根据页面完善应用资料 3) 审核过后,通过应用详情页 ...